From 5d80bca8fe8586dc8c29fe3aa1f0b8e96cc94918 Mon Sep 17 00:00:00 2001 From: Brian Sweeney Date: Fri, 9 Oct 2015 21:44:56 -0400 Subject: [PATCH] Add version information Users now have three ways to find out the dompdf version: - VERSION file in repo root - $dompdf->version, built on-the-fly from the VERSION file - the Producer field in the PDF metadata The VERSION file depends on format string replacement using the `git archive` command. If the format string isn't replaced dompdf will not use the value in the file and will fall back to "dompdf" as the version. --- .gitattributes | 1 + VERSION | 1 + lib/Cpdf.php | 2 +- src/Adapter/CPDF.php | 2 +- src/Adapter/PDFLib.php | 3 ++- src/Dompdf.php | 36 +++++++++++++++++++++++++++++++++++- 6 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 VERSION diff --git a/.gitattributes b/.gitattributes index 1fcd17b95..2b07aa7d6 100644 --- a/.gitattributes +++ b/.gitattributes @@ -11,3 +11,4 @@ *.txt text *.svg text +VERSION export-subst diff --git a/VERSION b/VERSION new file mode 100644 index 000000000..025c6e77a --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +$Format:<%h>$ \ No newline at end of file diff --git a/lib/Cpdf.php b/lib/Cpdf.php index 9e3b00d16..92fcb8f91 100644 --- a/lib/Cpdf.php +++ b/lib/Cpdf.php @@ -1228,7 +1228,7 @@ protected function o_info($id, $action, $options = '') $this->objects[$id] = array( 't' => 'info', 'info' => array( - 'Creator' => 'R and OS php pdf writer, http://www.ros.co.nz', + 'Producer' => 'CPDF (dompdf)', 'CreationDate' => $date ) ); diff --git a/src/Adapter/CPDF.php b/src/Adapter/CPDF.php index 2f0f25371..3fe71bbc4 100644 --- a/src/Adapter/CPDF.php +++ b/src/Adapter/CPDF.php @@ -194,7 +194,7 @@ function __construct($paper = "letter", $orientation = "portrait", Dompdf $dompd $dompdf->get_option("temp_dir") ); - $this->_pdf->addInfo("Creator", "DOMPDF"); + $this->_pdf->addInfo("Producer", sprintf("%s + CPDF", $dompdf->version)); $time = substr_replace(date('YmdHisO'), '\'', -2, 0) . '\''; $this->_pdf->addInfo("CreationDate", "D:$time"); $this->_pdf->addInfo("ModDate", "D:$time"); diff --git a/src/Adapter/PDFLib.php b/src/Adapter/PDFLib.php index 337f4878a..09f7f33c2 100644 --- a/src/Adapter/PDFLib.php +++ b/src/Adapter/PDFLib.php @@ -181,7 +181,8 @@ function __construct($paper = "letter", $orientation = "portrait", Dompdf $dompd $this->_pdf->set_parameter("textformat", "utf8"); $this->_pdf->set_parameter("fontwarning", "false"); - $this->_pdf->set_info("Creator", "DOMPDF"); + // TODO: fetch PDFLib version information for the producer field + $this->_pdf->set_info("Producer", sprintf("%s + PDFLib", $dompdf->version)); // Silence pedantic warnings about missing TZ settings $tz = @date_default_timezone_get(); diff --git a/src/Dompdf.php b/src/Dompdf.php index 8938258f3..364489f05 100644 --- a/src/Dompdf.php +++ b/src/Dompdf.php @@ -71,6 +71,13 @@ */ class Dompdf { + /** + * Version string for dompdf + * + * @var string + */ + private $version = 'dompdf'; + /** * DomDocument representing the HTML document * @@ -252,6 +259,11 @@ class Dompdf public function __construct() { $this->setOptions(new Options); + + $versionFile = realpath(__DIR__ . '/../VERSION'); + if (file_exists($versionFile) && ($version = file_get_contents($versionFile)) !== false && $version !== '$Format:<%h>$') { + $this->version = sprintf('dompdf %s', $version); + } $this->localeStandard = sprintf('%.1f', 1.0) == '1.0'; $this->saveLocale(); @@ -954,7 +966,7 @@ public function set_option($key, $value) $this->options->set($key, $value); return $this; } - + /** * @param array $options * @return $this @@ -1411,4 +1423,26 @@ public function getFontMetrics() { return $this->fontMetrics; } + + /** + * PHP5 overloaded getter + * Along with {@link Dompdf::__set()} __get() provides access to all + * properties directly. Typically __get() is not called directly outside + * of this class. + * + * @param string $prop + * + * @throws Exception + * @return mixed + */ + function __get($prop) + { + switch ($prop) + { + case 'version' : + return $this->version; + default: + throw new Exception( 'Invalid property: ' . $field ); + } + } }