[ 
https://issues.apache.org/jira/browse/PDFBOX-3272?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15193890#comment-15193890
 ] 

Gregor Ambrozic edited comment on PDFBOX-3272 at 3/14/16 7:16 PM:
------------------------------------------------------------------

I built and tested the trunk version after commit and unfortunately, it didn't 
solve the problem for me.

After some digging around PDFBox code I found the reason.

The missing close is in the "org.apache.fontbox.ttf.TTFParser" class. Method 
"public TrueTypeFont parse(File ttfFile)" should call "close()" on the 
InputStream it creates from the file supplied with "ttfFile" argument. The 
"close" shoud be called right after the actual parsing is done by method 
"TrueTypeFont parse(TTFDataStream raf)".

Also, I found a workaround for now. 

Before: 
{code}PDFont font = PDType0Font.load(doc, new 
File("./pdf/OpenSans-Regular.ttf"));{code}

Instead of passing file to "load" method, I pass "InputStream" which I can 
close after "load" call:
{code}
File fontFile = new File("./pdf/OpenSans-Regular.ttf");
FileInputStream fis = new FileInputStream(fontFile);
PDFont font = PDType0Font.load(doc, fis);
fis.close();
{code}

I hope this helps.


was (Author: [email protected]):
I built and tested the trunk version after commit and unfortunately, it didn't 
solve the problem for me.

After some digging around PDFBox code I found the reason.

The missing close is in the "org.apache.fontbox.ttf.TTFParser" class. Method 
"public TrueTypeFont parse(File ttfFile)" should call "close()" on the 
InputStream it creates from the file supplied with "ttfFile" argument. The 
"close" shoud be called right after the actual parsing is done by method 
"TrueTypeFont parse(TTFDataStream raf)". The same goes for "parse" methods 
which take String and InputStream arguments.

Also, I found a workaround for now. 

Before: 
{code}PDFont font = PDType0Font.load(doc, new 
File("./pdf/OpenSans-Regular.ttf"));{code}

Instead of passing file to "load" method, I pass "InputStream" which I can 
close after "load" call:
{code}
File fontFile = new File("./pdf/OpenSans-Regular.ttf");
FileInputStream fis = new FileInputStream(fontFile);
PDFont font = PDType0Font.load(doc, fis);
fis.close();
{code}

I hope this helps.

> Loaded fonts file descriptors open after closing document
> ---------------------------------------------------------
>
>                 Key: PDFBOX-3272
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-3272
>             Project: PDFBox
>          Issue Type: Bug
>          Components: FontBox
>    Affects Versions: 2.0.0
>         Environment: Apache Tomcat, Linux
>            Reporter: Gregor Ambrozic
>            Assignee: Andreas Lehmkühler
>             Fix For: 2.0.0
>
>         Attachments: OpenSans-Regular.ttf
>
>
> I am experiencing problems with TTF fonts loaded for generating PDFs which 
> eventually result in too many open files on Linux. The PDFBox version I 
> tested last was 2.0.0-RC3.
> Basically for each PDF I create a document and load two fonts which I want to 
> use. After the document is generated I close all the resources, but the file 
> descriptors for both fonts remain open.
> The file descriptors should be automatically closed or an API should exist to 
> close font resources.
> My basic code:
> {code}
> import java.io.File;
> import java.io.IOException;
> import java.lang.management.ManagementFactory;
> import org.apache.commons.io.output.ByteArrayOutputStream;
> import org.apache.pdfbox.pdmodel.PDDocument;
> import org.apache.pdfbox.pdmodel.PDPage;
> import org.apache.pdfbox.pdmodel.PDPageContentStream;
> import org.apache.pdfbox.pdmodel.common.PDRectangle;
> import org.apache.pdfbox.pdmodel.font.PDFont;
> import org.apache.pdfbox.pdmodel.font.PDType0Font;
> public class FontTest
> {
>       // Run the program which will create 1 PDF document and close all 
> resources per second for 100 seconds.
>       // The font open file descriptor count will increase all the time, 
> until the program finishes.
>       // Command to check open files: lsof -p PID | grep ttf
>       public static void main(String[] args)
>       {
>               // should print out PID before @<hostname>
>               System.out.println("process id: " + 
> ManagementFactory.getRuntimeMXBean().getName());
>               for (int i = 0; i < 100; i++)
>               {
>                       createPDF();
>                       try
>                       {
>                               Thread.sleep(1000);
>                       }
>                       catch (InterruptedException e)
>                       {
>                               e.printStackTrace();
>                       }
>               }
>       }
>       private static void createPDF()
>       {
>               PDDocument doc = null;
>               PDPage page = null;
>               ByteArrayOutputStream bos = null;
>               try
>               {
>                       doc = new PDDocument();
>                       page = new PDPage(PDRectangle.A4);
>                       doc.addPage(page);
>                       // using standard font
>                       PDFont font = PDType0Font.load(doc, new 
> File("./pdf/OpenSans-Regular.ttf"));
>                       PDPageContentStream content = new 
> PDPageContentStream(doc, page);
>                       content.beginText();
>                       content.setFont(font, 72);
>                       content.showText("OMG");
>                       content.endText();
>                       content.close();
>                       bos = new ByteArrayOutputStream();
>                       doc.save(bos);
>                       byte[] bytes = bos.toByteArray();
>                       System.out.println("create new pdf with size: " + 
> bytes.length);
>               }
>               catch (Exception e)
>               {
>                       e.printStackTrace();
>               }
>               finally
>               {
>                       try
>                       {
>                               doc.close();
>                               bos.close();
>                       }
>                       catch (IOException e)
>                       {
>                               e.printStackTrace();
>                       }
>               }
>       }
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to