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.
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");
Nice to know, but the call to fseek is overhead. The ‘r’ option sets the file pointer to the beginning anyway
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
Daniel, you are free to publish the post on your blog.
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