I am working on an assignment wherein I need to create an HTML file and convert the HTML file to PDF. I am using Thymeleaf and flying-saucer-pdf to get this done. The Thymeleaf part works fine and creates the HTML file. However, the PDF creation part fails with out-of-memory error. The HTML file that gets created is around 275 MB and has around 8000000 lines.
OutputStream outputStream = new FileOutputStream("outFile.pdf")
String html = new String(Files.readAllBytes(Paths.get("data.html")));
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
// renderer.setDocument(new File("data.html"));
renderer.layout();
renderer.createPDF(outputStream);
renderer.finishPDF();
outputStream.close();
the execution gets stuck in the renderer.layout() for a long time and then fails. I tried both renderer.setDocumentFromString and renderer.setDocument and the results are the same. Also, i tried com.openhtmltopdf for PDF generation from HTML and it also resulted in Out Of Memory error.
String html = new String(Files.readAllBytes(Paths.get("data.html")));
Document document = Jsoup.parse(html, "UTF-8");
document.outputSettings().syntax(Document.OutputSettings.Syntax.xml);
try (OutputStream os = new FileOutputStream("outFile.pdf")) {
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.withUri("outFile.pdf");
builder.toStream(os);
builder.withW3cDocument(new W3CDom().fromJsoup(document), "/");
builder.run();
}
Wondering whether there are any alternate libraries or buffered logic that take care of the memory issue. Any help and pointers are much appriciated.