Generating PDF Files in Java: A Complete Guide with Best Practices
As the need for generating PDFs in Java applications continues to grow, developers often find themselves exploring different ways to produce high-quality documents. Whether you're generating reports, invoices, or exporting data, PDFs are a widely...

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 PdfGenerator {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("example.pdf"));
document.open();
document.add(new Paragraph("Hello, this is a sample PDF generated using iText."));
} catch (DocumentException | FileNotFoundException e) {
e.printStackTrace();
} finally {
document.close();
}
}
}
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import java.io.IOException;
public class PdfBoxGenerator {
public static void main(String[] args) {
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
try {
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
contentStream.newLineAtOffset(100, 700);
contentStream.showText("Hello, this is a sample PDF generated using PDFBox.");
contentStream.endText();
contentStream.close();
document.save("example_pdfbox.pdf");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
document.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- Streaming Large PDFs: Instead of generating the entire PDF in memory, use streaming APIs to write chunks of data to the file system progressively.
- Use Memory Efficient Libraries: Libraries like iText offer mechanisms for managing memory better when working with large PDFs.
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("secure.pdf"));
writer.setEncryption("user_password".getBytes(), "owner_password".getBytes(),
PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
- iText is ideal for applications needing advanced features such as interactive forms, encryption, or dynamic content generation. It’s also more mature and provides extensive documentation.
- PDFBox, being open-source and more lightweight, is perfect for projects that don’t need complex PDF functionality but still need a reliable library for generating documents.
Read more at : Generating PDF Files in Java: A Complete Guide with Best Practices
About the author
I am Tuanh.net. As of 2024, I have accumulated 8 years of experience in backend programming. I am delighted to connect and share my knowledge with everyone.
Comments
Join the discussionNo comments yet. Be the first to comment.





