Merge PDF Files with PHP and GhostScript
This article provides a quick tutorial on merging multiple PDF files into a single file programmatically using PHP and Ghostscript. This quick application was tested on a Ubuntu 18.04 server and Windows desktop.
What is Ghostscript?
Ghostscript is simply an interpreter for PostScript® and PDF files and has been in active development for over 30 years.
Ghostscript is available for both Windows and Linux and can be downloaded here.
Linux also offers the ability to install Ghostscript easily via the command line:
sudo apt update
sudo apt install ghostscript
How to Merge PDF Files
First, let's assign all of the files we want to merge into an array:
$files = [
"page-1.pdf",
"page-2.pdf",
"page-3.pdf"
];
Second, we'll create a blank PDF file that we'll be merging all of our previously defined PDF files into and call it merged.pdf:
file_put_contents(getcwd() . "/pdf/merged.pdf", "");
Next comes the fun part! We're going to create the command that we'll feed into Ghostscript that assigns our newly created PDF file as our merge document:
$command = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=" . getcwd() . "/pdf/merged.pdf ";
Notice that there's a space at the end of this command. This is to ensure we separated our destination document from the remaining separate files that we'll add next:
foreach ($files as $file) {
$command .= getcwd() . "/pdf/" . $file . " ";
}
This loops through our $files
array and appends the file path and name to the end of the command, once again adding a space after each file name to ensure the names are separated.
Next, we'll append 2&>1 to the end of the command so we can see the result of the command execution:
$command .= "2&>1";
Finally, let's execute our command!
$result = shell_exec($command);
PHP's shell_exec()
method will invoke Ghostscript using the command we created, and generate the merged PDF file in the background into our pdf directory. The result is the three PDF files defined in our $files
array is now merged into a single PDF document named merged.pdf.
You can output the result stored in our $result
variable with PHP's echo
command.
Notes for Windows Users
A few items to note if you're running this script on a Windows machine:
- Unless you want to define the whole path to the Ghostscript .exe file in your PHP script, make sure to create an environment variable to the correct path.
- Make sure to use the command line executeable in your Ghostscript installation's
bin
directory. The file name isgswin64c.exe
at the time of this writing. - Make sure to change the
/
to\\
for the file paths. Windows uses backslashes for file paths and the double backslash is needed for PHP as an escape character.
Conclusion
This was a quick tutorial illustrating how to merge multiple PDF files into a single file with PHP and Ghostscript. You can alter the code above as needed for your application.
You can also download the full example from my GitHub repository.
Happy coding!
Created: September 25, 2021