Uploaded image for project: 'PDFBox'
  1. PDFBox
  2. PDFBOX-5781

OutOfMemoryError in FileSystemFontsProvider.scanFonts

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.0.30, 3.0.1 PDFBox
    • 2.0.31, 3.0.2 PDFBox, 4.0.0
    • FontBox
    • None

    Description

      We experienced an OutOfMemoryError when calling
      PDAcroForm.getDefaultResources().getFont(COSName); with COSName{Helv}

      The reason seemed to be that PdfBox initializes a FontCache when getFont is called and this scans all fonts on the system. This also loads some large system fonts (AppleColorEmoji is 189,9MB). Each font gets copied into a single large byte array at the location below and this causes an OutOfMemoryError at this point in the code.

      org.apache.pdfbox.pdmodel.font.FileSystemFontProvider#addTrueTypeFontImpl:773

      InputStream is = ttf.getOriginalData();
      byte[] ba = IOUtils.toByteArray(is);
      is.close();
      String hash = computeHash(ba); 

      I think this would be easily fixed by using a DigestInputStream instead of a byte array to compute hashes at this location. I have tested this locally and it seemed to work. Please see the attached .patch file

      Attachments

        1. FileSystemFontProvider_use_DigestInputStream.patch
          4 kB
          Kim Hage
        2. FileSystemFontProvider_OutOfMemoryError.stacktrace
          3 kB
          Kim Hage
        3. 3.0.patch
          3 kB
          Kim Hage
        4. 2.0.31.patch
          3 kB
          Kim Hage

        Activity

          People

            tilman Tilman Hausherr
            kim_hage Kim Hage
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: