documents4j 将word转pdf文件,本地(Windows)测试没问题,摆设到服务器( ...

打印 上一主题 下一主题

主题 985|帖子 985|积分 2965

问题

报错如下:

代码

首先要包管你的Java代码没问题,可以参考下面代码
maven依靠
  1. <!--documents4j-->
  2. <dependency>
  3.     <groupId>com.documents4j</groupId>
  4.     <artifactId>documents4j-local</artifactId>
  5.     <version>1.0.3</version>
  6. </dependency>
  7. <!-- documents4j-->
  8. <dependency>
  9.     <groupId>com.documents4j</groupId>
  10.     <artifactId>documents4j-transformer-msoffice-word</artifactId>
  11.     <version>1.0.3</version>
  12. </dependency>
复制代码
DocxToPdfUtil工具类
  1. import com.documents4j.api.DocumentType;
  2. import com.documents4j.api.IConverter;
  3. import com.documents4j.job.LocalConverter;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10. import lombok.extern.slf4j.Slf4j;
  11. @Slf4j
  12. public class DocxToPdfUtil {
  13.     /**
  14.      * 通过documents4j 实现word转pdf
  15.      *
  16.      * @param sourcePath 源文件地址 如 /root/example.doc
  17.      */
  18.     public static File documents4jWordToPdf(String sourcePath) {
  19.         return documents4jWordToPdf(new File(sourcePath));
  20.     }
  21.     /**
  22.      * 通过documents4j 实现word转pdf
  23.      *
  24.      * @param file 源文件
  25.      */
  26.     public static File documents4jWordToPdf(File file) {
  27.         String os = System.getProperty("os.name").toLowerCase();
  28.         log.info("当前系统:{}", os);
  29.         if (os.contains("win")) {
  30.             // Windows操作系统
  31.             return winDocuments4jWordToPdf(file);
  32.         } else if (os.contains("nix") || os.contains("nux") || os.contains("mac")) {
  33.             // Unix/Linux/Mac操作系统
  34.             return linuxDocuments4jWordToPdf(file);
  35.         } else {
  36.             // 未知操作系统
  37.             throw new RuntimeException("不支持当前操作系统转换文档");
  38.         }
  39.     }
  40.     /**
  41.      * 通过documents4j 实现word转pdf -- Windows 环境 需要有 Microsoft Office 服务
  42.      *
  43.      * @param file 源文件
  44.      */
  45.     public static File winDocuments4jWordToPdf(File file) {
  46.         String parentPath = file.getParent();
  47.         File outputFile = new File(parentPath + File.separator + file.getName().replaceAll("\\.(docx?|\\w+)$", "") + ".pdf");
  48.         try {
  49.             // 这种方式在Linux服务器不可用,所以除非你是window服务器
  50.             InputStream docxInputStream = new FileInputStream(file);
  51.             OutputStream outputStream = new FileOutputStream(outputFile);
  52.             IConverter converter = LocalConverter.builder().build();
  53.             converter.convert(docxInputStream)
  54.                     .as(DocumentType.DOCX)
  55.                     .to(outputStream)
  56.                     .as(DocumentType.PDF).execute();
  57.             docxInputStream.close();
  58.             outputStream.close();
  59.             return outputFile;
  60.         } catch (Exception e) {
  61.             e.printStackTrace();
  62.             return null;
  63.         }
  64.     }
  65.     /**
  66.      * 通过documents4j 实现word转pdf -- linux 环境 需要有 libreoffice 服务
  67.      *
  68.      * @param file 源文件
  69.      */
  70.     public static File linuxDocuments4jWordToPdf(File file) {
  71.         // 获取文件的绝对路径和目录路径
  72.         String absolutePath = file.getAbsolutePath();
  73.         String parentPath = file.getParent();
  74.         // 构建LibreOffice的命令行工具命令
  75.         String commands = "libreoffice --convert-to pdf "
  76.                 + absolutePath + " --outdir " + parentPath;
  77.         // 执行转换命令
  78.         try {
  79.             boolean result = executeLinuxCmd(commands);
  80.             if (result) {
  81.                 // 转换成功,返回转换后的PDF文件
  82.                 String pdfFilePath = parentPath + File.separator + file.getName().replaceAll("\\.(docx?|\\w+)$", "") + ".pdf";
  83.                 log.info(pdfFilePath);
  84.                 log.info(pdfFilePath);
  85.                 return new File(pdfFilePath);
  86.             } else {
  87.                 return null;
  88.             }
  89.         } catch (Exception e) {
  90.             // 转换失败
  91.             log.error("Word文档转换为PDF失败,原因:执行命令时出现异常。", e);
  92.             return null;
  93.         }
  94.     }
  95.     /**
  96.      * 执行命令行
  97.      *
  98.      * @param cmd 命令行
  99.      * @return
  100.      * @throws IOException
  101.      */
  102.     private static boolean executeLinuxCmd(String cmd) throws IOException {
  103.         // 执行命令行工具命令
  104.         Process process = Runtime.getRuntime().exec(cmd);
  105.         try {
  106.             process.waitFor();
  107.         } catch (InterruptedException e) {
  108.             log.error("执行 Linux 命令异常:", e);
  109.             return false;
  110.         }
  111.         return true;
  112.     }
  113. }
复制代码
测试

  1. @SpringBootApplication
  2. public class PdfdemoApplication {
  3.     public static void main(String[] args) {
  4.         //Linux服务器
  5.          String inputFile = "/usr/local/a.docx";
  6.         //本地
  7. //        String inputFile = "C:\\zhushanglin\\test\\a.docx";
  8.         
  9.         DocxToPdfUtil.documents4jWordToPdf(inputFile);
  10.         System.out.println("Done! Pdf ");
  11.         SpringApplication.run(PdfdemoApplication.class, args);
  12.     }
  13. }
复制代码
发现本地win没问题,到服务器Linux就有问题,缘故起因是 documents4j 使用 Microsft Office 的 APIs 来举行文档转换,因此须要在Linux上安装 OpenOffice/LibreOffice 编辑器
Ubuntu:
  1. sudo apt-get install libreoffice
复制代码
CentOS:
  1. sudo yum install libreoffice
复制代码
把这装上后,再测试,发现可以完美转换。

下课!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

篮之新喜

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表