Apache poi xwpf word转PDF中文显示问题解决

原问题解决方法:https://github.com/opensagres/xdocreport/issues/161

POM依赖

XML 复制代码
   <properties>
        <java.version>1.8</java.version>
        <poi.version>3.14</poi.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>${poi.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>${poi.version}</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
            <version>2.0.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.xdocreport.document</artifactId>
            <version>2.0.1</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

请记住,word转换如果没问题,那么就要设置pdf的字体,这个是铁律!

核心配置代码

将PdfOptions提出来作为方法使用

java 复制代码
 private PdfOptions getPdfOptions() {
        PdfOptions pdfOptions = PdfOptions.create();
        // 解决中文不显示问题
        pdfOptions.fontProvider(new IFontProvider() {
            @Override
            public Font getFont(String familyName, String encoding, float size, int style, Color color) {
                try {
                    File file = new File(pdfChineseTTF);
                    Preconditions.checkState(file.exists(), "中文字体文件不存在:" + pdfChineseTTF);
                    BaseFont bfChinese = BaseFont.createFont(pdfChineseTTF, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                    Font fontChinese = new Font(bfChinese, size, style, color);
                    if (familyName != null) {
                        fontChinese.setFamily(familyName);
                    }
                    return fontChinese;
                } catch (Throwable e) {
                    e.printStackTrace();
                    // An error occurs, use the default font provider.
                    return ITextFontRegistry.getRegistry().getFont(familyName, encoding, size, style, color);
                }
            }
        });
        return pdfOptions;
    }

PDF下载流问题

注意前端PDF文件流空白问题参考以下方案进行处理

js下载PDF文件流,打开空白问题_使用jspdf导出服务器上pdf文件的时候文件里面没有内容_夏小锋的博客-CSDN博客

前端下载二进制流pdf文件,下载成功,打开空白问题解决_前端下载pdf文件流打开空白_Helen_fight的博客-CSDN博客

相关推荐
DS随心转插件25 分钟前
ChatGPT或Gemini如何生成word文档
人工智能·ai·chatgpt·word·deepseek·ds随心转
白狐_7981 天前
【疑难杂症】Word 惊现“数字 7 消失术”:特定字体 GBK 编码下的渲染陷阱排查
word
weixin_6681 天前
Agent 应该中 PDF 工具方案分析报告 - AI分析分享
人工智能·pdf
fjhcom1 天前
PDF与图片互转WEB应用开发教程
前端·pdf·图片·web应用·streamlit
polaris06301 天前
【Spring Boot 实现 PDF 导出】
spring boot·后端·pdf
南风微微吹2 天前
【2026大英赛】全国大学生英语竞赛C类历年真题试卷及答案电子版PDF(2012-2025年)
pdf·大英赛
热爱生活的五柒2 天前
为什么word里面没有墨迹公式,从哪找
word
南风微微吹2 天前
【专八】英语专业八级TEM8历年真题及答案解析电子版PDF(2009-2025年)
pdf·英语专八
西门吹-禅2 天前
【pdf 翻译免费】
pdf
薛定谔之死猫2 天前
Ruby简单粗暴把图片合成PDF文档
java·pdf·ruby