解决docker环境下aspose-words转换word成pdf后乱码问题

描述

环境:docker

部署工具:Jenkins

需求:本地上传的word文档需要转换成pdf

问题:转换之后的pdf文档出现小框框(乱码)

转换成PDF的操作

pom:

复制代码
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.10.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.10.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.10.1</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.core</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.xdocreport.itext.extension</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.luhuiguo</groupId>
            <artifactId>aspose-words</artifactId>
            <version>23.1</version>
        </dependency>

License:

在resources文件夹下新增license.xml文件

复制粘贴下方代码:

XML 复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<License>
    <Data>
        <Products>
            <Product>Aspose.Total for Java</Product>
            <Product>Aspose.Words for Java</Product>
        </Products>
        <EditionType>Enterprise</EditionType>
        <SubscriptionExpiry>20991231</SubscriptionExpiry>
        <LicenseExpiry>20991231</LicenseExpiry>
        <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
    </Data>
    <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>

Java代码:

java 复制代码
import com.aspose.words.*;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.*;
import java.util.Objects;

/**
 * word转pdf
 * @author Dr.Monster
 */
public class Word2PdfUtil {
    public static void word2PdfPoi(String docFile,String pdfFile) {
        try{
            XWPFDocument document;
            InputStream doc = new FileInputStream(docFile);
            document = new XWPFDocument(doc);
            PdfOptions options = PdfOptions.create();
            OutputStream out = new FileOutputStream(pdfFile);
            PdfConverter.getInstance().convert(document, out, options);
            doc.close();
            out.close();
        }catch (Exception e){

        }
    }

    /**
     * 获取 license 去除水印
     * 若不验证则转化出的pdf文档会有水印产生
     */
    private static void getLicense() {
        String licenseFilePath = "word-license.xml";
        try {
            InputStream is = Word2PdfUtil.class.getClassLoader().getResourceAsStream(licenseFilePath);
            License license = new License();
            license.setLicense(Objects.requireNonNull(is));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * word 转 pdf
     *
     * @param wordFile word 文件路径
     * @param pdfFile  生成的 pdf 文件路径
     */
    public static void word2Pdf(String wordFile, String pdfFile) {
        File file = new File(pdfFile);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdir();
        }
        getLicense();
        try (FileOutputStream os = new FileOutputStream(new File(pdfFile))) {
            Document doc = new Document(wordFile);
//            FontSettings.getDefaultInstance().setFontsFolder("/usr/share/fonts/CN/fonts/Fonts" , true);
            FontSettings.getDefaultInstance().setFontsFolder("/var/fonts" , true);
            doc.save(os, SaveFormat.PDF);
        } catch (Exception e) {

        }
    }

    /**
     * word 转 pdf
     *
     * @param wordFile word 文件流
     * @param pdfFile  生成的 pdf 文件流
     */
    public static void word2Pdf(InputStream wordFile, OutputStream pdfFile) {
        getLicense();
        try {
            Document doc = new Document(wordFile);
            doc.save(pdfFile, SaveFormat.PDF);
        } catch (Exception e) {

        }
    }
}

调用方式:

java 复制代码
Word2PdfUtil.word2Pdf("你的word文件路径和文件名" , "你的pdf文件路径和文件名");

出现的问题

word文档可以正常转换,本地文件转换显示正常,但是在服务上转换后,显示乱码,原因是没有对应字体。

解决方案:

1:复制windows上的字体

2:复制到/usr/share/fonts/CN/fonts/Fonts路径下(没有可以自己新增,理论上来说,任何地方都可以)

3:建立与容器与机器的文件映射关系,并在Jenkins中进行配置

XML 复制代码
-v /usr/share/fonts/CN/fonts/Fonts:/var/fonts

4:在代码中设置字体所在目录

java 复制代码
FontSettings.getDefaultInstance().setFontsFolder("/var/fonts" , true);

注意,目录为容器中的挂载目录,不是机器的目录,通过容器中的地址,可以直接访问机器中对应映射关系的地址。

参考资料:

java将word转pdf_java word转pdf poi-CSDN博客

相关推荐
天河归来2 小时前
第三方接口不稳定,如何让核心任务继续完成
java·大数据
Knight_AL2 小时前
Java 文件下载时文件名中的 `#` 被转换成 `%23`?一次 URL 编码与解码问题排查
java·开发语言
螺蛳粉 螺蛳粉3 小时前
第 05 篇:etcd 深入:Raft 共识、watch 机制与快照备份恢复实战(底座认知 05)
docker·云原生·容器·kubernetes·etcd·分布式存储·kube-apiserver
砚底藏山河4 小时前
python量化自定义周期 K 线合成
java·开发语言·数据库·python·金融
猿究院_夏燊4 小时前
MySQL 的四种隔离级别
java·数据库·mysql
WORLDPO92264 小时前
QTE-020-01-L/F/C-D-A-K-TR_实物_数据手册PDF_国产替代
pdf
码流子5 小时前
移动收费怎么做?手持终端+App+收费中心的架构实践
java·开发语言·架构
霸道流氓气质5 小时前
AI 流式对话与实时聊天平台:SSE 解码竞态、有状态会话隔离与背压取消的 Vue3 + Spring AI 生产级实战
java·人工智能·spring
云创智城-yuncitys5 小时前
YunParking 4.0:JDK 17 与 Spring Boot 3.2 的停车云升级
java·开发语言·spring boot
文丑颜不良啊5 小时前
LeetCode1661~1680题目解析
java·leetcode