From 9d58fa8ec629536d02dec8981a630be3d43e62b6 Mon Sep 17 00:00:00 2001 From: "anton.nikolaev" Date: Mon, 3 Apr 2017 15:07:24 +0300 Subject: [PATCH] bug-fix #8 Single page issue --- src/Base.php | 2 +- src/Html.php | 2 +- src/Pdf.php | 44 +++++++++++++++++++++++++++++++++++++------- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/Base.php b/src/Base.php index 9add8c6..28a643e 100644 --- a/src/Base.php +++ b/src/Base.php @@ -42,7 +42,7 @@ public function getOptions($key=null) public function setOptions($key, $value=null) { if (is_array($key)) { - $this->options = array_merge($this->options, $key); + $this->options = array_replace_recursive($this->options, $key); } elseif (is_string($key)) { $this->options[$key] = $value; } diff --git a/src/Html.php b/src/Html.php index 08b8b4e..0d0d88d 100644 --- a/src/Html.php +++ b/src/Html.php @@ -32,7 +32,7 @@ class Html extends Base public function __construct($options=[]) { - $this->setOptions(array_merge($this->defaultOptions, $options)); + $this->setOptions(array_replace_recursive($this->defaultOptions, $options)); } /** diff --git a/src/Pdf.php b/src/Pdf.php index 9edc8c7..1124552 100644 --- a/src/Pdf.php +++ b/src/Pdf.php @@ -20,6 +20,7 @@ class Pdf extends Base private $file = null; private $info = null; private $html = null; + private $result = null; private $defaultOptions = [ 'pdftohtml_path' => '/usr/bin/pdftohtml', @@ -30,7 +31,7 @@ class Pdf extends Base 'imageJpeg' => false, 'ignoreImages' => false, 'zoom' => 1.5, - 'noFrames' => false, + 'noFrames' => true, ], 'outputDir' => '', @@ -44,7 +45,7 @@ class Pdf extends Base public function __construct($file, $options=[]) { - $this->setOptions(array_merge($this->defaultOptions, $options)); + $this->setOptions(array_replace_recursive($this->defaultOptions, $options)); $this->setFile($file)->setInfoObject()->setHtmlObject(); } @@ -146,9 +147,17 @@ private function getContent() $fileinfo = pathinfo($this->file); $base_path = $this->getOutputDir() . '/' . $fileinfo['filename']; - for ($i = 1; $i <= $this->countPages(); $i++) { - $content = file_get_contents($base_path . '-' . $i . '.html'); - $this->html->addPage($i, $content); + $countPages = $this->countPages(); + if ($countPages) { + if ($countPages > 1) + for ($i = 1; $i <= $countPages; $i++) { + $content = file_get_contents($base_path . '-' . $i . '.html'); + $this->html->addPage($i, $content); + } + else { + $content = file_get_contents($base_path . '.html'); + $this->html->addPage(1, $content); + } } if ($this->getOptions('clearAfter')) @@ -161,11 +170,32 @@ private function getContent() */ private function generate() { + $this->result = null; + $command = $this->getCommand(); + $this->result = exec($command); + return $this; + } + + /** + * Get command for generate html + * @return string + */ + public function getCommand() { + if ($this->countPages() > 1) + $this->setOptions(['generate'=>['noFrames' => false]]); $output = $this->getOutputDir() . '/' . preg_replace("/\.pdf$/", '', basename($this->file)) . '.html'; $options = $this->generateOptions(); $command = $this->getOptions('pdftohtml_path') . ' ' . $options . ' ' . $this->file . ' ' . $output; - $result = exec($command); - return $this; + return $command; + } + + /** + * Get result of generate html + * @return string|null + */ + public function getResult() + { + return $this->result; } /**