About Us

News

RSS
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)

Go Back...

Farid Valipour wrote on 7/25/09 at 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 wrote on 11/05/09 at 3:03am...
"Why not use GhostScript? Seems a pretty good solution to me."
John wrote on 11/05/09 at 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 wrote on 2/02/10 at 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
"

 


Leave a comment

Your Email Address


Your Name


Message