0
votes

How do I render an HTML template as a PDF in the play framework using itext without using the pdf module since it is unsupported?

1

1 Answers

1
votes

You can convert a HTML template pretty much in the same way as in a regular Java project. First add the dependencies to your build.sbt:

libraryDependencies += "com.itextpdf" % "itextpdf" % "5.4.2"
libraryDependencies += "com.itextpdf.tool" % "xmlworker" % "5.4.1"

Then, on a controller method:

// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("pdf.pdf"));
// step 3
document.open();
// step 4
XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new ByteArrayInputStream(views.html.index.render().body().getBytes()));

//step 5
document.close();

System.out.println( "PDF Created!" );