Hello,

I'm trying to create a thumbail of a PDF and I'm getting a bunch of NPE 
stacktraces when I call PDPage.convertToImage. This only occurs with certain 
PDFs. The NPE is being thrown from TTFSubFont.addCharCode. The NPEs are being 
logged (99 separate but identical stacktraces), but not passed up to my calling 
code. The resulting image is blank (all white). This makes it difficult for the 
calling code to detect the error condition. Is this a known problem? Is there a 
work-around so the calling code can detect the error?


Let me know if you want the actual PDF, and the preferred way to post it to the 
list.


This is how I'm calling the code:


private byte[] generatePdfThumbnail(PDDocument pdfDocument) throws IOException {
    PDPage page = (PDPage) pdfDocument.getDocumentCatalog()
            .getAllPages()
            .get(0);

    BufferedImage image = page.convertToImage(BufferedImage.TYPE_INT_RGB, 
RESOLUTION_DPI);
    int largestDimension = Math.max(image.getHeight(), image.getWidth());
    float scalingFactor = IMAGE_HEIGHTWIDTH / largestDimension;
    int scaledHeight = (int) (image.getHeight() * scalingFactor);
    int scaledWidth = (int) (image.getWidth() * scalingFactor);

    BufferedImage scaledImage = new BufferedImage(scaledWidth,
            scaledHeight,
            BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = scaledImage.createGraphics();
    graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics.drawImage(image, 0, 0, scaledWidth, scaledHeight, null);
    graphics.dispose();

    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        ImageIOUtil.writeImage(scaledImage,
                FORMAT_NAME,
                outputStream,
                RESOLUTION_DPI,
                IMAGE_QUALITY);
        return outputStream.toByteArray();
    }
}

Here is the stacktrace I'm seeing:


Mar 15, 2016 1:48:12 PM org.apache.pdfbox.util.PDFStreamEngine processOperator
WARNING: java.lang.NullPointerException
java.lang.NullPointerException
        at org.apache.fontbox.ttf.TTFSubFont.addCharCode(TTFSubFont.java:106)
        at org.apache.fontbox.ttf.TTFSubFont.<init>(TTFSubFont.java:95)
        at 
org.apache.pdfbox.pdmodel.font.PDTrueTypeFont.rebuildTTF(PDTrueTypeFont.java:550)
        at 
org.apache.pdfbox.pdmodel.font.PDTrueTypeFont.getawtFont(PDTrueTypeFont.java:478)
        at 
org.apache.pdfbox.pdmodel.font.PDSimpleFont.drawString(PDSimpleFont.java:110)
        at 
org.apache.pdfbox.pdfviewer.PageDrawer.processTextPosition(PageDrawer.java:260)
        at 
org.apache.pdfbox.util.PDFStreamEngine.processEncodedText(PDFStreamEngine.java:504)
        at org.apache.pdfbox.util.operator.ShowText.process(ShowText.java:56)
        at 
org.apache.pdfbox.util.PDFStreamEngine.processOperator(PDFStreamEngine.java:562)
        at 
org.apache.pdfbox.util.PDFStreamEngine.processSubStream(PDFStreamEngine.java:269)
        at 
org.apache.pdfbox.util.PDFStreamEngine.processSubStream(PDFStreamEngine.java:236)
        at 
org.apache.pdfbox.util.PDFStreamEngine.processStream(PDFStreamEngine.java:216)
        at org.apache.pdfbox.pdfviewer.PageDrawer.drawPage(PageDrawer.java:139)
        at org.apache.pdfbox.pdmodel.PDPage.convertToImage(PDPage.java:801)
        at 
ddf.catalog.transformer.input.pdf.PdfInputTransformer.generatePdfThumbnail(PdfInputTransformer.java:170)
        at 
ddf.catalog.transformer.input.pdf.PdfInputTransformer.generatePdfThumbnail(PdfInputTransformer.java:162)
        ...


Thanks in advance,

Glen




Reply via email to