解决PDFBox警告: Could not load font file: C:\Windows\FONTS\mstmc.ttf java.io.EOFException

最近使用pdfbox生成PDF文件,导出报表;一开始就遇到个问题;

上下文

import java.io.File; 
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument; 
import org.apache.pdfbox.pdmodel.PDPage; 
import org.apache.pdfbox.pdmodel.PDPageContentStream; 
import org.apache.pdfbox.pdmodel.font.PDType1Font;
  
public class AddingContentDemo {
   public static void main (String args[]) throws IOException {
      //加载现有文档
      File file = new File("C:/PdfBox_Examples/my_doc.pdf");
      PDDocument document = PDDocument.load(file);
      
      //检索文档的页面
      PDPage page = document.getPage(1);
      PDPageContentStream contentStream = new PDPageContentStream(document, page);
     
      //开始内容流
      contentStream.beginText(); 
      
      //将字体设置为内容流
      contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);
      //设置线的位置
      contentStream.newLineAtOffset(25, 500);
      String text = "This is the sample document and we are adding content to it.";
      //以字符串形式添加文本 
      contentStream.showText(text);      
      //结束内容流
      contentStream.endText();
      System.out.println("Content added");
      //关闭内容流
      contentStream.close();
      //保存文档
      document.save(new File("C:/PdfBox_Examples/new.pdf"));
      //关闭文档
      document.close();
   }
}

版本依赖

<dependency>
  <groupId>org.apache.pdfboxgroupId>
  <artifactId>pdfboxartifactId>
  <version>2.0.27version>
dependency>

<dependency>
  <groupId>org.apache.pdfboxgroupId>
  <artifactId>fontboxartifactId>
  <version>2.0.13version>
dependency>

<dependency>
  <groupId>org.apache.pdfboxgroupId>
  <artifactId>pdfbox-toolsartifactId>
  <version>2.0.17version>
dependency>
...

报错信息

D:\ins\Java\JDK\jdk-11.0.14\bin\java.exe -javaagent:D:\ins\JetBrains\Toolbox\apps\IDEA-U\ch-1\212.5712.43\lib\idea_rt.jar=10566:D:\ins\JetBrains\Toolbox\apps\IDEA-U\ch-1\212.5712.43\bin -Dfile.encoding=UTF-8 -classpath F:\git-local\pdfboxDemo\target\classes;D:\ins\maven\repository\org\apache\httpcomponents\httpclient\4.5.13\httpclient-4.5.13.jar;D:\ins\maven\repository\org\apache\httpcomponents\httpcore\4.4.13\httpcore-4.4.13.jar;D:\ins\maven\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;D:\ins\maven\repository\commons-codec\commons-codec\1.11\commons-codec-1.11.jar;D:\ins\maven\repository\joda-time\joda-time\2.12.1\joda-time-2.12.1.jar;D:\ins\maven\repository\org\apache\pdfbox\pdfbox\2.0.27\pdfbox-2.0.27.jar;D:\ins\maven\repository\org\apache\pdfbox\fontbox\2.0.13\fontbox-2.0.13.jar;D:\ins\maven\repository\org\apache\pdfbox\pdfbox-tools\2.0.17\pdfbox-tools-2.0.17.jar;D:\ins\maven\repository\org\apache\pdfbox\pdfbox-debugger\2.0.17\pdfbox-debugger-2.0.17.jar;D:\ins\maven\repository\com\github\dhorions\boxable\1.7.0\boxable-1.7.0.jar;D:\ins\maven\repository\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;D:\ins\maven\repository\org\apache\commons\commons-csv\1.9.0\commons-csv-1.9.0.jar;D:\ins\maven\repository\org\jsoup\jsoup\1.15.1\jsoup-1.15.1.jar utils.PDFBoxLyj
12月 05, 2022 2:05:25 下午 org.apache.pdfbox.pdmodel.font.FileSystemFontProvider loadDiskCache
警告: New fonts found, font cache will be re-built
12月 05, 2022 2:05:25 下午 org.apache.pdfbox.pdmodel.font.FileSystemFontProvider <init>
警告: Building on-disk font cache, this may take a while
12月 05, 2022 2:05:25 下午 org.apache.pdfbox.pdmodel.font.FileSystemFontProvider addTrueTypeFont
警告: Could not load font file: C:\Windows\FONTS\mstmc.ttf
java.io.EOFException
	at org.apache.fontbox.ttf.TTFDataStream.readUnsignedInt(TTFDataStream.java:152)
	at org.apache.fontbox.ttf.TTFParser.readTableDirectory(TTFParser.java:312)
	at org.apache.fontbox.ttf.TTFParser.parse(TTFParser.java:139)
	at org.apache.fontbox.ttf.TTFParser.parse(TTFParser.java:87)
	at org.apache.pdfbox.pdmodel.font.FileSystemFontProvider.addTrueTypeFont(FileSystemFontProvider.java:666)
	at org.apache.pdfbox.pdmodel.font.FileSystemFontProvider.scanFonts(FileSystemFontProvider.java:383)
	at org.apache.pdfbox.pdmodel.font.FileSystemFontProvider.<init>(FileSystemFontProvider.java:361)
	at org.apache.pdfbox.pdmodel.font.FontMapperImpl$DefaultFontProvider.<clinit>(FontMapperImpl.java:141)
	at org.apache.pdfbox.pdmodel.font.FontMapperImpl.getProvider(FontMapperImpl.java:160)
	at org.apache.pdfbox.pdmodel.font.FontMapperImpl.findFont(FontMapperImpl.java:430)
	at org.apache.pdfbox.pdmodel.font.FontMapperImpl.findFontBoxFont(FontMapperImpl.java:393)
	at org.apache.pdfbox.pdmodel.font.FontMapperImpl.getFontBoxFont(FontMapperImpl.java:367)
	at org.apache.pdfbox.pdmodel.font.PDType1Font.<init>(PDType1Font.java:146)
	at org.apache.pdfbox.pdmodel.font.PDType1Font.<clinit>(PDType1Font.java:79)
	at utils.PDFBoxLyj.main(PDFBoxLyj.java:95)

12月 05, 2022 2:05:25 下午 org.apache.pdfbox.pdmodel.font.FileSystemFontProvider <init>
警告: Finished building on-disk font cache, found 192 fonts
Content added

进程已结束,退出代码为 0

Debug

提示加载某个字体文件时报错;
C:\Windows\FONTS\mstmc.ttf
对应系统目录看了下确实没有该文件;
解决PDFBox警告: Could not load font file: C:\Windows\FONTS\mstmc.ttf java.io.EOFException_第1张图片
然后去百度找这个字体想安装上,找了一圈也没找到,也没看到啥有关的文章;
https://blog.csdn.net/guozhangjie1992/article/details/103679640
找到个回答是修改源码的,感觉不太靠谱,还是治标不治本……

无奈卡住了;

去谷歌看看,找到了些有价值的问答;
https://stackoverflow.com/questions/67588149/pdfbox-eofexception-null-when-setting-textfield-value?newreg=4b300bf7c00643c3b2d49957be6e50b4

PDFBox的官网也有bug记录;
https://issues.apache.org/jira/browse/PDFBOX-5095

看他们的对话,提出问题的老哥应该也是中国人;(机翻了下)
解决PDFBox警告: Could not load font file: C:\Windows\FONTS\mstmc.ttf java.io.EOFException_第2张图片
解决PDFBox警告: Could not load font file: C:\Windows\FONTS\mstmc.ttf java.io.EOFException_第3张图片
按照他们的对话描述,网上找到mstmc.ttf打开看不太像是字体文件,或者这个字体文件已经损坏,explorer.exe文件资源管理器把这个文件忽略了所以看不到,直接使用CMD去这个路径下看看:

Microsoft Windows [版本 10.0.19044.2251]
(c) Microsoft Corporation。保留所有权利。

C:\Users\Administrator>cd /windows/fonts

C:\Windows\Fonts>dir /s mstmc*
 驱动器 C 中的卷是 系统
 卷的序列号是 0284-E676

 C:\Windows\Fonts 的目录

2022/05/04  02:05             4,096 mstmc.ttf
               1 个文件          4,096 字节

     所列文件总数:
               1 个文件          4,096 字节
               0 个目录  8,129,482,752 可用字节

C:\Windows\Fonts>copy mstmc.ttf d:\
C:\Windows\Fonts>del mstmc.ttf
已复制         1 个文件。

于是我把这个文件备份到D盘根目录,然后把当前的删掉了,再次运行程序就解决了;

你可能感兴趣的:(Java,java,pdf,pdfbox)