diff --git a/CHANGELOG.md b/CHANGELOG.md index 287c30e..37e5316 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ ### Fixed +## [2.0.2] - 2016-09-20 +### Fixed +- Fixed a bug with options without values + ## [2.0.1] - 2016-05-19 ### Fixed - Fixed a critical bug diff --git a/README.md b/README.md index 4d81306..a09f87e 100644 --- a/README.md +++ b/README.md @@ -50,8 +50,11 @@ $input->setUrl('https://www.google.com'); $converter = new Converter($input, new DownloadOutput()); +$converter->setOption('n'); $converter->setOption('d', '300'); + $converter->setOptions([ + 'no-background', 'margin-bottom' => '100', 'margin-top' => '100', ]); diff --git a/src/Spiritix/HtmlToPdf/Converter.php b/src/Spiritix/HtmlToPdf/Converter.php index 4944968..b8cc368 100644 --- a/src/Spiritix/HtmlToPdf/Converter.php +++ b/src/Spiritix/HtmlToPdf/Converter.php @@ -152,6 +152,13 @@ public function setOptions($options) } foreach ($options as $key => $value) { + + // Convert key-only options to regular ones + if (is_numeric($key) && !empty($value)) { + $key = $value; + $value = ''; + } + $this->setOption($key, $value); } @@ -225,9 +232,9 @@ private function buildCommand() } $key = escapeshellcmd(trim($key)); - $value = escapeshellarg(trim($value)); + $value = trim($value); - $optionsString .= $key . ' ' . $value . ' '; + $optionsString .= $key . (empty($value) ? '' : ' ' . escapeshellarg($value)) . ' '; } $command = $this->getBinaryPath() . ' ' . $optionsString . ' - -'; diff --git a/tests/ConverterTest.php b/tests/ConverterTest.php index 7b58f16..42b7814 100644 --- a/tests/ConverterTest.php +++ b/tests/ConverterTest.php @@ -25,14 +25,19 @@ public function setUp() public function testOptions() { + $this->converter->setOption('n'); $this->converter->setOption('R', '500'); $this->converter->setOption('margin-top', '100'); $this->converter->setOptions([ + 'ignore-load-errors', 'B' => '50', 'margin-left' => '10', ]); + $value = $this->converter->getOption('n'); + $this->assertEquals('', $value); + $value = $this->converter->getOption('R'); $this->assertEquals('500', $value); @@ -41,8 +46,10 @@ public function testOptions() $options = $this->converter->getOptions(); $this->assertEquals([ + 'n' => '', 'R' => '500', 'margin-top' => '100', + 'ignore-load-errors' => '', 'B' => '50', 'margin-left' => '10', ], $options);