• Word转PDF简单示例,分别在windows和centos中完成转换


    概述

    本篇博客以简单的示例代码分别在Windows和Linux环境下完成Word转PDF的文档转换。

    文章提供SpringBoot + Vue3的示例代码。

    文章为什么要分为Windows和Linux环境?

    因为在如下提供的Windows后端示例代码中使用documents4j库做转换,此库需要调用命令行工具,并且需要安装Microsoft Word,但在Linux上无法安装Microsoft Word,因此如下提供了两份后端代码。

     过程

    前端传入word文件 -> 后端处理 -> 返回转换后的字节数组(byte[])

    Windows后端代码

    maven依赖

    1. <dependency>
    2. <groupId>com.documents4jgroupId>
    3. <artifactId>documents4j-localartifactId>
    4. <version>1.0.3version>
    5. dependency>
    6. <dependency>
    7. <groupId>com.documents4jgroupId>
    8. <artifactId>documents4j-transformer-msoffice-wordartifactId>
    9. <version>1.0.3version>
    10. dependency>

     示例代码

    1. // controller接口
    2. @PostMapping("/upload")
    3. public byte[] convertDocxToPdf(@RequestParam("file") MultipartFile file) throws IOException {
    4. if (!file.getOriginalFilename().endsWith(".docx")) {
    5. throw new IllegalArgumentException("文件类型不支持");
    6. }
    7. try (InputStream docxInputStream = file.getInputStream();
    8. ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
    9. IConverter converter = LocalConverter.builder().build();
    10. converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
    11. return outputStream.toByteArray();
    12. }
    13. }

    CentOS后端代码

    maven依赖

    1. <dependency>
    2. <groupId>org.jodconvertergroupId>
    3. <artifactId>jodconverter-localartifactId>
    4. <version>4.4.2version>
    5. dependency>

    示例代码

    1. @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    2. public byte[] uploadFile(@RequestParam("file") MultipartFile file) throws IOException, OfficeException {
    3. if (file.getOriginalFilename().endsWith(".docx")) {
    4. LocalOfficeManager officeManager = LocalOfficeManager.install();
    5. try {
    6. officeManager.start();
    7. DocumentConverter converter = LocalConverter.builder().officeManager(officeManager).build();
    8. ByteArrayOutputStream out = new ByteArrayOutputStream();
    9. converter.convert(file.getInputStream()).as(DefaultDocumentFormatRegistry.DOCX).to(out).as(DefaultDocumentFormatRegistry.PDF).execute();
    10. return out.toByteArray();
    11. } finally {
    12. OfficeUtils.stopQuietly(officeManager);
    13. }
    14. } else {
    15. throw new IOException("文件类型不支持");
    16. }
    17. }

    *** 当使用上方的代码进行word转pdf之后,输出的很大可能会出现中文文字不能正确显示,文字全部变成小矩形框。

    这是因为在linux上没有中文字体库导致的。

     在centos7中安装中文字体库 

    1.首先检查安装所需要的工具

    yum -y install fontconfig

    yum -y install ttmkfdir

     2.之后检查/usr/share目录是否有fonts 和 fontconfig

    3.创建chinese目录,用于存放我们需要的字体

    在/usr/share/fonts下创建chinese

    4.下载需要的字体

    我们到自己的windows电脑上查找想要的字体,访问C:\Windows\Fonts

    可以搜索自己文档转换过程中需要的字体,例如:宋体

    将字体拷贝放到centos的/usr/share/fonts/chinese目录中,并修改chinese目录的权限:

    chmod -R 755 /usr/share/fonts/chinese

    5.执行命令,生成 TrueType 字体的字体度量

    ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir

    6.配置刚才创建中文字体目录,使之生效即可。

    vi /etc/fonts/fonts.conf 

    7.执行命令,刷新字体缓存

    fc-cache 

     至此,重新访问后端服务进行word转pdf会发现字体成功显示。

    前端测试代码

     在此提供与后端代码配套测试的前端代码(vue3)