Linux 上的 PDFBox 2.0.17 字体



我正在将PDF文档中的页面转换为字节,然后从中构建图像。

在 Windows 上,图像构造良好。在 Linux 上,图像上的字母看起来很脏(彼此重叠(

在日志(网络逻辑(中,我看到以下内容,表明 Linux 上缺少所需的字体。

<Dec 3, 2019 11:06:35 PM EST> <Warning> <org.apache.pdfbox.pdmodel.font.PDType1Font> <BEA-000000> <Using fallback font LiberationSans for Helvetica-Bold>
<Dec 3, 2019 11:06:35 PM EST> <Warning> <org.apache.pdfbox.pdmodel.font.PDType1Font> <BEA-000000> <Using fallback font LiberationSans for Times-Roman>
<Dec 3, 2019 11:06:35 PM EST> <Warning> <org.apache.pdfbox.pdmodel.font.PDType1Font> <BEA-000000> <Using fallback font LiberationSans for Times-Bold>
<Dec 3, 2019 11:06:35 PM EST> <Warning> <org.apache.pdfbox.pdmodel.font.PDType1Font> <BEA-000000> <Using fallback font LiberationSans for Times-Italic>
<Dec 3, 2019 11:06:35 PM EST> <Warning> <org.apache.pdfbox.pdmodel.font.PDType1Font> <BEA-000000> <Using fallback font LiberationSans for Helvetica>

如何在 Linux 上提供缺少的字体?我看到在 2 之前的版本上使用属性文件 (PDFBox_External_Fonts.properties( 的引用。我可以在pdfbox版本2.0.17上做什么?我找不到有关如何继续的任何文档。

来自PDFBox用户邮件列表的Tilman Hausherr提供了帮助。

将所需的字体复制到 {home}/.fonts 文件夹有助于解决我的问题。 PDFBox 代码在以下目录中查找字体。

protected String[] getSearchableDirectories()
{
return new String[] { System.getProperty("user.home") + "/.fonts", // user
"/usr/local/fonts", // local
"/usr/local/share/fonts", // local shared
"/usr/share/fonts", // system
"/usr/X11R6/lib/X11/fonts" // X
};
}

Linux : org.apache.fontbox.util.autodetect.UnixFontDirFinder.java Windows
: org.apache.fontbox.util.autodetect.WindowsFontsDirFinder.Java
PDFBox通过上述类加载系统的字体。 您可以检查来源。
解决方案1:您可以将缺少的字体添加到任何目录,然后在上述类
中添加查找目录 解决方案2:作为蒂尔曼豪舍的解决方案。

还有一件事:当PDFBox首先加载系统中的所有字体时。 然后创建一个名为 .pdfbox.cache 的文件。 如果您希望 PDFBox 重新加载字体或加载新添加的字体 ,您需要先删除该文件。 如果有任何疑问,请告诉我。

我写这篇文章是为了那些可能与OP有相同问题但在Microsoft-Azure上的Linux WebApps安装上使用PdfBox的人。我还提供了@user1187958和@Lux的答案中没有给出的更多信息 - 对此我很感激,因为他们帮助我解决了我的问题。

如上所述@user1187958,可以在PDFBox搜索的目录之一中安装字体(通过以下代码(

package org.apache.fontbox.util.autodetect;
public class UnixFontDirFinder extends NativeFontDirFinder
{
protected String[] getSearchableDirectories() {
return new String[] { System.getProperty("user.home") + "/.fonts", "/usr/local/fonts", "/usr/local/share/fonts", "/usr/share/fonts", "/usr/X11R6/lib/X11/fonts"};
}
}

但是,问题是所有这些目录(据我所知(在服务器重新启动期间都被 Azure 删除。事实上,您显然需要重新启动服务器才能让PDFBox注册字体已上传。所以我所做的 - 尽管我想有更好的方法 - 是从PDFBox中提取org.apache.fontbox.util.autodetect.UnixFontDirFinder.jar,反编译它,添加我自己的目录(根据下面的代码摘录(,然后将其插入回.jar

package org.apache.fontbox.util.autodetect;
public class UnixFontDirFinder extends NativeFontDirFinder
{
protected String[] getSearchableDirectories() {
return new String[] { System.getProperty("user.home") + "/.fonts", "/usr/local/fonts", "/usr/local/share/fonts", "/usr/share/fonts", "/usr/X11R6/lib/X11/fonts" 
,"/home/site/wwwroot/webapps/myapp/fonts"};
}
}

上传新.jar后,我将必要的字体上传到目录/home/site/wwwroot/webapps/myapp/fonts,重新启动服务器,它就可以工作了。

请注意,根据以下代码,上传的字体必须是以下格式之一 .ttf、.otf、.pfb、.ttcorg.apache.fontbox.util.autodetect.FileFinder.java

private boolean checkFontfile(final File file) {
final String name = file.getName().toLowerCase(Locale.US);
return (name.endsWith(".ttf") || name.endsWith(".otf") || name.endsWith(".pfb") || name.endsWith(".ttc")) && !name.startsWith("fonts.");
}

C:/Windows/Fonts目录上传 TTF 文件将起作用,但需要检查此类操作的合法性。

最新更新