From e03d84c096967f21bb8904c4b25b5308d8d8ec92 Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Tue, 21 Oct 2014 17:09:35 -0400 Subject: [PATCH 01/12] Updated composer install instructions --- README.markdown | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/README.markdown b/README.markdown index b2a7db50..2d72eb81 100644 --- a/README.markdown +++ b/README.markdown @@ -10,14 +10,8 @@ You will have to download wkhtmltopdf `0.12.x` in order to use Snappy. ## Installation using [Composer](http://getcomposer.org/) -Add to your `composer.json`: - -```json -{ - "require" : { - "knplabs/knp-snappy": "*" - } -} +```bash +$ composer require knplabs/knp-snappy ``` ## Usage From cba465c4f90a6b69dcc562e6f0797e29c6ecc8f6 Mon Sep 17 00:00:00 2001 From: Daniel Karpinski Date: Mon, 10 Nov 2014 11:14:59 +0100 Subject: [PATCH 02/12] Add check exists before unlink and refactor Header & Footer file now only removed when option is not null and file exists. Fixes: when option['header-html'] or option['footer-html'] is null then unlink(): No such file or directory thrown on generate. --- src/Knp/Snappy/Pdf.php | 48 +++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/src/Knp/Snappy/Pdf.php b/src/Knp/Snappy/Pdf.php index be2ee771..922a4170 100644 --- a/src/Knp/Snappy/Pdf.php +++ b/src/Knp/Snappy/Pdf.php @@ -29,14 +29,12 @@ public function __construct($binary = null, array $options = array()) */ protected function handleOptions(array $options = array()) { - $headerHtml = isset($options['header-html']) ? $options['header-html'] : null; - if (null !== $headerHtml && !filter_var($headerHtml, FILTER_VALIDATE_URL) && !$this->isFile($headerHtml)) { - $options['header-html'] = $this->createTemporaryFile($headerHtml, 'html'); + if ($this->isFileHeader($options['header-html']) && !$this->isFile($options['header-html'])) { + $options['header-html'] = $this->createTemporaryFile($options['header-html'], 'html'); } - $footerHtml = isset($options['footer-html']) ? $options['footer-html'] : null; - if (null !== $footerHtml && !filter_var($footerHtml, FILTER_VALIDATE_URL) && !$this->isFile($footerHtml)) { - $options['footer-html'] = $this->createTemporaryFile($footerHtml, 'html'); + if ($this->isFileFooter($options['footer-html']) && !$this->isFile($options['footer-html'])) { + $options['footer-html'] = $this->createTemporaryFile($options['footer-html'], 'html'); } return $options; @@ -52,15 +50,49 @@ public function generate($input, $output, array $options = array(), $overwrite = parent::generate($input, $output, $options, $overwrite); // to delete header or footer generated files - if (array_key_exists('header-html', $options) && !filter_var($options['header-html'], FILTER_VALIDATE_URL)) { + if ($this->isFileHeader($options['header-html']) && $this->isFile($options['header-html'])) { $this->unlink($options['header-html']); } - if (array_key_exists('footer-html', $options) && !filter_var($options['footer-html'], FILTER_VALIDATE_URL)) { + if ($this->isFileFooter($options['footer-html']) && $this->isFile($options['footer-html'])) { $this->unlink($options['footer-html']); } } + /** + * @param $options + * @return bool + */ + protected function isFileHeader($options) + { + if ( isset($options['header-html']) ) + { + return !$this->isOptionUrl($options['header-html']); + } + return false; + } + + /** + * @return bool + */ + protected function isFileFooter() + { + if ( isset($options['footer-html']) ) + { + return !$this->isOptionUrl($options['footer-html']); + } + return false; + } + + /** + * @param $option + * @return bool + */ + protected function isOptionUrl($option) + { + return (bool) filter_var($option, FILTER_VALIDATE_URL); + } + /** * {@inheritDoc} */ From 379542d9de7aa3810c837a88163963370f8bbb1c Mon Sep 17 00:00:00 2001 From: Daniel Karpinski Date: Mon, 10 Nov 2014 11:28:33 +0100 Subject: [PATCH 03/12] Fix isFileHeader/isFileFooter Params Now passing full options array --- src/Knp/Snappy/Pdf.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Knp/Snappy/Pdf.php b/src/Knp/Snappy/Pdf.php index 922a4170..c1d83e97 100644 --- a/src/Knp/Snappy/Pdf.php +++ b/src/Knp/Snappy/Pdf.php @@ -29,11 +29,11 @@ public function __construct($binary = null, array $options = array()) */ protected function handleOptions(array $options = array()) { - if ($this->isFileHeader($options['header-html']) && !$this->isFile($options['header-html'])) { + if ($this->isFileHeader($options) && !$this->isFile($options['header-html'])) { $options['header-html'] = $this->createTemporaryFile($options['header-html'], 'html'); } - if ($this->isFileFooter($options['footer-html']) && !$this->isFile($options['footer-html'])) { + if ($this->isFileFooter($options) && !$this->isFile($options['footer-html'])) { $options['footer-html'] = $this->createTemporaryFile($options['footer-html'], 'html'); } @@ -50,11 +50,11 @@ public function generate($input, $output, array $options = array(), $overwrite = parent::generate($input, $output, $options, $overwrite); // to delete header or footer generated files - if ($this->isFileHeader($options['header-html']) && $this->isFile($options['header-html'])) { + if ($this->isFileHeader($options) && $this->isFile($options['header-html'])) { $this->unlink($options['header-html']); } - if ($this->isFileFooter($options['footer-html']) && $this->isFile($options['footer-html'])) { + if ($this->isFileFooter($options) && $this->isFile($options['footer-html'])) { $this->unlink($options['footer-html']); } } From 349fc20c940748ee248d1247627ecfc9a51fa4ea Mon Sep 17 00:00:00 2001 From: Daniel Karpinski Date: Fri, 14 Nov 2014 10:48:50 +0100 Subject: [PATCH 04/12] CS fixes --- src/Knp/Snappy/Pdf.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Knp/Snappy/Pdf.php b/src/Knp/Snappy/Pdf.php index c1d83e97..766a81aa 100644 --- a/src/Knp/Snappy/Pdf.php +++ b/src/Knp/Snappy/Pdf.php @@ -65,8 +65,7 @@ public function generate($input, $output, array $options = array(), $overwrite = */ protected function isFileHeader($options) { - if ( isset($options['header-html']) ) - { + if (isset($options['header-html'])) { return !$this->isOptionUrl($options['header-html']); } return false; @@ -77,8 +76,7 @@ protected function isFileHeader($options) */ protected function isFileFooter() { - if ( isset($options['footer-html']) ) - { + if (isset($options['footer-html'])) { return !$this->isOptionUrl($options['footer-html']); } return false; @@ -90,7 +88,7 @@ protected function isFileFooter() */ protected function isOptionUrl($option) { - return (bool) filter_var($option, FILTER_VALIDATE_URL); + return (bool)filter_var($option, FILTER_VALIDATE_URL); } /** From 3d166758fd251b35b0d7c2cb6171c9e3e4c980dc Mon Sep 17 00:00:00 2001 From: R2c Date: Wed, 19 Nov 2014 17:53:55 +0100 Subject: [PATCH 05/12] Add a possibility to specify the temporary folder --- src/Knp/Snappy/AbstractGenerator.php | 36 +++++++++++++++++++++++++++- test/Knp/Snappy/PdfTest.php | 23 +++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/Knp/Snappy/AbstractGenerator.php b/src/Knp/Snappy/AbstractGenerator.php index 796345ff..060b4989 100644 --- a/src/Knp/Snappy/AbstractGenerator.php +++ b/src/Knp/Snappy/AbstractGenerator.php @@ -20,6 +20,11 @@ abstract class AbstractGenerator implements GeneratorInterface private $timeout = false; private $defaultExtension; + /** + * @var string + */ + protected $temporaryFolder; + /** * Constructor * @@ -346,7 +351,8 @@ protected function checkProcessStatus($status, $stdout, $stderr, $command) */ protected function createTemporaryFile($content = null, $extension = null) { - $filename = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . uniqid('knp_snappy', true); + $filename = rtrim($this->getTemporaryFolder(), DIRECTORY_SEPARATOR) + . DIRECTORY_SEPARATOR . uniqid('knp_snappy', true); if (null !== $extension) { $filename .= '.'.$extension; @@ -503,6 +509,34 @@ protected function prepareOutput($filename, $overwrite) } } + /** + * Get TemporaryFolder + * + * @return string + */ + public function getTemporaryFolder() + { + if ($this->temporaryFolder === null) { + return sys_get_temp_dir(); + } + + return $this->temporaryFolder; + } + + /** + * Set temporaryFolder + * + * @param string $temporaryFolder + * + * @return $this + */ + public function setTemporaryFolder($temporaryFolder) + { + $this->temporaryFolder = $temporaryFolder; + + return $this; + } + /** * Wrapper for the "file_get_contents" function * diff --git a/test/Knp/Snappy/PdfTest.php b/test/Knp/Snappy/PdfTest.php index e86b57dd..53d17dff 100644 --- a/test/Knp/Snappy/PdfTest.php +++ b/test/Knp/Snappy/PdfTest.php @@ -20,6 +20,27 @@ public function testThatSomething() $testObject->getOutputFromHtml('', array()); $this->assertRegExp("/emptyBinary --lowquality '.*' '.*'/", $testObject->getLastCommand()); } + + public function testThatSomethingUsingTmpFolder() + { + $testObject = new PdfSpy(); + $testObject->setTemporaryFolder(__DIR__); + + $testObject->getOutputFromHtml('', array('footer-html' => 'footer')); + $this->assertRegExp("/emptyBinary --lowquality --footer-html '.*' '.*' '.*'/", $testObject->getLastCommand()); + } + + /** + * @expectedException PHPUnit_Framework_Error + */ + public function testThatSomethingUsingWrongTmpFolder() + { + $testObject = new PdfSpy(); + $testObject->setTemporaryFolder(__DIR__.'/i-dont-exist'); + + $testObject->getOutputFromHtml('', array('footer-html' => 'footer')); + } + } class PdfSpy extends Pdf @@ -54,4 +75,4 @@ public function getOutput($input, array $options = array()) return "output"; } -} \ No newline at end of file +} From 64f2ee06d44e3789403a3f13e66bb8030c6b76a3 Mon Sep 17 00:00:00 2001 From: Matyas Somfai Date: Mon, 24 Nov 2014 15:06:52 +0100 Subject: [PATCH 06/12] remove only temporary files created by the generator, on destruct --- src/Knp/Snappy/AbstractGenerator.php | 35 +++++++++++++++-------- src/Knp/Snappy/Pdf.php | 11 +------ test/Knp/Snappy/AbstractGeneratorTest.php | 28 ------------------ 3 files changed, 24 insertions(+), 50 deletions(-) diff --git a/src/Knp/Snappy/AbstractGenerator.php b/src/Knp/Snappy/AbstractGenerator.php index 060b4989..303958e2 100644 --- a/src/Knp/Snappy/AbstractGenerator.php +++ b/src/Knp/Snappy/AbstractGenerator.php @@ -25,6 +25,11 @@ abstract class AbstractGenerator implements GeneratorInterface */ protected $temporaryFolder; + /** + * @var array + */ + public $temporaryFiles = array(); + /** * Constructor * @@ -41,6 +46,11 @@ public function __construct($binary, array $options = array(), array $env = null $this->env = $env; } + public function __destruct() + { + $this->removeTemporaryFiles(); + } + /** * This method must configure the media options * @@ -155,10 +165,6 @@ public function generateFromHtml($html, $output, array $options = array(), $over } $this->generate($fileNames, $output, $options, $overwrite); - - foreach ($fileNames as $filename) { - $this->unlink($filename); - } } /** @@ -172,8 +178,6 @@ public function getOutput($input, array $options = array()) $result = $this->getFileContents($filename); - $this->unlink($filename); - return $result; } @@ -193,10 +197,6 @@ public function getOutputFromHtml($html, array $options = array()) $result = $this->getOutput($fileNames, $options); - foreach ($fileNames as $filename) { - $this->unlink($filename); - } - return $result; } @@ -296,7 +296,7 @@ protected function mergeOptions(array $options) * @param string $output The output filename * @param string $command The generation command * - * @throws RuntimeException if the output file generation failed + * @throws \RuntimeException if the output file generation failed */ protected function checkOutput($output, $command) { @@ -325,7 +325,7 @@ protected function checkOutput($output, $command) * @param string $stderr The stderr content * @param string $command The run command * - * @throws RuntimeException if the output file generation failed + * @throws \RuntimeException if the output file generation failed */ protected function checkProcessStatus($status, $stdout, $stderr, $command) { @@ -360,11 +360,22 @@ protected function createTemporaryFile($content = null, $extension = null) if (null !== $content) { file_put_contents($filename, $content); + $this->temporaryFiles[md5($filename)] = $filename; } return $filename; } + /** + * Removes all temporary files + */ + protected function removeTemporaryFiles() + { + foreach ($this->temporaryFiles as $file) { + $this->unlink($file); + } + } + /** * Builds the command string * diff --git a/src/Knp/Snappy/Pdf.php b/src/Knp/Snappy/Pdf.php index 766a81aa..2f71ebdf 100644 --- a/src/Knp/Snappy/Pdf.php +++ b/src/Knp/Snappy/Pdf.php @@ -25,7 +25,7 @@ public function __construct($binary = null, array $options = array()) /** * handle options to transform HTML header-html or footer-html into files contains HTML * @param array $options - * @return array $options Tranformed options + * @return array $options Transformed options */ protected function handleOptions(array $options = array()) { @@ -48,15 +48,6 @@ public function generate($input, $output, array $options = array(), $overwrite = $options = $this->handleOptions($options); parent::generate($input, $output, $options, $overwrite); - - // to delete header or footer generated files - if ($this->isFileHeader($options) && $this->isFile($options['header-html'])) { - $this->unlink($options['header-html']); - } - - if ($this->isFileFooter($options) && $this->isFile($options['footer-html'])) { - $this->unlink($options['footer-html']); - } } /** diff --git a/test/Knp/Snappy/AbstractGeneratorTest.php b/test/Knp/Snappy/AbstractGeneratorTest.php index 8b1c251d..04932e45 100644 --- a/test/Knp/Snappy/AbstractGeneratorTest.php +++ b/test/Knp/Snappy/AbstractGeneratorTest.php @@ -194,7 +194,6 @@ public function testGenerateFromHtml() 'configure', 'generate', 'createTemporaryFile', - 'unlink' ), array( 'the_binary' @@ -211,12 +210,6 @@ public function testGenerateFromHtml() ) ->will($this->returnValue('the_temporary_file')) ; - $media - ->expects($this->once()) - ->method('unlink') - ->with($this->equalTo('the_temporary_file')) - ->will($this->returnValue(true)) - ; $media ->expects($this->once()) ->method('generate') @@ -238,7 +231,6 @@ public function testGenerateFromHtmlWithHtmlArray() 'configure', 'generate', 'createTemporaryFile', - 'unlink' ), array( 'the_binary' @@ -255,12 +247,6 @@ public function testGenerateFromHtmlWithHtmlArray() ) ->will($this->returnValue('the_temporary_file')) ; - $media - ->expects($this->once()) - ->method('unlink') - ->with($this->equalTo('the_temporary_file')) - ->will($this->returnValue(true)) - ; $media ->expects($this->once()) ->method('generate') @@ -337,7 +323,6 @@ public function testGetOutputFromHtml() 'configure', 'getOutput', 'createTemporaryFile', - 'unlink' ), array(), '', @@ -361,12 +346,6 @@ public function testGetOutputFromHtml() ) ->will($this->returnValue('the output')) ; - $media - ->expects($this->once()) - ->method('unlink') - ->with($this->equalTo('the_temporary_file')) - ->will($this->returnValue(true)) - ; $this->assertEquals('the output', $media->getOutputFromHtml('foo', array('foo' => 'bar'))); } @@ -379,7 +358,6 @@ public function testGetOutputFromHtmlWithHtmlArray() 'configure', 'getOutput', 'createTemporaryFile', - 'unlink' ), array(), '', @@ -403,12 +381,6 @@ public function testGetOutputFromHtmlWithHtmlArray() ) ->will($this->returnValue('the output')) ; - $media - ->expects($this->once()) - ->method('unlink') - ->with($this->equalTo('the_temporary_file')) - ->will($this->returnValue(true)) - ; $this->assertEquals('the output', $media->getOutputFromHtml(array('foo'), array('foo' => 'bar'))); } From 3a70bf7edec51a6691dad8bca88b96bdd58b3046 Mon Sep 17 00:00:00 2001 From: Piotr Antosik Date: Wed, 26 Nov 2014 22:18:43 +0100 Subject: [PATCH 07/12] Fix #121 isFileFooter have missed argument --- src/Knp/Snappy/Pdf.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Knp/Snappy/Pdf.php b/src/Knp/Snappy/Pdf.php index 2f71ebdf..f2442b7e 100644 --- a/src/Knp/Snappy/Pdf.php +++ b/src/Knp/Snappy/Pdf.php @@ -51,7 +51,7 @@ public function generate($input, $output, array $options = array(), $overwrite = } /** - * @param $options + * @param array $options * @return bool */ protected function isFileHeader($options) @@ -63,9 +63,10 @@ protected function isFileHeader($options) } /** + * @param array $options * @return bool */ - protected function isFileFooter() + protected function isFileFooter($options) { if (isset($options['footer-html'])) { return !$this->isOptionUrl($options['footer-html']); From 1db37e279d9a585f2f770fb6499b96c60019bf3a Mon Sep 17 00:00:00 2001 From: Piotr Antosik Date: Wed, 26 Nov 2014 22:33:48 +0100 Subject: [PATCH 08/12] Update dev branch alias --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a9f7c32f..de3880c2 100644 --- a/composer.json +++ b/composer.json @@ -56,7 +56,7 @@ ], "extra": { "branch-alias": { - "dev-master": "0.2.x-dev" + "dev-master": "0.3.x-dev" } } } From 6ba20beef1756650c2782f1cacbc8ad22df44509 Mon Sep 17 00:00:00 2001 From: Kris Pypen Date: Fri, 5 Dec 2014 10:43:09 +0100 Subject: [PATCH 09/12] Add possibility to pass a cover as html --- src/Knp/Snappy/Pdf.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Knp/Snappy/Pdf.php b/src/Knp/Snappy/Pdf.php index f2442b7e..d9e68b44 100644 --- a/src/Knp/Snappy/Pdf.php +++ b/src/Knp/Snappy/Pdf.php @@ -37,6 +37,10 @@ protected function handleOptions(array $options = array()) $options['footer-html'] = $this->createTemporaryFile($options['footer-html'], 'html'); } + if ($this->isFileCover($options) && !$this->isFile($options['cover'])) { + $options['cover'] = $this->createTemporaryFile($options['cover'], 'html'); + } + return $options; } @@ -74,6 +78,18 @@ protected function isFileFooter($options) return false; } + /** + * @param array $options + * @return bool + */ + protected function isFileCover($options) + { + if (isset($options['cover'])) { + return !$this->isOptionUrl($options['cover']); + } + return false; + } + /** * @param $option * @return bool From 2489c102b58a04f66b116f48bf8fa29ea4bc25cd Mon Sep 17 00:00:00 2001 From: Kris Pypen Date: Fri, 5 Dec 2014 12:21:37 +0100 Subject: [PATCH 10/12] add documentation --- README.markdown | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.markdown b/README.markdown index 2d72eb81..f78538aa 100644 --- a/README.markdown +++ b/README.markdown @@ -55,6 +55,8 @@ $snappy->setOption('no-background', true); $snappy->setOption('allow', array('/path1', '/path2')); $snappy->setOption('cookie', array('key' => 'value', 'key2' => 'value2')); $snappy->setOption('cover', 'pathToCover.html'); +// .. or pass a cover as html +$snappy->setOption('cover', '

Bill cover

'); $snappy->setOption('toc', true); ``` From 6616d9a95a0fbd057c26b76af51900499786f46d Mon Sep 17 00:00:00 2001 From: Kris Pypen Date: Fri, 5 Dec 2014 12:21:53 +0100 Subject: [PATCH 11/12] added a newline before the return statement --- src/Knp/Snappy/Pdf.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Knp/Snappy/Pdf.php b/src/Knp/Snappy/Pdf.php index d9e68b44..6ec5db5a 100644 --- a/src/Knp/Snappy/Pdf.php +++ b/src/Knp/Snappy/Pdf.php @@ -87,6 +87,7 @@ protected function isFileCover($options) if (isset($options['cover'])) { return !$this->isOptionUrl($options['cover']); } + return false; } From 2b186efd44f3e6b62c1706682e3c9fc96fff95fa Mon Sep 17 00:00:00 2001 From: Arnaud Langlade Date: Fri, 12 Dec 2014 12:23:55 +0100 Subject: [PATCH 12/12] Missing variable --- src/Knp/Snappy/Image.php | 4 ++-- src/Knp/Snappy/Pdf.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Knp/Snappy/Image.php b/src/Knp/Snappy/Image.php index 4e811f76..5c2f668a 100644 --- a/src/Knp/Snappy/Image.php +++ b/src/Knp/Snappy/Image.php @@ -15,11 +15,11 @@ class Image extends AbstractGenerator /** * {@inheritDoc} */ - public function __construct($binary = null, array $options = array()) + public function __construct($binary = null, array $options = array(), array $env = null) { $this->setDefaultExtension('jpg'); - parent::__construct($binary, $options); + parent::__construct($binary, $options, $env); } /** diff --git a/src/Knp/Snappy/Pdf.php b/src/Knp/Snappy/Pdf.php index f2442b7e..116f09ae 100644 --- a/src/Knp/Snappy/Pdf.php +++ b/src/Knp/Snappy/Pdf.php @@ -15,11 +15,11 @@ class Pdf extends AbstractGenerator /** * {@inheritDoc} */ - public function __construct($binary = null, array $options = array()) + public function __construct($binary = null, array $options = array(), array $env = null) { $this->setDefaultExtension('pdf'); - parent::__construct($binary, $options); + parent::__construct($binary, $options, $env ); } /**