Vamsee
Vamsee

Reputation: 183

Auto-generate the Content in PDF format - Java

I'm developing a Java web App which could calculate one's IQ. I want the App to have an option Get Your Certificate at the end. I want a PDF file (A Certificate of appreciation) to be auto generated with the pre-entered name of the User and his IQ Score.

How can one achieve this? I've already seen this type of feature in some websites which provide certifications..

Upvotes: 0

Views: 854

Answers (2)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

Take a look a some iText samples. You can fill out a form, then click "flatten" and you have a PDF containing the data you used. As you're talking about a certificate, the easiest solution would be to create a PDF template using AcroForm technology. For instance: state.pdf is the interactive PDF that was used in the example I just mentioned.

The code used to fill out and flatten this form can be found here. For more examples, please read Chapter 6 of my book "iText in Action" (that chapter is available for free; you need section 6.3.5). I've also written a complete chapter about integrating code like this in a web application. You can find the examples that come with this chapter here.

Basically, you need to do something like this:

PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader,
        new FileOutputStream(dest));
AcroFields fields = stamper.getAcroFields();
fields.setField("name", "CALIFORNIA");
fields.setField("abbr", "CA");
fields.setField("capital", "Sacramento");
fields.setField("city", "Los Angeles");
fields.setField("population", "36,961,664");
fields.setField("surface", "163,707");
fields.setField("timezone1", "PT (UTC-8)");
fields.setField("timezone2", "-");
fields.setField("dst", "YES");
stamper.setFormFlattening(true);
stamper.close();
reader.close();

Caveat regarding the data that is entered: The simple example uses a very basic font that doesn't know how to display special characters. If you need characters such as ñ, é, à, etc... You'll need to introduce a font with more glyphs.

Caveat regarding the -tag you used: I have written this helloworld.jsp that results in this PDF, which proves that is is possible to generate PDF from JSP. Nevertheless, it is a bad idea to do so. When you learned how to write JSP, your teacher probably told you that JSP shouldn't be used to create binary files. (If he didn't tell you this, he either forgot or he wasn't a good teacher.) As there are so many pitfalls when using JSP to create binary files and as a JSP file is eventually compiled to a Servlet anyway, you should forget about creating a JSP to create a PDF and prefer writing a Servlet. It will save you plenty of time and your code will be easier to maintain (the slightest change to your JSP file can break the code).

Upvotes: 0

Aaron Gotreaux
Aaron Gotreaux

Reputation: 273

Java PDF APIs

  • Here is an answer to a similar question referencing a few well-known APIs.
  • Here is a more recent article detailing the licenses for those APIs.
  • Yet another listing of resources.

Flow of control

  1. User clicks a link that generates a request that will be handled by the servlet.
  2. Extract whatever you need from the URL within the servlet.
  3. Use your chosen API to build the content for the PDF using a writer.
  4. Push the PDF to the client.

Upvotes: 1

Related Questions