On the heels of PDF Generation with Pisa in Django I’ve had the need to pull from a library of pdfs and concatenate based on user selection. At first there seemed to be no good solutions but research and a recommendation by James Tauber (thanks James) turned up pdftk (the pdf toolkit).
The pdf toolkit has some nice features beyond concatenation to inlcude:
- Merge PDF Documents
- Split PDF Pages into a New Document
- Rotate PDF Pages or Documents
- Decrypt Input as Necessary (Password Required)
- Encrypt Output as Desired
- Fill PDF Forms with FDF Data or XFDF Data and/or Flatten Forms
- Apply a Background Watermark or a Foreground Stamp
- Report on PDF Metrics such as Metadata, Bookmarks, and Page Labels
- Update PDF Metadata
- Attach Files to PDF Pages or the PDF Document
- Unpack PDF Attachments
- Burst a PDF Document into Single Pages
- Uncompress and Re-Compress Page Streams
- Repair Corrupted PDF (Where Possible)
I won’t go into all these options. You can see the command reference on the pdftk site.
To do the concatenation in your Django app you can something like this:
import os
def makepdf(request, some_id_todo_something_with):
os.system("pdftk filename1.pdf filename2.pdf cat output newname.pdf")
return HttpResponseRedirect(reverse('send_somewhere'))
I’m simply serving them up as static files when they are done.