ralfstuckert / pdfbox-layout

MIT License
156 stars 74 forks source link

pdf.save() in endless loop #84

Open baszero opened 3 years ago

baszero commented 3 years ago

General question regarding pdf.save() method: is there any known issue where this method is looping forever and eating up all the JVM memory until OutOfMemoryException?

rst.pdfbox.layout.elements.Document pdf = ...initialition....;
OutputStream os = ...prepare outputstream for pdf...;
pdf.save(os); // takes forever

My code works fine in 98% of all cases, but under certain conditions, the save command hangs... Any ideas?

baszero commented 3 years ago

I made a thread dump and see this in the case where it hangs:

"http-bio-8443-exec-2" #24 daemon prio=5 os_prio=31 tid=0x00007fd80fcfd800 nid=0x3c07 runnable [0x00007000082e9000]
   java.lang.Thread.State: RUNNABLE
    at java.io.UnixFileSystem.checkAccess(Native Method)
    at java.io.File.canRead(File.java:768)
    at javax.imageio.ImageIO.read(ImageIO.java:1300)
    at rst.pdfbox.layout.elements.ImageElement.<init>(ImageElement.java:44)
    at com.myowncode.helper.PDFHelper$1.afterPage(PDFHelper.java:96)
    at rst.pdfbox.layout.elements.Document.afterPage(Document.java:386)
    at rst.pdfbox.layout.elements.render.RenderContext.closePage(RenderContext.java:414)
    at rst.pdfbox.layout.elements.render.RenderContext.newPage(RenderContext.java:371)
    at rst.pdfbox.layout.elements.render.VerticalLayout.turnPage(VerticalLayout.java:62)
    at rst.pdfbox.layout.elements.render.VerticalLayout.layoutAndDrawReleative(VerticalLayout.java:213)
    at rst.pdfbox.layout.elements.render.VerticalLayout.renderReleative(VerticalLayout.java:148)
    at rst.pdfbox.layout.elements.render.VerticalLayout.render(VerticalLayout.java:96)
    at rst.pdfbox.layout.elements.render.VerticalLayout.render(VerticalLayout.java:79)
    at rst.pdfbox.layout.elements.render.RenderContext.render(RenderContext.java:309)
    at rst.pdfbox.layout.elements.Document.render(Document.java:299)
    at rst.pdfbox.layout.elements.Document.save(Document.java:341)
    at com.myowncode.resource.pdf.PDFGenerateResource.getView(PDFGenerateResource.java:70)
baszero commented 3 years ago

Update: I found the root cause: if an ImageElement is in "portrait orientation" and it is higher than the actual page height, the library hangs forever...

I use this code to insert an image into the pdf, maybe this one is not correct for such extreme portrait images:

    private void addImage(Document pdf, Node imageNode) throws Exception {
        InputStream is = null;
        try {
            is = imageNode.getInputStream();
            ImageElement img = new ImageElement(is);
            img.setWidth(ImageElement.SCALE_TO_RESPECT_WIDTH);
            img.setHeight(ImageElement.SCALE_TO_RESPECT_WIDTH);
            pdf.add(img);

        } finally {
            try {
                is.close();
            } catch (Exception e) {
            } finally {
            }
        }
    }

My assumption was of course that in case an image is higher than the available height of the page, that maxHeight is set and the width would be calculated accordingly automatically (not using the full width of the page...)

Is this a bug? Or what can I do in order to avoid this behaviour?

oytuntez commented 1 year ago

I've seen the same thing with Paragraph.setMaxWidth as well as Image.