You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Glen Peterson (JIRA)" <ji...@apache.org> on 2018/06/07 20:26:00 UTC

[jira] [Comment Edited] (PDFBOX-4242) Fontbox does not close file descriptor when loading fonts.

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

Glen Peterson edited comment on PDFBOX-4242 at 6/7/18 8:25 PM:
---------------------------------------------------------------

PDType0Font is *not* closing it. I looked again here:
 [https://github.com/apache/pdfbox/blob/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java#L112]

It's only closed automatically if closeOnSubset=true and embedSubset=false.  They are both true when using the load() method I used.

To close them, you'd probably have to keep a list of open fonts, and close them when the document is closed.  I can make my code do that.  Thanks for talking me through this - it really helped!


was (Author: glen@organicdesign.org):
PDType0Font is *not* closing it. I looked again here:
 [https://github.com/apache/pdfbox/blob/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java#L112]

{{ if (closeOnSubset)}}
{{ {}}
{{    if (embedSubset)}}
{{    {}}
{{        this.ttf = ttf;}}
{{    }}}
{{    else}}
{{    {}}
{{        // the TTF is fully loaded and it is save to close the underlying data source}}
{{        ttf.close();}}
{{    }}}
{{ }}}

So it's only closed automatically if closeOnSubset=true and embedSubset=false.  They are both true when using the load() method I used.

To close them, you'd probably have to keep a list of open fonts, and close them when the document is closed.  I can make my code do that.  Thanks for talking me through this - it really helped!

> Fontbox does not close file descriptor when loading fonts.
> ----------------------------------------------------------
>
>                 Key: PDFBOX-4242
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-4242
>             Project: PDFBox
>          Issue Type: Bug
>          Components: FontBox
>    Affects Versions: 2.0.9
>            Reporter: Glen Peterson
>            Priority: Major
>              Labels: file_leak
>
> My app has been getting "java.io.FileNotFoundException (No file descriptors available)" and I've confirmed that it's because fontbox isn't closing it's file descriptors.
> In org.apache.fontbox.ttf.TTFParser there's this method:
> {{public TrueTypeFont parse(File ttfFile) throws IOException {}}
>  {{  RAFDataStream raf = new RAFDataStream(ttfFile, "r");}}
> {{  try {}}
>  {{    return this.parse((TTFDataStream)raf);}}
>  {{  } catch (IOException var4) {}}
>  {{    // close only on error (file is still being accessed later)}}
>  {{    raf.close();}}
>  {{    throw var4;}}
>  {{}}}
>  {{}}}
> I would have expected to see the close() in a finally block so that the file is always closed, not just on exceptions. Presumably, you can keep it in memory without leaving the file descriptor open?
> {{public TrueTypeFont parse(File ttfFile) throws IOException {}}
>  {{  RAFDataStream raf = new RAFDataStream(ttfFile, "r");}}
> {{  try {}}
>  {{    return this.parse((TTFDataStream)raf);}}
>  {{  } catch (IOException var4) {}}{{    raf.close();}}
>  {{    throw var4;}}
>  {{  } finally {}}
>  {{    raf.close();}}
>  {{}}}
>  {{}}}
> I tried performing this in a lazy initialization, but it blew up:
> java.lang.RuntimeException: java.io.IOException: The TrueType font null does not contain a 'cmap' tableCaused by: java.io.IOException: The TrueType font null does not contain a 'cmap' table
>   at org.apache.fontbox.ttf.TrueTypeFont.getUnicodeCmapImpl(TrueTypeFont.java:548)
>   at org.apache.fontbox.ttf.TrueTypeFont.getUnicodeCmapLookup(TrueTypeFont.java:528)
>   at org.apache.fontbox.ttf.TrueTypeFont.getUnicodeCmapLookup(TrueTypeFont.java:514)
>   at org.apache.fontbox.ttf.TTFSubsetter.<init>(TTFSubsetter.java:91)
>   at org.apache.pdfbox.pdmodel.font.TrueTypeEmbedder.subset(TrueTypeEmbedder.java:321)
>   at org.apache.pdfbox.pdmodel.font.PDType0Font.subset(PDType0Font.java:239)
>   at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1271)
> Thoughts?
> Thanks for PDFBox - it's been really helpful!



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: dev-help@pdfbox.apache.org