Creating complex pdf using java

2019-02-23 09:10发布

问题:

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?

回答1:

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



回答2:

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



回答3:

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



回答4:

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/



回答5:

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



回答6:

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.