flyingsaucerproject / flyingsaucer

XML/XHTML and CSS 2.1 renderer in pure Java
Other
1.98k stars 557 forks source link

disable print causes nullpointer #375

Open mahmutcanprehcm opened 2 weeks ago

mahmutcanprehcm commented 2 weeks ago

i have an html page which can be dynamically grow. i want to get one pdf page.

This code causes NullPointerException:

         ITextRenderer iTextRenderer = new ITextRenderer();
         iTextRenderer.setDocumentFromString(readHtmlFileAsString("foo.html"));
         SharedContext sharedContext = iTextRenderer.getSharedContext();
         sharedContext.setPrint(false);
         iTextRenderer.layout();

ITextRendere.java:

        public void createPDF(OutputStream os, boolean finish, int initialPageNo) throws DocumentException {
        List<PageBox> pages = _root.getLayer().getPages();
        RenderingContext c = newRenderingContext();
        c.setInitialPageNo(initialPageNo);
        PageBox firstPage = pages.get(0);
        ....

it this a bug, or wrong usage?

asolntsev commented 2 weeks ago

@mahmutcanprehcm Please show the NullPointerException itself with full stacktrace.

mahmutcanprehcm commented 1 week ago
java.lang.NullPointerException
    at org.xhtmlrenderer.layout.Layer.layoutPages(Layer.java:1066)
    at org.xhtmlrenderer.pdf.ITextRenderer.layout(ITextRenderer.java:232)
    at de.printer.service.pdf.DefaultTemplateBuilderService.xhtmlToPdf(DefaultTemplateBuilderService.java:72)
    at de.printer.service.pdf.DefaultTemplateBuilderService.dtoToByte(DefaultTemplateBuilderService.java:33)
mahmutcanprehcm commented 1 week ago

Created by Apache Maven 3.3.9

version=9.1.11 groupId=org.xhtmlrenderer artifactId=flying-saucer-core

asolntsev commented 1 week ago

@mahmutcanprehcm But version 9.1.11 is very old. Please try with the latest version 9.9.1.

mahmutcanprehcm commented 1 week ago

the same with 9.9.1

java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:100)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
    at java.base/java.util.Objects.checkIndex(Objects.java:385)
    at java.base/java.util.ArrayList.get(ArrayList.java:427)
    at org.xhtmlrenderer.pdf.ITextRenderer.createPDF(ITextRenderer.java:372)
    at org.xhtmlrenderer.pdf.ITextRenderer.createPDF(ITextRenderer.java:328)
    at com.solutionfactory.HtmlToPdfOpenPDF.main(HtmlToPdfOpenPDF.java:32)

"The problem occurs when you call setPrint(false) on SharedContext."

ITextRenderer.java Line 372

  RenderingContext c = newRenderingContext();
        c.setInitialPageNo(initialPageNo);
        PageBox firstPage = pages.get(0);
        com.lowagie.text.Rectangle firstPageSize = new com.lowagie.text.Rectangle(0, 0, firstPage.getWidth(c) / _dotsPerPoint,
                firstPage.getHeight(c) / _dotsPerPoint);
asolntsev commented 1 week ago

@mahmutcanprehcm I would say, it's rather a wrong usage.

Why do you call setPrint(false)?

When you convert a web page to PDF, it's usually intended for printing. It means setPrint(true) would be reasonable, and it is the default value.

mahmutcanprehcm commented 1 week ago

I need a single page. It is an invoice that goes to a thermal printer and if the customer wants to download a PDF, he should not receive several pages.

asolntsev commented 1 week ago

Ok, but how setPrint(false) is related to single/multiple pages? It dictates "media: print" or "media: screen". It doesn't affect number of pages.

mahmutcanprehcm commented 1 week ago

OK. Unfortunately I couldn't find any documentation about this method.

How can I then ensure that everything only ends up on one page?

pbrant commented 1 week ago

I'm not sure I entirely understand your use case, but one way to effectively disable pagination would be to set a page height of e.g. 500 cm.

On Wed, Sep 4, 2024, 10:11 mahmutcanprehcm @.***> wrote:

OK. Unfortunately I couldn't find any documentation about this method.

How can I then ensure that everything only ends up on one page?

— Reply to this email directly, view it on GitHub https://github.com/flyingsaucerproject/flyingsaucer/issues/375#issuecomment-2328200794, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAF42F4XTEURSDIWJE2ZKTZU26DBAVCNFSM6AAAAABNMN4NTGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMRYGIYDANZZGQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

mahmutcanprehcm commented 1 week ago

Bildschirmfoto vom 2024-09-04 13-34-59

this produces always a large pdf file