当前位置: 首页 > news >正文

解决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 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代码:

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) {}}
}

调用方式:

Word2PdfUtil.word2Pdf("你的word文件路径和文件名" , "你的pdf文件路径和文件名");

出现的问题

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

解决方案:

1:复制windows上的字体

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

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

-v /usr/share/fonts/CN/fonts/Fonts:/var/fonts

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

FontSettings.getDefaultInstance().setFontsFolder("/var/fonts" , true);

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

参考资料:

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

http://www.lryc.cn/news/504889.html

相关文章:

  • C# 生成随机数的方法
  • ip_done
  • 3D可视化引擎HOOPS Visualize与HOOPS Luminate Bridge的功能与应用
  • Docder 搭建Redis分片集群 散片插槽 数据分片 故障转移 Java连接
  • 校园交友app/校园资源共享小程序/校园圈子集合二手物品交易论坛、交友等综合型生活服务社交论坛
  • Chaos Mesh云原生的混沌测试平台搭建
  • Vue3之组合式API详解
  • 大模型的构建与部署(3)——数据标注
  • AI发展与LabVIEW程序员就业
  • 本地事务 + 消息队列事务方案设计
  • pinctrl子系统学习笔记
  • 使用vue-element 的计数器inputNumber,传第三个参数
  • 如何从0构建一个flask项目,直接上实操!!!
  • Mongoose连接数据库操作实践
  • centos 7.9 freeswitch1.10.9环境搭建
  • Gitlab服务管理和仓库项目权限管理
  • LLMs之Llama-3:Llama-3.3的简介、安装和使用方法、案例应用之详细攻略
  • OpenCV函数及其应用
  • vulnhub靶场【DriftingBlues】之3
  • 文件上传—阿里云OSS对象存储
  • mybatis-plus超详细讲解
  • 【Linux】--- 进程的概念
  • Unity NTPComponent应用, 实现一个无后端高效获取网络时间的组件
  • go语言使用zlib压缩[]byte
  • Windows 配置 Tomcat环境
  • 【python从入门到精通】-- 第六战:列表和元组
  • Python | 数据可视化中常见的4种标注及示例
  • LearnOpenGL学习(高级OpenGL -> 高级GLSL,几何着色器,实例化)
  • Scala学习记录
  • vue使用pdfh5.js插件,显示pdf文件白屏