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");

This site is a digital habitat of Sameer Borate, a freelance web developer working in PHP, MySQL and WordPress. I also provide web scraping services, website design and development and integration of various Open Source API's. Contact me at metapix[at]gmail.com for any new project requirements and price quotes.

7 Responses

1

arne tarara

July 21st, 2011 at 9:57 am

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

2

Daniel Carrero

July 22nd, 2011 at 8:59 am

Excellent post!
I read in PHPDeveloper.org post ->
http://www.phpdeveloper.org/news/16621
But they mention the function “pdf_get_value”
http://www.php.net/manual/en/function.pdf-get-value.php
I prefer your function is independent of external libraries or modifications to php.
I will republish this post on my blog, if you give me permission for it.

Thanks a lot

3

Leer la version de un PDF con PHP | Blog de Daniel Carrero

July 22nd, 2011 at 9:30 am

[...] trata de una función php, creada por Sameer Borate que nos permite saber la versión de un archivo [...]

sameer

July 22nd, 2011 at 7:53 pm

Daniel, you are free to publish the post on your blog.

5

Sameer Borate Blog: Lesen Sie die Version eines PDF in PHP | PHP Boutique

July 22nd, 2011 at 10:53 pm

[...] Borate hat einen schnellen Beitrag in seinem Blog heute mit etwas Code, mit dem Sie Lesen Sie die Version eines PDF-Dokuments programmatisch ohne Abhängigkeit von einem PDF-Erweiterung oder-Bibliothek installiert. Die [...]

6

Programowanie w PHP » Blog Archive » Sameer Borate’s Blog: Read the version of a PDF in PHP

July 26th, 2011 at 5:42 am

[...] Borate has a quick post to his blog today with some code that lets you read the version of a PDF document programmatically without a dependency on a PDF extension or library being installed. The [...]

7

Leonard Rosenthol

July 26th, 2011 at 12:15 pm

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

Your thoughts