【问题标题】:PDFBox 2.0.17 Fonts on LinuxLinux 上的 PDFBox 2.0.17 字体
【发布时间】:2019-12-04 04:12:11
【问题描述】:

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

在 Windows 上,图像构造良好。在 Linux 上,图像上的字母看起来有污迹(相互重叠)

在日志 (weblogic) 中,我看到以下内容表明 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 上做什么?我找不到任何有关如何继续的文档。

【问题讨论】:

    标签: java pdfbox


    【解决方案1】:

    PDFBox users mailing list 的 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
         };
     }
    

    【讨论】:

      【解决方案2】:

      我写这篇文章是为了帮助那些可能遇到与 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.jar 中提取 org.apache.fontbox.util.autodetect.UnixFontDirFinder,反编译它,添加我自己的目录(根据下面的代码提取),然后将它插入回 .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,重新启动服务器,它工作了。

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

      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文件是可行的,但需要检查这种行为的合法性。

      【讨论】:

        【解决方案3】:

        Linux : org.apache.fontbox.util.autodetect.UnixFontDirFinder.java
        Windows:org.apache.fontbox.util.autodetect.WindowsFontsDirFinder.Java
        PDFBox 通过上述类加载系统的字体。您可以查看来源。
        解决方案 1:您可以将缺少的字体添加到任何 Dir,然后在上面的类中添加 find Dir
        解决方案 2:正如您提到的 Tilman Hausher 的解决方案。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-10
          • 2016-01-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多