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

使用docx4j 实现word转pdf(linux乱码处理)

由于系统之前使用了是itext进行转换的,现在已经不是开源的工具了,需要收费,然后进行改造,具体处理如下。

<dependency><groupId>org.docx4j</groupId><artifactId>docx4j</artifactId><version>6.1.2</version><exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></exclusion><exclusion><groupId>xalan</groupId><artifactId>xalan</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.docx4j</groupId><artifactId>docx4j-export-fo</artifactId><version>6.0.0</version><exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></exclusion><exclusion><groupId>xalan</groupId><artifactId>xalan</artifactId></exclusion></exclusions></dependency>

以上排查一下日志,因为和自己的有冲突,所以,自己项目看情况而定。

public static void writeWordModle(String inPath, String outPath, Map<String, String> params,String rootpath) throws Exception {FileInputStream is = null;FileOutputStream targetpdf = null;WordprocessingMLPackage wordMLPackage = null;CustomXWPFDocument doc = null;ByteArrayOutputStream baos = null;InputStream inputStream = null;try {// 加载 Word 文档File file = new File(inPath);is = new FileInputStream(file);doc = new CustomXWPFDocument(is);// 替换文本和表格中的变量WordUtils wordUtil = new WordUtils();wordUtil.replaceInPara(doc, params);    // 替换文本里面的变量wordUtil.replaceInTable(doc, params); // 替换表格里面的变量// 将 XWPFDocument 转换为字节数组baos = new ByteArrayOutputStream();doc.write(baos);baos.flush();byte[] docBytes = baos.toByteArray();// 使用字节数组创建输入流inputStream = new ByteArrayInputStream(docBytes);// 将输入流转换为 WordprocessingMLPackagewordMLPackage = WordprocessingMLPackage.load(inputStream);// 设置字体映射器IdentityPlusMapper fontMapper = new IdentityPlusMapper();//加载字体fontMapper = setFontMapper(fontMapper);//解决宋体(正文)和宋体(标题)的乱码问题,以下是在linux的关键PhysicalFonts.put("PMingLiU", PhysicalFonts.get("SimSun"));PhysicalFonts.put("新細明體", PhysicalFonts.get("SimSun"));wordMLPackage.setFontMapper(fontMapper);    // 执行转换targetpdf = new FileOutputStream(outPath);Docx4J.toPDF(wordMLPackage, targetpdf);} catch (Exception e) {e.printStackTrace();throw e;} finally {// 关闭所有流close(is);close(targetpdf);close(baos);close(inputStream);}}

因为我是通过word模版生成了流然后通过流转换成pdf的,可以使用File docFile = new File(docxPath);替代代码中的   inputStream = new ByteArrayInputStream(docBytes);

 //解决宋体(正文)和宋体(标题)的乱码问题,以下是在linux的关键PhysicalFonts.put("PMingLiU", PhysicalFonts.get("SimSun"));PhysicalFonts.put("新細明體", PhysicalFonts.get("SimSun"));

这句很关键,我测试了很久找了很多资料,这个解决了linux环境乱码的问题。

/** * 字体设置* @param mlPackage* @throws Exception*/private static IdentityPlusMapper setFontMapper( IdentityPlusMapper fontMapper) throws Exception {fontMapper.put("隶书", PhysicalFonts.get("LiSu"));fontMapper.put("宋体", PhysicalFonts.get("SimSun"));fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft Yahei"));fontMapper.put("黑体", PhysicalFonts.get("SimHei"));fontMapper.put("楷体", PhysicalFonts.get("KaiTi"));fontMapper.put("新宋体", PhysicalFonts.get("NSimSun"));fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));fontMapper.put("幼圆", PhysicalFonts.get("YouYuan"));fontMapper.put("华文宋体", PhysicalFonts.get("STSong"));fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong"));fontMapper.put("等线", PhysicalFonts.get("SimSun"));fontMapper.put("等线 Light", PhysicalFonts.get("SimSun"));fontMapper.put("华文琥珀", PhysicalFonts.get("STHupo"));fontMapper.put("华文隶书", PhysicalFonts.get("STLiti"));fontMapper.put("华文新魏", PhysicalFonts.get("STXinwei"));fontMapper.put("华文彩云", PhysicalFonts.get("STCaiyun"));fontMapper.put("方正姚体", PhysicalFonts.get("FZYaoti"));fontMapper.put("方正舒体", PhysicalFonts.get("FZShuTi"));fontMapper.put("华文细黑", PhysicalFonts.get("STXihei"));fontMapper.put("宋体扩展",PhysicalFonts.get("simsun-extB"));fontMapper.put("仿宋_GB2312",PhysicalFonts.get("FangSong_GB2312"));fontMapper.put("新細明體",PhysicalFonts.get("SimSun"));return fontMapper;}

设置字体,主要是windows使用,其实不设置也没问题。以上基本解决了代码的问题。

linux字体环境准备及处理

1、复制windows的C:\Windows\Fonts\下的所有字体库,放在桌面的fonts文件夹里

2、在linux目录/usr/share/fonts/chinese 创建文件夹chinese ,如果文件夹不存在,手动创建

3、把fonts拷贝上去,并给他授权chmod -R 755 /usr/share/fonts/chinese

4、# mkfontscale (如果提示 mkfontscale: command not found,需自行安装 # yum install mkfontscale )
# mkfontdir
# fc-cache -fv (如果提示 fc-cache: command not found,则需要安装# yum install fontconfig 

5、到路径vi /etc/fonts/fonts.conf 修改配置,把/usr/share/fonts/chinese添加进去

6、最后执行,fc-cache和fc-list :lang=zh  

/usr/share/fonts/chinese/simsun.ttc: SimSun\-PUA,宋体\-PUA:style=Regular
/usr/share/fonts/chinese/simsun.ttc: NSimSun,新宋体:style=Regular
/usr/share/fonts/chinese/simsunb.ttf: SimSun\-ExtB:style=Regular,Normal,oby?ejné,Standard,Κανονικ?,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/usr/share/fonts/chinese/SimsunExtG.ttf: SimSun\-ExtG:style=Regular
/usr/share/fonts/chinese/simsun.ttf: SimSun,宋体:style=Regular
/usr/share/fonts/chinese/simsun.ttc: SimSun,宋体:style=Regular

出现上面的类似即表示成功。

http://www.lqws.cn/news/488485.html

相关文章:

  • 支持java8的kafka版本
  • Halcon ——— OCR字符提取与多类型识别技术详解
  • Python Arrow 库详解:更智能的日期时间处理
  • 北京他山科技:全球首款AI触觉感知芯片破局者
  • 七八章习题测试
  • 半导体行业中的专用标准产品ASSP是什么?
  • SRS(Simple Realtime Server) 开源直播/双录服务器
  • 商品中心—11.商品B端搜索系统的实现文档二
  • 嵌入式系统内核镜像相关(六)
  • 网络/信号/电位跟踪
  • Actuator内存泄露及利用Swagger未授权自动化测试实现
  • 【软考高级系统架构论文】论面向方面的编程技术及其应用
  • 班翎流程平台 | 逐级审批支持多种选择模式
  • OpenHarmony中默认export 添加环境变量
  • 用html实现数字生命
  • 日志技术-Logback入门程序
  • Life:Internship in OnSea Day 1
  • 【nvidia-H100-ib排障实战2】:服务器 InfiniBand 网络性能问题深度分析
  • 深入理解JavaScript设计模式之迭代器模式
  • 【python实用小脚本-111】基于PyTorch的人脸口罩检测系统技术文档
  • 构建你的API防护盾 - 抵御恶意爬虫、注入与业务欺诈
  • qwen 的词编码模型中tokenid 到 高维向量是什么实现的,tokenid的排列有什么特点
  • 【Altium Designer】---- 02创建元器件符号和封装
  • 基本图算法介绍
  • Maven 之 JUnit 测试体系构建全解析
  • 淘宝直播带货API开发:弹幕抓取与商品点击热力图生成系统
  • 春秋云镜【CVE-2017-18349】fastjson wp
  • 从java到vue3:第一天
  • 支持向量机(SVM):数学引擎与工程实践深度解析——从最大间隔到核技巧的完整推导与应用
  • 【技术分享】XR技术体系浅析:VR、AR与MR的区别、联系与应用实践