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

相关推荐
BillKu13 分钟前
Java核心概念详解:JVM、JRE、JDK、Java SE、Java EE (Jakarta EE)
java·jvm·jdk·java ee·jre·java se·jakarta ee
感哥1 小时前
Docker网络
docker
刘婉晴1 小时前
【Java】NIO 简单介绍
java·nio
TextIn智能文档云平台1 小时前
复杂PDF文档结构化提取全攻略——从OCR到大模型知识库构建
pdf·ocr
会飞的小菠菜1 小时前
PDF文件中的广告二维码图片该怎么批量删除
pdf·删除·二维码·批量
渣哥1 小时前
聊聊我和 ArrayList、LinkedList、Vector 的“一地鸡毛”
java
浮游本尊1 小时前
Java学习第20天 - 性能优化与监控
java
纪莫2 小时前
技术面:Java并发(线程同步、死锁、多线程编排)
java·java面试⑧股
衍余未了2 小时前
k8s 内置的containerd配置阿里云个人镜像地址及认证
java·阿里云·kubernetes
叽哥2 小时前
Kotlin学习第 4 课:Kotlin 函数:从基础定义到高阶应用
android·java·kotlin