Convert PDF ver 1.7 to ver 1.6 by using php only

Convert PDF ver 1.7 to ver 1.6 by using php only

October 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Convert PDF ver 1.7 to ver 1.6 by using php only

Converting PDF Version 1.7 to 1.6 Using PHP Only

As a senior developer working on APIs that return PDF files, you might find yourself in situations where the PDF version is not compatible with your project’s library. In this case, let’s explore how to convert a PDF file from version 1.7 to the older and more widely supported version 1.6 using PHP.

Understanding the Problem

The Zend PDF library, which is quite popular for working with PDFs in PHP applications, does not support parsing PDF files created with version 1.7 of the specification. If you’re receiving such files from an API and need to process them within your application, you’ll need a way to downgrade the version.

The Conversion Process

Converting between different PDF versions involves modifying specific parts of the file header and structure. While there are no built-in PHP functions for direct conversion, we can achieve this by manipulating the raw binary data using streams.

Here’s a step-by-step approach to convert a PDF from version 1.7 to 1.6:

Step 1: Open the File in Read Mode

First, use fopen to open the input file (version 1.7) in read mode. This will allow you to read the binary data.

$inputFile = fopen('input.pdf', 'rb');

Step 2: Create a Temporary Write Stream

Next, create a temporary write stream using tmpfile. This will be used to store the converted (version 1.6) PDF data in memory.

$outputStream = tmpfile();

Step 3: Read and Convert Each Chunk

Loop through the input file chunk by chunk, reading the binary data and writing it to the output stream after performing the necessary conversion steps. The key changes required are:

  • Modify the header version number from “%PDF-1.7” to “%PDF-1.6”
  • Adjust the cross-reference table if needed
  • Update any relevant flags or values in the document
while (!feof($inputFile)) {
    $chunk = fread($inputFile, 4096); // Read 4KB chunks

    // Search for and replace the version number
    $newChunk = str_replace('%PDF-1.7', '%PDF-1.6', $chunk);

    // Write the converted chunk to the output stream
    fwrite($outputStream, $newChunk);
}

Step 4: Save the Output File

After processing all chunks, rewrite the contents of the output stream to a new file.

$outputFile = fopen('output.pdf', 'w');
rewind($outputStream);
while (!feof($outputStream)) {
    fwrite($outputFile, fread($outputStream, 4096));
}
fclose($inputFile);
fclose($outputStream);
fclose($outputFile);

Conclusion

While this process involves low-level manipulation of binary data and may not be perfect for all use cases, it serves as a starting point for downgrading PDF versions using PHP.

Remember that PDF specifications are complex, and converting between versions requires a deep understanding of the file structure. This approach is a simplification that works in many common scenarios but might fail with more advanced or specialized PDF files.

If you’re looking for a more robust solution, consider using libraries specifically designed for PDF manipulation, such as FPDF or TCPDF, which provide higher-level APIs and handle version compatibility automatically.

For more information on working with PDFs in PHP applications, check out the Laravel Company blog for tutorials and best practices.

Magento 1 API PHP Errors

Our network