Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating complex pdf using java

I have an Java/Java EE based application wherein I have a requirement to create PDF certificates for various services that will be provided to the users. I am looking for a way to create PDF (no need for digital certificates for now).

What is the easiest and convenient way of doing that? I have tried

  1. XSL to pdf conversion
  2. HTML to PDF conversion using itext.
  3. crude java way (using PDFWriter, PdfPCell etc.)

What is the best way out of these or is there any other way which is easier and convenient?

like image 656
Ankit Avatar asked Jan 04 '13 05:01

Ankit


4 Answers

When you talk about Certificates, I think of standard sheets that look identical for every receiver of the certificate, except for:

  • the name of the receiver
  • the course that was followed by the receiver
  • a date

If this is the case, I would use any tool that allows you to create a fancy certificate (Acrobat, Open Office, Adobe InDesign,...) and create a static form (sometimes referred to as an AcroForm) containing three fields: name, course, date.

I would then use iText to fill in the fields like this:

PdfReader reader = new PdfReader(pathToCertificateTemplate);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(pathToCertificate));
AcroFields form = stamper.getAcroFields();
form.setField("name", name);
form.setField("course", course);
form.setField("date", date);
stamper.setFormFlattening(true);
stamper.close();
reader.close();

Creating such a certificate from code is "the hard way"; creating such a certificate from XML is "a pain" (because XML isn't well-suited for defining a layout), creating a certificate from (HTML + CSS) is possible with iText's XML Worker, but all of these solutions have the disadvantage that it's hard work to position every item correctly, to make sure everything fits on the same page, etc...

It's much easier to maintain a template with fixed fields. This way, you only have to code once. If for some reason you want to move the fields to another place, you only have to change the template, you don't have to worry about messing around in code, XML, HTML or CSS.

See http://www.manning.com/lowagie2/samplechapter6.pdf for some more info (section 6.3.5).

like image 163
Bruno Lowagie Avatar answered Nov 13 '22 02:11

Bruno Lowagie


Try using Jasper Reports mate. Check it out at http://community.jaspersoft.com/

like image 32
medina Avatar answered Nov 13 '22 02:11

medina


I recommend the first method: XSL to pdf conversion, which is the most powerful. I have experience to produce a lot of PDF reports(each having thousands of pages) gracefully by use of Apache FOP, I think it's good enough and fairly easy(but it requires some knowledge of xsl-FO).

like image 1
Hui Zheng Avatar answered Nov 13 '22 01:11

Hui Zheng


Even though, this is old question, I think it should be anwered.

To create very complex pdf such as certificates,reports or payment slips etc. You can definitely use Dynamic Reports library. This library is dependent on jasper reports (This is also very popular and old library). Dynamic reports will provide you to design your documents using java code so that you can easily manipulate or make changes as required.

There are lots of examples available there at their site and very easy to learn from those examples.

Below is link for it :

http://www.dynamicreports.org/

like image 1
Vivek Avatar answered Nov 13 '22 01:11

Vivek