Disable font cache

When I call PDField.setValue to set the value for the form field, I get the following stack:

 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.<init>(FileOutputStream.java:194) at java.io.FileOutputStream.<init>(FileOutputStream.java:145) at java.io.FileWriter.<init>(FileWriter.java:73) at org.apache.pdfbox.pdmodel.font.FileSystemFontProvider.saveDiskCache(FileSystemFontProvider.java:290) at org.apache.pdfbox.pdmodel.font.FileSystemFontProvider.<init>(FileSystemFontProvider.java:226) at org.apache.pdfbox.pdmodel.font.FontMapperImpl$DefaultFontProvider.<clinit>(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.<init>(PDType1Font.java:145) at org.apache.pdfbox.pdmodel.font.PDType1Font.<clinit>(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.<init>(PDDefaultAppearanceString.java:85) at org.apache.pdfbox.pdmodel.interactive.form.PDVariableText.getDefaultAppearanceString(PDVariableText.java:93) at org.apache.pdfbox.pdmodel.interactive.form.AppearanceGeneratorHelper.<init>(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) 

I am launching PDFBox 2.0.4, which is the newest version. My web server most likely does not have write access to .pdfbox.cache in the default location (which seems to be a JVM property of user.home ). Is there a way to disable disk caching or change the location of the cache file?

I noticed that I can set the JVM wide system property called pdfbox.fontcache , but my webapp uses jvm with other applications, so this is not an optimal solution. I also tried using this solution and installed pdfbox.fontcache in /tmp , but actually did not create the file (although now it only throws the stack once per download).

I looked at the code in FileSystemFontProvider , and the problematic code is apparently in saveDiskCache . In this method, it first tries to write a file, but instead of a SecurityException, a FileNotFoundException is thrown. FileNotFoundException inherits from IOException.

 File file = getDiskCacheFile(); try { writer = new BufferedWriter(new FileWriter(file)); } catch (SecurityException e) { return; } 
+5
source share
1 answer

When you install pdfbox.fontcache temporary folder like /tmp , where your JVM can write a new file inside, then the .pdfbox.cache cache .pdfbox.cache can be created when creating PDF using PDFBox (I also use PDFBox 2.0.4).

Perhaps your JVM cannot create a new file inside your /tmp ? To verify this, try creating a new file with the user launching your JVM using the interactive command line (shell).

With the ls -lA /tmp you should see the .pdfbox.cache file in the temporary folder that you are setting up (for example, with the JVM and the tomcat user):

-rw-r - r-- 1 tomcat tomcat 2050 Dec 29 16: 13.pdfbox.cache

This is not an optimal solution because you cannot set several pdfbox.fontcache system properties on a single JVM.

+3
source

Source: https://habr.com/ru/post/1261789/


All Articles