Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Generating PDF Files with Java for Electronic Vouchers

Tech 2

iText Library Overview

iText is a widely used open-source Java library for generating PDF documents, originally hosted on SourceForge. It supports creating PDF or RTF files, and can convert XML or HTML content to PDF format. Two major versions are available: iText 5, which is the most commonly used in public projects, and iText 7, a full refactoring of the original library with improved design. For most basic use cases, either version will work well, as iText 5 remains the most prevalent in publlic Maven repositories.

Basic PDF Generation Example

First, add the Maven dependency for iText 5:

<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.11</version>
</dependency>

A minimal implementation to generate a PDF file:

package com.example.pdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class BasicPdfGenerator {
    private static final String OUTPUT_PATH = "output/HelloWorld.pdf";

    public static void main(String[] args) throws FileNotFoundException, DocumentException {
        Document pdfDocument = new Document();
        PdfWriter writer = PdfWriter.getInstance(pdfDocument, new FileOutputStream(OUTPUT_PATH));
        pdfDocument.open();
        pdfDocument.add(new Paragraph("Hello World"));
        pdfDocument.close();
        writer.close();
    }
}

Adding Chinese Language Support

By default, iText does not support Chinese charactres, so you need to include a supported Chinese font file such as SimHei (simhei.ttf).

A sample implementation with Chinese text rendering:

package com.example.pdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class ChinesePdfGenerator {
    private static final String OUTPUT_PATH = "output/ChineseHelloWorld.pdf";
    private static final String CHINESE_FONT_PATH = "simhei.ttf";

    public static void main(String[] args) throws FileNotFoundException, DocumentException {
        Document pdfDocument = new Document();
        PdfWriter writer = PdfWriter.getInstance(pdfDocument, new FileOutputStream(OUTPUT_PATH));
        pdfDocument.open();
        Font chineseFont = FontFactory.getFont(CHINESE_FONT_PATH, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        pdfDocument.add(new Paragraph("Hello World, 这是中文测试内容", chineseFont));
        pdfDocument.close();
        writer.close();
    }
}

Generating PDF from HTML Content

For complex PDF layouts, you can use HTML templates to generate PDF documents. The iText 5 XML Worker add-on is required for this functionality.

Add the Maven dependency for XML Worker:

<!-- https://mvnrepository.com/artifact/com.itextpdf.tool/xmlworker -->
<dependency>
    <groupId>com.itextpdf.tool</groupId>
    <artifactId>xmlworker</artifactId>
    <version>5.5.11</version>
</dependency>

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.