Converting PDF files into older PDF versions using PHP.

In this guide, we will show you 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. This happened because the library only supported PDF version 1.4, whereas I was attempting to use it with PDF files that were version 1.7.

The solution in this case is to use GhostScript.

What is GhostScript?

GhostScript is a PDF interpreter that you can use via the command line.

It is particularly useful in this kind of situation because it has an easy-to-use PDF conversion command.

Installing GhostScript.

If you are using an Ubuntu server, you can run 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.

Below is an example of a command that will convert PDF files into version 1.4:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH 
-sOutputFile=/path/to/new.pdf /path/to/old.pdf

Although this command might look complicated and scary, it is actually pretty straight forward when you break it down:

  • 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.

Below is an example of how to run GhostScript commands 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 our command and then runs it via PHP’s exec function.

PHP’s exec function allows us to execute external programs.

However, some hosting platforms might disable this function due to security concerns.

Unfortunately, there is no workaround for this.

GhostScript creating blank PDF files.

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. In other words, 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.