This is a short guide on how to convert a PDF file into an older PDF version using PHP. This can be useful in situations where the PHP library or application you are using cannot process some of the newer PDF versions.
In my case, a PDF merger library that I was using started to throw fatal errors. Why? Because it only supported PDF version 1.4 and I was attempting to use it with PDF files that were version 1.7.
My solution in this case was to use GhostScript.
What is GhostScript?
GhostScript is a PDF interpreter that you can use via the command line. GhostScript is particularly handy in this kind of situation because it has an easy-to-use PDF conversion command.
If you’re running an Ubuntu server, you can use the following command to install or upgrade GhostScript to the newest version:
sudo apt-get update sudo apt-get install ghostscript
If the above guide does not apply to you, then you can check out GhostScript’s installation guide.
Converting a PDF file to an older version using GhostScript.
The GhostScript command that converts PDF files is structured like so:
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH -sOutputFile=/path/to/new.pdf /path/to/old.pdf
The command above might look complicated and scary, but it’s actually pretty straight forward:
- gs calls GhostScript.
- The -sDEVICE parameter specifies the output device. In our case, we set it to pdfwrite because we want to convert our file into a PDF file.
- The -dCompatibilityLevel parameter is extremely important in this case, as it allows us to set the PDF version of the new file. In the example above, I am converting the file to PDF version 1.4.
- The -dNOPAUSE and -dBATCH parameters allow the conversion process to run without any interactive prompting. This is useful because we obviously do not want any interaction going on if we are running this command via PHP. We just want to execute the command and keep going.
- Finally, you have the -sOutputFile parameter. This specifies the path to the new file and the path to the old file – aka, the file that we want to convert.
Running GhostScript commands with PHP.
Here is an example of how to run a GhostScript command with PHP:
//The PDF version that you want to convert //the file into. $pdfVersion = "1.4"; //The path that you want to save the new //file to $newFile = "/path/to/new.pdf"; //The path of the file that you want //to convert $currentFile = "/path/to/old.pdf"; //Create the GhostScript command $gsCmd = "gs -sDEVICE=pdfwrite -dCompatibilityLevel=$pdfVersion -dNOPAUSE -dBATCH -sOutputFile=$newFile $currentFile"; //Run it using PHP's exec function. exec($gsCmd);
The code above constructs a GhostScript command and then runs it via PHP’s exec function.
PHP’s exec function allows us to execute external programs. However, on some hosting solutions, it may be disabled due to security concerns (the function exec has been listed in the disable_functions directive in php.ini). Unfortunately, there is no workaround for this.
GhostScript creating blank PDF files.
A quick “gotcha” that held me up at the start:
While I was experimenting with GhostScript, I stumbled across an issue where all of my converted PDF files were blank. This issue occurred because I was using the same file path for both the new file and the current file. i.e. I was attempting to convert and replace the PDF file in one go.
In the end, I had to give the new converted file a different name and then delete the old file after the GhostScript command had finished executing.
PDF quality is bad.
If you find that the quality of your converted PDF files is poor, then you can adjust this by adding the -dPDFSETTINGS parameter to your GhostScript command. In my case, I added -dPDFSETTINGS=/printer to the command. The /printer setting tells GhostScript to create a high quality 300 dpi image.
Hopefully, you found this guide helpful!