Reputation: 247
I am converting a page in a PDF document to bytes and then constructing an image out of it.
On Windows, the image is constructed fine. On Linux, the letters on the image look smudged (overlap each other)
In the logs (weblogic), i see the following indicating the fonts required are missing on 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>
How can supply the missing fonts on Linux? I see references to using a properties file (PDFBox_External_Fonts.properties) on versions before 2. What can i do on pdfbox version 2.0.17? I am unable to find any documentation on how to proceed.
Upvotes: 5
Views: 4149
Reputation: 2952
I am writing this specifically for the benefit who may have the same problem as the OP but are using PdfBox on a Linux WebApps installation on Microsoft-Azure. I am as well as providing a little bit more information not given in @user1187958 and @Lux's answers - for which I am thankful, as they helped me solve my problem.
As @user1187958 said above, it is possible to install the fonts in the one of the directories that PDFBox searches (via the following code)
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"};
}
}
However the problem is that all these directories are (to the best of my knowledge) deleted by Azure during a server restart. And in fact you apparently need a server restart for PDFBox to register that the fonts have been uploaded. So what I did - although I imagine there are better ways - is to extract org.apache.fontbox.util.autodetect.UnixFontDirFinder
from the PDFBox.jar, decompile it, add my own directory (as per the code extract below), and then insert it back into the .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"};
}
}
After uploading the new .jar, I then uploaded the requisite fonts to the directory /home/site/wwwroot/webapps/myapp/fonts
, restarted the server, and it worked.
Note that the fonts that are uploaded have to be one of the following formats .ttf, .otf, .pfb, .ttc as per the following code from org.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.");
}
Uploading TTF files from the C:/Windows/Fonts
directory will work, but the legality of such action needs to be checked.
Upvotes: 2
Reputation: 247
Tilman Hausherr from the PDFBox users mailing list helped out.
Copying the required fonts to the {home}/.fonts folder helped resolve my issue. The PDFBox code looks in the following directories for fonts.
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
};
}
Upvotes: 5
Reputation: 144
Linux : org.apache.fontbox.util.autodetect.UnixFontDirFinder.java
Windows : org.apache.fontbox.util.autodetect.WindowsFontsDirFinder.Java
the PDFBox load the system's fonts by above classes. you can check the sources.
Solution 1 : you can add the missing fonts to any Dir, then add find Dir in above classes
Solution 2 : as your metioned Tilman Hausher's solution.
one more thing : when PDFBox first load all fonts in system. then create a file named .pdfbox.cache. if you want PDFBox reload fonts or load your new added fonts , you need to delete that file first. please let me know if any concern.
Upvotes: 2