danfickle / openhtmltopdf

An HTML to PDF library for the JVM. Based on Flying Saucer and Apache PDF-BOX 2. With SVG image support. Now also with accessible PDF support (WCAG, Section 508, PDF/UA)!
https://danfickle.github.io/pdf-templates/index.html
Other
1.93k stars 359 forks source link

I'm gettting Caused by: java.io.IOException: Error: End-of-File, expected line #931

Closed erickjhorman closed 11 months ago

erickjhorman commented 1 year ago

Hey When I'm trying to generate a pdf and put together with another one I'm getting this error "Caused by: java.io.IOException: Error: End-of-File, expected line"

This is my code

String content = this.writer.toString();

                Document parsedDocument = Jsoup.parse(content);
                parsedDocument.outputSettings()
                        .syntax(Document.OutputSettings.Syntax.xml);

                PdfRendererBuilder builder = new PdfRendererBuilder();
                builder.toStream(baos);
                builder.withW3cDocument(new W3CDom().fromJsoup(parsedDocument), StringUtils.EMPTY);
                builder.run();

                byte[] pdfContent = baos.toByteArray();
                PDDocument document = PDDocument.load(pdfContent);
                PDDocumentInformation pdDocumentInformation = document.getDocumentInformation();
                document.setDocumentInformation(pdDocumentInformation);
                document.save(baos);
                document.close();

                logger.info("PDF generated successfully");
                logger.info(filePdf.getPath());

Anyone knows what happened?

Zorobay commented 1 year ago

I've gotten a few of these too and they were almost always because I was reusing Streams. I don't think you can write to baos with you PdfRendererBuilder and then save your document to baos again. Try instantiating a new output stream for document.save(new_output_stream).

erickjhorman commented 11 months ago

Thanks i finally solved it.