Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDFBox 2.0.17 Fonts on Linux

Tags:

java

pdfbox

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.

like image 628
user1187958 Avatar asked Jan 26 '23 13:01

user1187958


2 Answers

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
     };
 }
like image 195
user1187958 Avatar answered Jan 28 '23 03:01

user1187958


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.

like image 40
Lux Avatar answered Jan 28 '23 02:01

Lux