[reportlab-users] can reportlab combine PDFs and/or add a password to a PDF?

George Kappel gkappel at herrspacific.com
Wed Apr 11 18:26:49 EDT 2007

pdftk is limiting when you are trying to keep features like bookmarks
working so if you can handle a little bit of java checkout iText. This
will run with java or gcj

Following is a snippet taken from the iText docs and altered to encrypt,
compress and concat any # of pdf files while maintaining the integrity
of bookmarks

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PRAcroForm;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.SimpleBookmark;

* Tool that can be used to concatenate existing PDF files.
public class pdf_concat {

* This class can be used to concatenate existing PDF files.
* (This was an example known as PdfCopy.java)
* @param args the command line arguments
public static void main(String args[]) {
if (args.length < 4) {
System.err.println("arguments: userpw ownerpw file1 [file2
...] destfile");
else {
//System.out.println("PdfCopy example");
try {
int pageOffset = 0;
ArrayList master = new ArrayList();
int f = 2;
String outFile = args[args.length-1];
Document document = null;
PdfCopy writer = null;
while (f < args.length-1) {
// we create a reader for a certain document
PdfReader reader = new PdfReader(args[f]);
// we retrieve the total number of pages
int n = reader.getNumberOfPages();
List bookmarks = SimpleBookmark.getBookmark(reader);
if (bookmarks != null) {
if (pageOffset != 0)
pageOffset, null);
pageOffset += n;

if (f == 2) {
// step 1: creation of a document-object
document = new
// step 2: we create a writer that listens to
the document
writer = new PdfCopy(document, new
writer.setEncryption(true, args[0], args[1],

// step 3: we open the document
// step 4: we add content
PdfImportedPage page;
for (int i = 0; i < n; ) {
page = writer.getImportedPage(reader, i);
PRAcroForm form = reader.getAcroForm();
if (form != null)
if (!master.isEmpty())
// step 5: we close the document
catch(Exception e) {

Tim Roberts wrote:

> Mark Turansky wrote:


>> I just installed ReportLab 2.0 and looked over the remarkably good

>> documentation. I was able to generate a PDF containing several graphs

>> very easily. I did not see two specific features in the

>> documentation, so I thought I'd ask the mailing list.


>> Is it possible to

>> a) secure the PDF with a password?

>> b) open other PDFs and combine them into a single PDF?



> Allow me to point out that the excellent open source tool "pdftk" can

> perform both of these tasks, as well as many more. Everyone who works

> with PDF files should have it in their toolbox.


> http://www.accesspdf.com/pdftk/



More information about the reportlab-users mailing list