Read the version of a PDF in PHP

The following is a very short code to read the version number of a PDF document using PHP. I needed this recently during a PDF processing app developed in PHP. As Adobe uses different compression methods in various versions, it becomes necessary to be able to identify the version of the PDF under work.

<?php
 
function pdfVersion($filename)
{ 
    $fp = @fopen($filename, 'rb');
 
    if (!$fp) {
        return 0;
    }
 
    /* Reset file pointer to the start */
    fseek($fp, 0);
 
    /* Read 20 bytes from the start of the PDF */
    preg_match('/\d\.\d/',fread($fp,20),$match);
 
    fclose($fp);
 
    if (isset($match[0])) {
        return $match[0];
    } else {
        return 0;
    }
} 
 
$version = pdfVersion("example.pdf");
pdf


7 thoughts on “Read the version of a PDF in PHP

  1. Nice to know, but the call to fseek is overhead. The ‘r’ option sets the file pointer to the beginning anyway

  2. Actually, this code won’t work with many PDF files.

    For over ten years now (since PDF 1.4), there has been a way for the version number in the header to be overwritten later on in the document, should newer content be added that increases the version.

    The only way to determine the version of a PDF ACCURATELY is to parse the file according to the rules of the standard (ISO 32000-1:2008). That’s why using tools such as PDFlib and others are worthwhile.

    Leonard Rosenthol
    PDF Architect
    Adobe Systems

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>