SKR
SKR

Reputation: 803

Create a PDF From a JSP Output

I have a webpage with a export option to PDF. I have to display the contents of the page in the PDF. Currently I use iText PDF Library to generate PDFs. The problem is creating PDF with iText is quite a challenge. Moreover we get frequent layout/UI changes for the webpage, so we have make the same changes to PDF.

Is there any way i can convert my JSP output to PDF. Like for example "if we set the content type to contentType="application/vnd.ms-excel", a JSP table can be rendered as Excel document.

Upvotes: 3

Views: 16950

Answers (3)

BalusC
BalusC

Reputation: 1108722

You don't need to change the iText code generation if you use it in combination with Flying Saucer (a.k.a. XhtmlRenderer). It's then basically as simple as:

String inputPath = new File("/file.xhtml").toURI().toURL().toString();
OutputStream outputStream = new FileOutputStream("/file.pdf");

ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(inputPath);
renderer.layout();
renderer.createPDF(outputStream);
outputStream.close();

You can find a blog with more code samples here.

Upvotes: 2

Marko
Marko

Reputation: 5552

You should check wkhtmltopdf.

Upvotes: 0

Script Runner
Script Runner

Reputation: 282

Have you checked Jasper Reports ? It has the concept of XML templates. Also same template can be used to generate Word / XLS / PDF/ CSV / XML output.

Upvotes: 3

Related Questions