PDFBox: Disable Font Cache or change its location
<p>When I call <code>PDField.setValue</code> to set the value for a form field, I get the following stacktrace:</p>

<pre><code>FileSystemFontProvider.saveDiskCache(349) | Could not write to font cache
java.io.FileNotFoundException: /.pdfbox.cache (Permission denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:194)
at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:145)
at java.io.FileWriter.&lt;init&gt;(FileWriter.java:73)
at org.apache.pdfbox.pdmodel.font.FileSystemFontProvider.saveDiskCache(FileSystemFontProvider.java:290)
at org.apache.pdfbox.pdmodel.font.FileSystemFontProvider.&lt;init&gt;(FileSystemFontProvider.java:226)
at org.apache.pdfbox.pdmodel.font.FontMapperImpl$DefaultFontProvider.&lt;clinit&gt;(FontMapperImpl.java:130)
at org.apache.pdfbox.pdmodel.font.FontMapperImpl.getProvider(FontMapperImpl.java:149)
at org.apache.pdfbox.pdmodel.font.FontMapperImpl.findFont(FontMapperImpl.java:413)
at org.apache.pdfbox.pdmodel.font.FontMapperImpl.findFontBoxFont(FontMapperImpl.java:376)
at org.apache.pdfbox.pdmodel.font.FontMapperImpl.getFontBoxFont(FontMapperImpl.java:350)
at org.apache.pdfbox.pdmodel.font.PDType1Font.&lt;init&gt;(PDType1Font.java:145)
at org.apache.pdfbox.pdmodel.font.PDType1Font.&lt;clinit&gt;(PDType1Font.java:79)
at org.apache.pdfbox.pdmodel.font.PDFontFactory.createFont(PDFontFactory.java:62)
at org.apache.pdfbox.pdmodel.PDResources.getFont(PDResources.java:143)
at org.apache.pdfbox.pdmodel.interactive.form.PDDefaultAppearanceString.processSetFont(PDDefaultAppearanceString.java:164)
at org.apache.pdfbox.pdmodel.interactive.form.PDDefaultAppearanceString.processOperator(PDDefaultAppearanceString.java:131)
at org.apache.pdfbox.pdmodel.interactive.form.PDDefaultAppearanceString.processAppearanceStringOperators(PDDefaultAppearanceString.java:107)
at org.apache.pdfbox.pdmodel.interactive.form.PDDefaultAppearanceString.&lt;init&gt;(PDDefaultAppearanceString.java:85)
at org.apache.pdfbox.pdmodel.interactive.form.PDVariableText.getDefaultAppearanceString(PDVariableText.java:93)
at org.apache.pdfbox.pdmodel.interactive.form.AppearanceGeneratorHelper.&lt;init&gt;(AppearanceGeneratorHelper.java:94)
at org.apache.pdfbox.pdmodel.interactive.form.PDTextField.constructAppearances(PDTextField.java:262)
at org.apache.pdfbox.pdmodel.interactive.form.PDTerminalField.applyChange(PDTerminalField.java:228)
at org.apache.pdfbox.pdmodel.interactive.form.PDTextField.setValue(PDTextField.java:218)
</code></pre>

<p>I am running PDFBox 2.0.4 which is the newest version. My webserver most likely does not have access to write to <code>.pdfbox.cache</code> in the default location (which seems to be the JVM property <code>user.home</code>). Is there any way to disable the disk caching or change the location of the cache file?</p>

<p>I did notice that I can set a JVM wide system property called <code>pdfbox.fontcache</code>, but my webapp shares a jvm with other applications so this isn't an optimal solution. I also tried using that solution and setting the <code>pdfbox.fontcache</code> to <code>/tmp</code>, but it didn't actually create a file (although it now only throws the stacktrace once per boot).</p>

<p>I looked into the code in the <a href="https://svn.apache.org/repos/asf/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/FileSystemFontProvider.java" rel="nofollow noreferrer"><code>FileSystemFontProvider</code></a> and the problematic code seems to be in the <a href="https://github.com/apache/pdfbox/blob/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/FileSystemFontProvider.java#L306" rel="nofollow noreferrer"><code>saveDiskCache</code></a> method. In that method, it first tries to write the file, but a FileNotFoundException is thrown instead of a SecurityException. FileNotFoundException inherits from IOException.</p>

<pre><code>File file = getDiskCacheFile();
try
{
     writer = new BufferedWriter(new FileWriter(file));
}
catch (SecurityException e)
{
     return;
}
</code></pre>