/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下边,有可能会出错

相关推荐
文丑颜不良啊7 小时前
LeetCode2981~3000题目解析
java·leetcode
nice_lcj5208 小时前
【从零理解 Spring AI · 13】RAG 要解决什么:把“闭卷考试“变成“开卷“
java·spring·检索增强·rag·spring ai·ai应用开发
pps-key8 小时前
网络攻防入门:从“黑客梦”到合法实操(生存篇)
开发语言·web安全·php
学长毕设9 小时前
基于SpringBoot军事纪念馆管理系统设计与实现-附源码
java·spring boot·后端
汤姆宝库9 小时前
BPE分词器的完整实现:字节对合并与词表构建
开发语言·人工智能·c#
这个昵称不容易10 小时前
SpringBoot 医疗病历交互系统的设计与实现:192 个 Java 文件、256 个接口、22 张表,附真实运行截图
java·springboot·毕设
驭渊的小故事11 小时前
订单服务如何调用商品服务?一次 RestTemplate 跨服务调用入门
java·开发语言·php
Sakura_j_h_12 小时前
MCP SDK 2.0 实战:用 MCPServer 搭本地笔记检索服务
开发语言·笔记·python·qt·ai工具·mcp·mcpserver
微笑星河q12 小时前
C++STL【vector】常用接口函数使用和迭代器失效问题
开发语言·c++
Eider199813 小时前
Python基础语法之并发相关3
开发语言·python