Blog

Back 9 Comments

Merge Multiple PDF Files with PHP


This tutorial explains how to successfully merge multiple PDF files using pdftk and PHP. A recent project of ours required this functionality, and we hope this tutorial saves you some time.

View Demo

Here are some initial tips:

     - Don't
try to use ImageMagick and/or GhostScript to merge the files.
     - Use passthru() instead of exec() or system() to run the pdftk command.

Step One: Install pdftk on your server (CentOS)
You must have pdftk running on your server in order for any of this to work. You also must have root access to your server. If not, see if your hosting provider can install this for you. If you're not running CentOS, you may be able to find your RPM here.

  1. Login to SSH
  2. cd /usr/src
  3. wget http://chrisschuld.com/wp-content/uploads/2008/05/pdftk-112-1i386.rpm
  4. rpm -i pdftk-112-1i386.rpm
  5. You're done installing pdftk (if everything goes well).


Step Two: Build the form to upload your PDF files 
In our project we actually worked with PDF files that were uploaded by the client in an administrative area, which could then be selected in a list before submitting the form to merge the files. In this tutorial, we're going to do everything at once.

Here's what happens when this form is submitted:

  1. Files are uploaded the server
  2. An array is built containing the names of the uploaded files
  3. The merged file is created using the pdftk
  4. The merged PDF opens in the browser


Demo | Source Code

This is the most basic example of how to merge PDF files with PHP. There are many other possibilities, but this should help get you started.

Resources:
AccessPDF
pdftk for CentOS5 (AccessPDF for CentOS 5)

Farid Valipour
July 25, 2009 10:55am
Dear Sir
thanks fot your job.
I have tested the pdftk in windows xp and could merge pdfs with php, but i have tested in windows server 2003 and IIS I could not merge file.

also I have tested in command and could , if you have opinion that what 's my problem with IIS6 , I'll appreciate.

BR
farid valipour
Quamis
November 5, 2009 3:03am
Why not use GhostScript? Seems a pretty good solution to me.
John
November 5, 2009 5:17pm
I initially tried to use GhostScript to merge multiple PDF files, but it was nowhere close to being as accurate as pdftk in merging PDF files. PDFTK is definitely much better.
Virendrasinh Kamreja
February 2, 2010 11:45am
This script looks excellent and i am going to try this with my website, which really wants this feature,

i am trying to save the merged file with the necessary attachments,
will update,

www.globusinternet.com
bluepicaso
May 26, 2010 2:44am
I'm new to this stuff. I work using CODEIGNITER. Can any one help me please.
Ahmad
August 3, 2010 2:17pm
I tried pdftk on my xp machine (Apache 2, php 5) and it worked perfectly. however, i'm trying to do exactly the same thing on a windows 2007 server (Apache 2, php 5) and it's not working.
Help please.
Christian P.
August 25, 2010 5:47am
I've tried your demo, but it didn't worked.
I couldn't download the merged pdf and was redirected to an errorpage,... Is it possible to merge the files on the fly without saving it on the server?
What about merging a pdf-string with a pdf-file? is it also possible?
dfgfg
December 10, 2012 7:56am
dfgdg
Altaf Hussain
January 15, 2013 11:48am
Hi,
I tested this script and it is excellent. But it is not working like i need. I have three files. header.pdf, footer.pdf and center.pdf . Header and footer has less contents where the contents are coming from a php script dynamically. But when i merged the files using this post example demo, it merged the files page per pdf. But i need it to merge it so that all the empty spaces in the document are removed. Is that possible or not?
Looking forward for a quick reply.
Thank you


Your comment has been posted....