Create complex pdf using java

I have a Java / Java EE-based application in which I have a requirement to create PDF certificates for various services that will be provided to users. I am looking for a way to create a PDF (now there is no need for digital certificates).

What is the easiest and most convenient way to do this? I tried

  • Convert XSL to PDF
  • Convert HTML to PDF using itext.
  • raw java way (using PDFWriter, PdfPCell, etc.).

What is the best way out of this, or is there another way that is simpler and more convenient?

+4
source share
6 answers

When you talk about certificates, I think of standard sheets that look the same for each certificate recipient, with the exception of:

  • receiver name
  • course followed by the receiver
  • the date

If so, I would use any tool that allows you to create a fancy certificate (Acrobat, Open Office, Adobe InDesign, ...) and create a static form (sometimes called AcroForm) containing three fields: name, course, date.

Then I used iText to populate the fields as follows:

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 a "hard way"; creating such a certificate from XML is a "pain" (because XML is not suitable for defining a layout), creating a document from (HTML + CSS) is possible using iText XML Worker, but all these solutions have the disadvantage that it works hard to correctly position each element, so that everything is on one page, etc ....

It is much easier to maintain a fixed margin template. This way you only need to code the code once. If for some reason you want to move the fields to another place, you only need to change the template, you do not need to worry about messing around in the code, XML, HTML or CSS.

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

+6
source

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

+1
source

I recommend the first method: converting XSL to PDF, which is the most powerful. I have experience in creating a large number of reports in PDF format (each of which contains thousands of pages) gracefully using Apache FOP , I think it is good enough and fair (but it requires some knowledge of xsl-FO ).

+1
source

Use the iText pdf library to create pdf. It will be easy for you to create pdf files from this api. Here's a link

http://itextpdf.com/

Text ® is a library that allows you to create and process PDF documents. This allows developers to search for improved web and other applications with the dynamic creation and / or manipulation of PDF documents.

Developers can use iText to:

Submit PDF to browser

Create dynamic documents from XML files or databases

Using pdf of many interactive features

Add bookmarks, page numbers, watermarks, etc.

Split, merge and manage PDF pages

PDF Form Automation

Add digital signatures to a PDF file

0
source

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

Hope this helps.

0
source

Although this is an old question, I think it should be adopted.

To create a very complex PDF file, such as certificates, reports or payrolls, etc. You can definitely use the dynamic reporting library. This library is dependent on jasper reports (it is also a very popular and old library). Dynamic reports let you design your documents using Java code so you can easily manipulate or make changes as needed.

There are many examples available on their website, and they are very easy to learn from these examples.

Below is a link to it:

http://www.dynamicreports.org/

0
source

Source: https://habr.com/ru/post/980294/


All Articles