Generate PDF files using iText and apache velocity template(.vm)
Asked Answered
P

3

5

What is the general workflow to generate a PDF using iText and an Apache Velocity template file (.vm) in Java?

I am interested in knowing steps like: parse template file, put Java object in context and steps to be performed to generate pdf etc.

I know this is a very basic question. But I am not able to find even a single example of this type on the web. I found XDocReport, but I am interested to know other alternatives as well.

Please help me with some sample project link or at least the steps to get started.

Pantoja answered 18/2, 2014 at 12:26 Comment(3)
What is your limitation with XDocReport?Hippodrome
Is it possible to set edit password for pdf files in xdcoreport? I am referring to PDFWriter.setEncryption method in iText.Pantoja
Hi @Angelo, added the question here: #21872387. Please take a look.Pantoja
H
5

Yes, you can. It all depends on how complex you want the PDFs to be.

Here are the steps for basic functionality

  1. Generate a HTML file using Apache Velocity template file (.vm).
  2. Use com.itextpdf.text.html.simpleparser.HTMLWorker (deprecated) to parse/convert that HTML file into a PDF.
  3. Additionally, you can use com.itextpdf.text.pdf.PdfCopy.PageStamp to add content (borders, stamps, notes, annotations etc) to an existing PDF.

There is also com.itextpdf.tool.xml.XMLWorker for more advanced HTML conversion (adding style sheets etc)

Highpriced answered 19/2, 2014 at 18:5 Comment(0)
H
4

Generating PDF using iText and an Apache Velocity template file (.vm) in Java directly is not possible because:

  • PDF is binary format,
  • Velocity generates plain text content.

On other words, Velocity cannot generate PDF.

XDocReport is able to generate a docx/odt report by merging a docx/odt template which contains some Velocity/Freemarker syntax with Java context. The generated docx/odt report can be convert it to pdf/xhtml.

It works because docx/odt are a zip which contains several xml entries. If you unzip a docx you will see word/document.xml. In this entry, you will see the content that you have typed with MS Word. word/document.xml is a plain text, so Velocity can be used in this case.

Here the XDocReport process to generate pdf from a docx template which uses Velocity:

  1. Load docx template. this step consist to unzip the docx and stores in a map each xml entries (name entry as key and byte array as value). For instance map contains a key with word/document.xml and the xml content of this entry as value.
  2. Loop for each xml entries which must be merged with Java context. For instance word/document.xml is merged with Java context by using Velocity and the result of merge replace the word/document.xml value of the map
  3. Rebuild a new docx by zipping each entries of the map.

At this step we have a generated docx (the report).

To convert it to another format, XDocReport provides a docx-to-pdf converter based on Apache POI and iText. Here the XDocReport process to convert a docx to pdf:

  1. Load docx with Apache POI
  2. Loop for each structures of POI (XWPFParagraph, etc.) to create iText structure (iText Paragraph).

Note that XDocReport is modular and you can use other converters as well.

Hippodrome answered 18/2, 2014 at 21:55 Comment(0)
P
0

At first,we use freemarker template to generate a html file,and then render html to a pdf file by IItextRender .Finally, we can view pdf file in browser,there has a very useful javascript tools called pdfjs. Maybe you can try it.

Prescript answered 19/9, 2017 at 8:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.