5

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?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Ankit
  • 3,083
  • 7
  • 35
  • 59
  • For HTML to PDF you should try flying saucer: http://code.google.com/p/flying-saucer/ – user1411778 Jan 04 '13 at 05:14
  • 1
    The *easiest and convenient way* depends on how flexible you want to be. If those "certificates" should be allowed to contain anything available in PDF, you will have to go the *crude Java way*, be it with iText as hinted at by the class names you cite or with another decent PDF library. If the "certificates" are simple text with minor bells and whistles, a scripting solution might be more appropriate. XSL or JasperReport both have their merits. HTML->PDF in my opinion would be sensible only if you cannot help but get templates for the certificates in HTMLn format. – mkl Jan 04 '13 at 09:01

7 Answers7

11

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

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • That is a nice option. Thanks a lot! – Ankit Jan 11 '13 at 12:30
  • The option suggested by you is nice, but I am not able to find a open source way of creating a PDF :(. I am trying to convert html -> PDF but however there are some issues. The images in the HTML page is not shown correctly in the pdf. The images dimensions are changed in the PDF, is there any way to correct it? – Ankit Jan 15 '13 at 03:36
  • Why using HTML to PDF? Why not open the HTML in Open Office / Libre Office and tune it exactly the way you want to create the PDF? HTML isn't well suited as a format to create a design. – Bruno Lowagie Jan 15 '13 at 07:53
  • html to pdf because this html is dynamic (html from jsp page). – Ankit Jan 15 '13 at 08:03
  • 2
    In that case, I don't understand the question: either you use AcroForms, or you use dynamic stuff. – Bruno Lowagie Jan 15 '13 at 09:05
  • I have a similar task as the original post. But instead of simple text fields I have graphs and charts that I need to insert. Any tips on this? – eggie5 May 25 '13 at 03:26
  • @eggie5 In that case I would use a push button as placeholder. See http://itextpdf.com/examples/iia.php?id=156 – Bruno Lowagie May 25 '13 at 07:48
  • @BrunoLowagie So to do this templating in the PDFs it seems like it relies on AcroForms fields. In this example you describe the PushButton field. The PDF i'm getting is from a graphic designer, and is created in Adobe Illustrator. I wonder if it's possible to do the AcroFields in illustrator... – eggie5 May 25 '13 at 16:31
  • I had to generate PDF and just thought about something similar, however, I'm a PDF n00b so I didn't know this "AcroForm" name. First Google search pointed me to your great answer, so thanks :D I just wanted to add that IF someone decided to get the hard way somehow, PDFBox not only allows you to fill pre-existing forms, but also to generate a complete PDF, although, as the OP said it is allways painfull to generate a rich format with code. As a rule of thumb, the code-generated documents are only worth it if the format doesn't support forms or if there are so many different layouts. – DGoiko Feb 18 '19 at 17:25
1

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

medina
  • 8,051
  • 4
  • 25
  • 24
1

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

Hui Zheng
  • 10,084
  • 2
  • 35
  • 40
  • I also found conversion of XSL to pdf smooth but I find creation of XSL as a cumbersome task, is there a easier way out to create xsl or it has to be created manually only? – Ankit Jan 04 '13 at 05:27
  • XSL is a little cumbersome, but once you get it done, you can reuse it over and over again. Besides, you may pick up some good editor(e.g. XMLSpy) to edit XSL/XML files, which may eliminate a lot of manual typing. – Hui Zheng Jan 04 '13 at 05:31
1

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/

Vivek
  • 56
  • 2
  • 10
  • this looks similar to Crystal Reports. Dynamic Reports is dead, though, you can read about it here https://www.reddit.com/r/java/comments/918qq4/what_happened_to_dynamicreports_or_what_are_the/ – DGoiko Feb 18 '19 at 17:27
1

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();
    }
    
phe
  • 169
  • 1
  • 8
0

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

  • I think this is what @Ankit called the *crude java way (using PDFWriter, PdfPCell etc.)*. ;) – mkl Jan 04 '13 at 08:52
0

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.

Paul Jowett
  • 6,513
  • 2
  • 24
  • 19