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

使用docx4j转换word为pdf处理中文乱码问题

word转pdf

    • 实现方法
  • maven
    • docx4j版本自己酌情升级

实现方法

import org.docx4j.Docx4J;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import cn.hutool.core.io.FileUtil;
import java.io.*;@Slf4j
@Service
public class FileService {/*** 获取pdf文件,通过word文件转换** @param file word文件* @date 2023/08/26 23:13*/public File getPdfByWordFile(File file) {Mapper fontMapper = new IdentityPlusMapper();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"));//解决宋体(正文)和宋体(标题)的乱码问题PhysicalFonts.put("PMingLiU", PhysicalFonts.get("SimSun"));PhysicalFonts.put("新細明體", PhysicalFonts.get("SimSun"));//输出空文件File outFile = FileUtil.createTempFile(".pdf", true);WordprocessingMLPackage pkg = null;try (InputStream resources = FileUtil.getInputStream(file)) {pkg = Docx4J.load(resources);pkg.setFontMapper(fontMapper);} catch (Exception e) {throw new RuntimeException(e);}try (FileOutputStream outputStream = new FileOutputStream(outFile)) {Docx4J.toPDF(pkg, outputStream);} catch (Exception e) {log.error("生成pdf文件异常");}return outFile;}
}

maven

docx4j版本自己酌情升级

可能存在漏洞
Dependency maven:org.apache.xmlgraphics:xmlgraphics-commons:2.3 is vulnerable

Upgrade to 2.9

CVE-2020-11988, Score: 8.2

Apache XmlGraphics Commons 2.4 and earlier is vulnerable to server-side request forgery, caused by improper input validation by the XMPParser. By using a specially-crafted argument, an attacker could exploit this vulnerability to cause the underlying server to make arbitrary GET requests. Users should upgrade to 2.6 or later.

        <!-- word转pdf --><dependency><groupId>org.docx4j</groupId><artifactId>docx4j-JAXB-Internal</artifactId><version>8.2.4</version><exclusions><exclusion><groupId>xerces</groupId><artifactId>xercesImpl</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.docx4j</groupId><artifactId>docx4j-export-fo</artifactId><version>8.2.4</version></dependency>
http://www.lryc.cn/news/333972.html

相关文章:

  • 【引子】C++从介绍到HelloWorld
  • Django检测到会话cookie中缺少HttpOnly属性手工复现
  • 2024数字城市建设博览会:一站式平台,满足多元需求
  • iOS 17.5系统或可识别并禁用未知跟踪器,苹果Find My技术应用越来越合理
  • 关于搭建elk日志平台
  • 【全套源码教程】基于SpringBoot+MyBatis+Vue的流浪动物救助网站的设计与实现
  • Word wrap在计算机代表的含义(自动换行)
  • 室友打团太吵?一条命令让它卡死
  • RabbitMQ3.13.x之八_RabbitMQ中数据文件和目录位置
  • 仿抖音短视频直播带货刷一刷商城社交电商源码系统小程序APP开发
  • Vue - 你知道Vue组件中的data为什么是一个函数吗
  • MUX VLAN
  • 漫谈:“标准”是一种幻觉 C++语言标准的意义
  • 【Wbpack原理】基础流程解析,实现 mini-webpack
  • Debian 安装 python 3.9.6
  • 搜索二维矩阵2 合并两个有序链表
  • 深入Tauri开发——从环境搭建到项目构建
  • Redis 和 Mysql 数据库数据如何保持一致性
  • 探索7个MAMP本地开发环境的高效替代软件
  • 靡语IT:Bootstrap 简介
  • 亚马逊店铺引流:海外云手机的利用方法
  • 10 Python进阶:MongoDB
  • Leetcode 142. 环形链表 II和Leetcode 242. 有效的字母异位词
  • 【嵌入式DIY实例】-MODBUS串行通信
  • 入门用Hive构建数据仓库
  • 【计算机网络】会话层
  • springboot实现七牛云的文件上传下载
  • 【RISC-V 指令集】RISC-V 向量V扩展指令集介绍(六)- 向量内存一致性模型
  • Lvgl9 WindowsSimulator Visual Studio2017
  • 【STL】链表(list)