Skip to Content

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.

Ad - Web Hosting from SiteGround - Crafted for easy site management. Click to learn more.
Advertising Disclosure: I am compensated for purchases made through affiliate links. Click here for details.

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 is gswin64c.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!

Posted by: Josh Rowe
Last Updated: January 08, 2022
Created: September 25, 2021

Comments

Marcio

22d
My brother, very well explained. Thank you!

Reply
 

Josh Rowe

22d
You are most welcome! 👏

Reply

Add A Comment

Comment Etiquette: Wrap code in a <code> and </code>. Please keep comments on-topic, do not post spam, keep the conversation constructive, and be nice to each other.