/simsun.ttc is not a valid TTF or OTF file.

错误提示

错误提示一:

/simsun.ttc is not a valid TTF or OTF file.

后话:看到这个提示你是不是真觉得是这个问题,不是有效的ttf,跟otf文件,然后就换成otf类型的文件,就出现了下边这个错误

错误提示二:

Caused by: com.lowagie.text.DocumentException: Table 'name' does not exist in /static/SourceHanSerifSC-VF.otf at com.lowagie.text.pdf.TrueTypeFont.getBaseFont(Unknown Source) ~[itext-2.1.7.jar:na] at com.lowagie.text.pdf.TrueTypeFont.process(Unknown Source) ~[itext-2.1.7.jar:na] at com.lowagie.text.pdf.TrueTypeFontUnicode.(Unknown Source) ~[itext-2.1.7.jar:na] at com.lowagie.text.pdf.BaseFont.createFont(Unknown Source) ~[itext-2.1.7.jar:na] at com.lowagie.text.pdf.BaseFont.createFont(Unknown Source) ~[itext-2.1.7.jar:na] at com.lowagie.text.pdf.BaseFont.createFont(Unknown Source) ~[itext-2.1.7.jar:na] at org.xhtmlrenderer.pdf.ITextFontResolver.addFont(ITextFontResolver.java:201) ~[flying-saucer-pdf-9.1.6.jar:na] at org.xhtmlrenderer.pdf.ITextFontResolver.addFont(ITextFontResolver.java:193) ~[flying-saucer-pdf-9.1.6.jar:na] at org.xhtmlrenderer.pdf.ITextFontResolver.addFont(ITextFontResolver.java:188) ~[flying-saucer-pdf-9.1.6.jar:na] at hu.rfit.textile.service.impl.PdfPrinterService.printPDF(PdfPrinterService.java:53) ~[TextileApp-1.0.0-SNAPSHOT.jar:1.0.0-SNAPSHOT:4749

说明

本文我是需要生成一个pdf

引入的Maven

复制代码
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.1</version>
</dependency>

代码部分

复制代码
      BaseFont bfChinese1 = BaseFont.createFont("/fonts/SourceHanSerifSC-VF.otf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        //字体
        Font titleChi1nese = new Font(bfChinese1, 18, Font.BOLD, BaseColor.BLACK);
        Font titleChin1ese3 = new Font(bfChinese1, 12, Font.BOLD,BaseColor.BLACK);
        Font secondtitl1eChinese = new Font(bfChinese1, 10, Font.NORMAL,BaseColor.BLACK);

        BaseFont bfChinese = BaseFont.createFont("/fonts/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        log.info("Font loaded successfully from: " +bfChinese );
        //字体
        Font titleChinese = new Font(bfChinese, 18, Font.BOLD, BaseColor.BLACK);
        Font titleChinese3 = new Font(bfChinese, 12, Font.BOLD,BaseColor.BLACK);
        Font secondtitleChinese = new Font(bfChinese, 10, Font.NORMAL,BaseColor.BLACK);

此时这样是不行的,不管怎么操作都会出现上边的那两问题

解决

pom.xml文件下边。build里边加上

复制代码
<resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>fonts/*</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <includes>
                <include>fonts/*</include>
            </includes>
        </resource>
</resources>

问题就这么解决了。还有就是字体文件不要直接放在resource下边,有可能会出错

相关推荐
自由随风飘2 小时前
python 题目练习1~5
开发语言·python
cynicme3 小时前
力扣3318——计算子数组的 x-sum I(偷懒版)
java·算法·leetcode
Bony-3 小时前
Go语言完全学习指南 - 从基础到精通------语言基础篇
服务器·开发语言·golang
青云交4 小时前
Java 大视界 -- Java 大数据在智能教育学习效果评估与教学质量改进实战
java·实时分析·生成式 ai·个性化教学·智能教育·学习效果评估·教学质量改进
崎岖Qiu4 小时前
【设计模式笔记17】:单例模式1-模式分析
java·笔记·单例模式·设计模式
fl1768314 小时前
基于python的天气预报系统设计和可视化数据分析源码+报告
开发语言·python·数据分析
Lei活在当下4 小时前
【现代 Android APP 架构】09. 聊一聊依赖注入在 Android 开发中的应用
java·架构·android jetpack
ACP广源盛139246256735 小时前
(ACP广源盛)GSV6172---MIPI/LVDS 信号转换为 Type-C/DisplayPort 1.4/HDMI 2.0 并集成嵌入式 MCU
c语言·开发语言·单片机·嵌入式硬件·音视频
不穿格子的程序员5 小时前
从零开始刷算法-栈-括号匹配
java·开发语言·
雪域迷影5 小时前
C#中通过get请求获取api.open-meteo.com网站的天气数据
开发语言·http·c#·get