Ankit
Ankit

Reputation: 3183

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?

Upvotes: 5

Views: 32194

Answers (7)

phe
phe

Reputation: 349

Bruno Lowagie pointed out a great way to generate a Template which is the same basically for all data and needs to be populated. However, Bruno Lowagie recommends iText as library to populate the fields. For me like for Ankit, this license was an issue why I had to choose another library. In the following I have a step-by-step guide how to create a template and populate it with data using Apaches PdfBox

    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox</artifactId>
        <version>2.0.16</version>
    </dependency>
  1. Create a Template with LibreOffice Writer. For placeholders use TextBoxes (View >> Toolbars >> Form Controls ). This will create a PDF with AcroForms as suggested by Bruno Lowagie

  2. Set a name for each Textbox. Set read-only to true.

  3. Save the document as PDF.

  4. Read the PDF-Template with PdfBox and set the values for the textboxes.

    InputStream is = getClass().getClassLoader().getResourceAsStream("Template.pdf");
    
    try {
        PDDocument pDDocument = PDDocument.load(is);
        PDAcroForm pDAcroForm = pDDocument.getDocumentCatalog().getAcroForm();
    
        PDField fieldName = pDAcroForm.getField("name");
        fieldName.setValue("FirstName Surname"); // <-- Replacement
    
        pDDocument.save(outStream);
        pDDocument.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    

Upvotes: 1

Vivek
Vivek

Reputation: 66

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/

Upvotes: 1

Paul Jowett
Paul Jowett

Reputation: 6581

You mentioned the PDFs can be complex. If this is to do with variability or layout, one option that provides reasonably sophisticated template-based layouts and controls is Docmosis. You provide Docmosis with doc or odt files as templates so they are very easy to change and the call Docmosis to mail-merge to create the pdf or other formats. Please not I work for the company that created Docmosis.

Hope that helps.

Upvotes: 0

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

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).

Upvotes: 11

Hui Zheng
Hui Zheng

Reputation: 10224

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).

Upvotes: 1

medina
medina

Reputation: 8159

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

Upvotes: 1

user1249380
user1249380

Reputation:

Use iText pdf library for creating the pdf's It will be easy for you to generate pdfs from that api. Here is the link

http://itextpdf.com/

Text ® is a library that allows you to create and manipulate PDF documents. It enables developers looking to enhance web- and other applications with dynamic PDF document generation and/or manipulation.

Developers can use iText to:

Serve PDF to a browser

Generate dynamic documents from XML files or databases

Use PDF's many interactive features

Add bookmarks, page numbers, watermarks, etc.

Split, concatenate, and manipulate PDF pages

Automate filling out of PDF forms

Add digital signatures to a PDF file

Upvotes: 0

Related Questions