解决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博客

相关推荐
jinanwuhuaguo37 分钟前
(第三十三篇)五月的文明奠基:OpenClaw 2026.5.2版本的文明级解读
android·java·开发语言·人工智能·github·拓扑学·openclaw
xmjd msup1 小时前
spring security 超详细使用教程(接入springboot、前后端分离)
java·spring boot·spring
952362 小时前
SpringBoot统一功能处理
java·spring boot·后端
Lyyaoo.2 小时前
优惠券秒杀业务分析
java·开发语言
消失的旧时光-19432 小时前
统一并发模型:线程、Reactor、协程本质是一件事(从线程到协程 · 第6篇·终章)
java·python·算法
勿忘初心12212 小时前
Java 国密 SM4 加密工具类实战(Hutool + BouncyCastle)|企业级数据加密 + 兼容 JDK8
java·数据安全·数据加密·后端开发·企业级开发·国密 sm4
庞轩px2 小时前
第8篇:原子类与CAS底层原理——无锁并发的实现
java·cas·乐观锁·aba·无锁编程·自旋
rleS IONS2 小时前
SpringBoot中自定义Starter
java·spring boot·后端
苍煜3 小时前
慢SQL优化实战教学
java·数据库·sql
AI进化营-智能译站3 小时前
ROS2 C++开发系列16-智能指针管理传感器句柄|告别ROS2节点内存泄漏与野指针
java·c++·算法·ai