diff --git a/library/Zend/Pdf.php b/library/Zend/Pdf.php index 38e1d6858e..6e76ca6acf 100644 --- a/library/Zend/Pdf.php +++ b/library/Zend/Pdf.php @@ -328,6 +328,7 @@ public function __construct($source = null, $revision = null, $load = false) $this->_loadNamedDestinations($this->_trailer->Root, $this->_parser->getPDFVersion()); $this->_loadOutlines($this->_trailer->Root); + $this->_loadJavaScript($this->_trailer->Root); if ($this->_trailer->Info !== null) { $this->properties = $this->_trailer->Info->toPhp(); @@ -574,6 +575,25 @@ protected function _loadOutlines(Zend_Pdf_Element_Reference $root) } } + /** + * Load JavaScript + * Populates the _javaScript string, for later use of getJavaScript method. + * + * @param Zend_Pdf_Element_Reference $root Document catalog entry + */ + protected function _loadJavaScript(Zend_Pdf_Element_Reference $root) + { + if ($root->Names === null || $root->Names->JavaScript === null || $root->Names->JavaScript->Names === null) { + return; + } + + foreach ($root->Names->JavaScript->Names->items as $item) { + if ($item instanceof Zend_Pdf_Element_Reference && $item->S->value === 'JavaScript') { + $this->_javaScript[] = $item->JS->value; + } + } + } + /** * Orginize pages to tha pages tree structure. * @@ -1387,17 +1407,70 @@ public function render($newSegmentOnly = false, $outputStream = null) } } - /** - * Set the document-level JavaScript + * Sets the document-level JavaScript * - * @param string $javascript + * @param string|array $javascript */ public function setJavaScript($javascript) { - $this->_javaScript = $javascript; + $this->resetJavaScript(); + + $this->addJavaScript($javascript); + } + + /** + * Resets the document-level JavaScript + */ + public function resetJavaScript() + { + $this->_javaScript = NULL; + $root = $this->_trailer->Root; + + if ($root->Names === null || $root->Names->JavaScript === null) { + return; + } + $root->Names->JavaScript = NULL; } + /** + * Appends JavaScript to the document-level JavaScript + * + * @param string|array $javascript + */ + public function addJavaScript($javascript) + { + if (empty($javascript)) { + throw new Zend_Pdf_Exception('JavaScript must be a non empty string or array of strings'); + } else if (!is_array($javascript)) { + $javascript = array($javascript); + } + + if (NULL === $this->_javaScript) { + $this->_javaScript = $javascript; + } else { + $this->_javaScript = array_merge($this->_javaScript, $javascript); + } + + if (!empty($this->_javaScript)) { + $items = array(); + + foreach ($this->_javaScript as $javascript) { + $jsCode = array( + 'S' => new Zend_Pdf_Element_Name('JavaScript'), + 'JS' => new Zend_Pdf_Element_String($javascript) + ); + $items[] = new Zend_Pdf_Element_String('EmbeddedJS'); + $items[] = $this->_objFactory->newObject(new Zend_Pdf_Element_Dictionary($jsCode)); + } + + $jsRef = $this->_objFactory->newObject(new Zend_Pdf_Element_Dictionary(array('Names' => new Zend_Pdf_Element_Array($items)))); + if (NULL === $this->_trailer->Root->Names) { + $this->_trailer->Root->Names = new Zend_Pdf_Element_Dictionary(); + } + $this->_trailer->Root->Names->JavaScript = $jsRef; + } + } /** * Convert date to PDF format (it's close to ASN.1 (Abstract Syntax Notation diff --git a/tests/Zend/AllTests.php b/tests/Zend/AllTests.php index 348908ec4b..518f8c5ac5 100644 --- a/tests/Zend/AllTests.php +++ b/tests/Zend/AllTests.php @@ -76,6 +76,7 @@ require_once 'Zend/OpenIdTest.php'; require_once 'Zend/OpenId/AllTests.php'; require_once 'Zend/Paginator/AllTests.php'; +require_once 'Zend/PdfTest.php'; require_once 'Zend/Pdf/AllTests.php'; require_once 'Zend/ProgressBar/AllTests.php'; require_once 'Zend/Reflection/AllTests.php'; @@ -215,6 +216,7 @@ public static function suite() $suite->addTest(Zend_Navigation_AllTests::suite()); $suite->addTest(Zend_Oauth_AllTests::suite()); $suite->addTest(Zend_Paginator_AllTests::suite()); + $suite->addTestSuite('Zend_PdfTest'); $suite->addTest(Zend_Pdf_AllTests::suite()); $suite->addTest(Zend_ProgressBar_AllTests::suite()); $suite->addTestSuite('Zend_RegistryTest'); diff --git a/tests/Zend/PdfTest.php b/tests/Zend/PdfTest.php new file mode 100644 index 0000000000..21ad5d6146 --- /dev/null +++ b/tests/Zend/PdfTest.php @@ -0,0 +1,159 @@ +assertNull($pdf->getJavaScript()); + } + + public function testSetAndGetJavasriptArray() + { + // getting JavaScript after setting it returns array + $pdf = new Zend_Pdf(); + $pdf->setJavaScript('print();'); + $this->assertTrue(is_array($pdf->getJavaScript())); + } + + public function testSetJavaScriptString() + { + // setting string value is possible + $pdf = new Zend_Pdf(); + $javaScriptString = 'print();'; + $pdf->setJavaScript($javaScriptString); + $javaScript = $pdf->getJavaScript(); + $this->assertEquals($javaScriptString, $javaScript[0]); + } + + public function testSetJavaScriptArray() + { + // setting string value is possible + $pdf = new Zend_Pdf(); + $javaScriptArray = array('print();', 'alert();'); + $pdf->setJavaScript($javaScriptArray); + $this->assertEquals($javaScriptArray, $pdf->getJavaScript()); + } + + public function testResetJavaScript() + { + // reset removes the added JavaScript + $pdf = new Zend_Pdf(); + $pdf->setJavaScript('print();'); + $pdf->resetJavaScript(); + $this->assertNull($pdf->getJavaScript()); + } + + public function testAddJavaScript() + { + // adding JavaScript appends previously added JavaScript + $pdf = new Zend_Pdf(); + $javaScriptArray = array('print();', 'alert();'); + $pdf->addJavaScript($javaScriptArray[0]); + $pdf->addJavaScript($javaScriptArray[1]); + $this->assertEquals($javaScriptArray, $pdf->getJavaScript()); + } + + public function testSetJavaScriptEmptyString() + { + // setting empty JavaScript string throws exception + $pdf = new Zend_Pdf(); + try { + $pdf->setJavaScript(''); + $this->fail('Expected exception when trying to set empty string.'); + } catch (Zend_Pdf_Exception $e) { + $this->assertContains('JavaScript must be a non empty string or array of strings', $e->getMessage()); + } + } + + public function testSetJavaScriptEmptyArray() + { + // setting empty JavaScript string throws exception + $pdf = new Zend_Pdf(); + try { + $pdf->setJavaScript(array()); + $this->fail('Expected exception when trying to set empty array.'); + } catch (Zend_Pdf_Exception $e) { + $this->assertContains('JavaScript must be a non empty string or array of strings', $e->getMessage()); + } + } + + public function testSetAndSaveLoadAndGetJavaScript() + { + $tempFile = tempnam(sys_get_temp_dir(), 'PdfUnitFile'); + $javaScript = array('print();', 'alert();'); + + $pdf = new Zend_Pdf(); + $pdf->setJavaScript($javaScript); + $pdf->save($tempFile); + unset($pdf); + + $pdf = Zend_Pdf::load($tempFile); + unlink($tempFile); + + $this->assertEquals($javaScript, $pdf->getJavaScript()); + } + + public function testSetAndSaveLoadAndResetAndSaveLoadAndGetJavaScript() + { + $tempFile = tempnam(sys_get_temp_dir(), 'PdfUnitFile'); + $javaScript = array('print();', 'alert();'); + + $pdf = new Zend_Pdf(); + $pdf->setJavaScript($javaScript); + $pdf->save($tempFile); + unset($pdf); + + $pdf = Zend_Pdf::load($tempFile); + unlink($tempFile); + + $pdf->resetJavaScript(); + $pdf->save($tempFile); + unset($pdf); + + $pdf = Zend_Pdf::load($tempFile); + unlink($tempFile); + + $this->assertNull($pdf->getJavaScript()); + } + +}