public function testNewPageCreator() { $pdf = new Pdf\PdfDocument(); $page = $pdf->newPage(Pdf\Page::SIZE_A4); $this->assertTrue($page instanceof Pdf\Page); }
/** * Object constructor * * @param integer $objCount */ public function __construct($objNum, $genNum, $isFree, $dump = null) { $this->_objNum = $objNum; $this->_genNum = $genNum; $this->_isFree = $isFree; if ($dump !== null) { if (strlen($dump) > 1024) { $this->_dump = Pdf\PdfDocument::getMemoryManager()->create($dump); } else { $this->_dump = $dump; } } }
/** * @group ZF-8462 */ public function testPhpVersionBug() { try { $file = __DIR__ . '/_files/ZF-8462.pdf'; $pdf = Pdf\PdfDocument::load($file); } catch (Pdf\Exception $e) { if (strpos($e->getMessage(), 'Cross-reference streams are not supported yet.') !== false) { // Skip expected exception return; } throw $e; } }
/** * Object constructor. * Constructor signatures: * * 1. Load PDF page from a parsed PDF file. * Object factory is created by PDF parser. * --------------------------------------------------------- * new \Zend\Pdf\Page(\Zend\Pdf\InternalType\DictionaryObject $pageDict, * \Zend\Pdf\ObjectFactory $factory); * --------------------------------------------------------- * * 2. Make a copy of the PDF page. * New page is created in the same context as source page. Object factory is shared. * Thus it will be attached to the document, but need to be placed into Zend_Pdf::$pages array * to be included into output. * --------------------------------------------------------- * new \Zend\Pdf\Page(\Zend\Pdf\Page $page); * --------------------------------------------------------- * * 3. Create new page with a specified pagesize. * If $factory is null then it will be created and page must be attached to the document to be * included into output. * --------------------------------------------------------- * new \Zend\Pdf\Page(string $pagesize, \Zend\Pdf\ObjectFactory $factory = null); * --------------------------------------------------------- * * 4. Create new page with a specified pagesize (in default user space units). * If $factory is null then it will be created and page must be attached to the document to be * included into output. * --------------------------------------------------------- * new \Zend\Pdf\Page(numeric $width, numeric $height, * \Zend\Pdf\ObjectFactory $factory = null); * --------------------------------------------------------- * * * @param mixed $param1 * @param mixed $param2 * @param mixed $param3 * @throws \Zend\Pdf\Exception */ public function __construct($param1, $param2 = null, $param3 = null) { if (($param1 instanceof InternalType\IndirectObjectReference || $param1 instanceof InternalType\IndirectObject) && $param1->getType() == InternalType\AbstractTypeObject::TYPE_DICTIONARY && $param2 instanceof ObjectFactory && $param3 === null) { switch ($param1->getType()) { case InternalType\AbstractTypeObject::TYPE_DICTIONARY: $this->_pageDictionary = $param1; $this->_objFactory = $param2; $this->_attached = true; $this->_safeGS = false; return; break; case InternalType\AbstractTypeObject::TYPE_NULL: $this->_objFactory = $param2; $pageWidth = $pageHeight = 0; break; default: throw new Exception\CorruptedPdfException('Unrecognized object type.'); break; } } else { if ($param1 instanceof Page && $param2 === null && $param3 === null) { // Clone existing page. // Let already existing content and resources to be shared between pages // We don't give existing content modification functionality, so we don't need "deep copy" $this->_objFactory = $param1->_objFactory; $this->_attached =& $param1->_attached; $this->_safeGS = false; $this->_pageDictionary = $this->_objFactory->newObject(new InternalType\DictionaryObject()); foreach ($param1->_pageDictionary->getKeys() as $key) { if ($key == 'Contents') { // Clone Contents property $this->_pageDictionary->Contents = new InternalType\ArrayObject(); if ($param1->_pageDictionary->Contents->getType() != InternalType\AbstractTypeObject::TYPE_ARRAY) { // Prepare array of content streams and add existing stream $this->_pageDictionary->Contents->items[] = $param1->_pageDictionary->Contents; } else { // Clone array of the content streams foreach ($param1->_pageDictionary->Contents->items as $srcContentStream) { $this->_pageDictionary->Contents->items[] = $srcContentStream; } } } else { $this->_pageDictionary->{$key} = $param1->_pageDictionary->{$key}; } } return; } else { if (is_string($param1) && ($param2 === null || $param2 instanceof ObjectFactory) && $param3 === null) { if ($param2 !== null) { $this->_objFactory = $param2; } else { $this->_objFactory = ObjectFactory::createFactory(1); } $this->_attached = false; $this->_safeGS = true; /** New page created. That's users App responsibility to track GS changes */ switch (strtolower($param1)) { case 'a4': $param1 = self::SIZE_A4; break; case 'a4-landscape': $param1 = self::SIZE_A4_LANDSCAPE; break; case 'letter': $param1 = self::SIZE_LETTER; break; case 'letter-landscape': $param1 = self::SIZE_LETTER_LANDSCAPE; break; default: // should be in "x:y" or "x:y:" form } $pageDim = explode(':', $param1); if (count($pageDim) == 2 || count($pageDim) == 3) { $pageWidth = $pageDim[0]; $pageHeight = $pageDim[1]; } else { /** * @todo support of user defined pagesize notations, like: * "210x297mm", "595x842", "8.5x11in", "612x792" */ throw new Exception\Exception\InvalidArgumentException('Wrong pagesize notation.'); } /** * @todo support of pagesize recalculation to "default user space units" */ } else { if (is_numeric($param1) && is_numeric($param2) && ($param3 === null || $param3 instanceof ObjectFactory)) { if ($param3 !== null) { $this->_objFactory = $param3; } else { $this->_objFactory = ObjectFactory::createFactory(1); } $this->_attached = false; $this->_safeGS = true; /** New page created. That's users App responsibility to track GS changes */ $pageWidth = $param1; $pageHeight = $param2; } else { throw new Exception\BadMethodCallException('Unrecognized method signature, wrong number of arguments or wrong argument types.'); } } } } $this->_pageDictionary = $this->_objFactory->newObject(new InternalType\DictionaryObject()); $this->_pageDictionary->Type = new InternalType\NameObject('Page'); $this->_pageDictionary->LastModified = new InternalType\StringObject(PdfDocument::pdfDate()); $this->_pageDictionary->Resources = new InternalType\DictionaryObject(); $this->_pageDictionary->MediaBox = new InternalType\ArrayObject(); $this->_pageDictionary->MediaBox->items[] = new InternalType\NumericObject(0); $this->_pageDictionary->MediaBox->items[] = new InternalType\NumericObject(0); $this->_pageDictionary->MediaBox->items[] = new InternalType\NumericObject($pageWidth); $this->_pageDictionary->MediaBox->items[] = new InternalType\NumericObject($pageHeight); $this->_pageDictionary->Contents = new InternalType\ArrayObject(); }
/** * Object constructor * * @param string $val */ public function __construct($val) { $this->value = Pdf\PdfDocument::getMemoryManager()->create($val); }
/** * @group ZF-8462 */ public function testPhpVersionBug() { $this->setExpectedException('\\Zend\\Pdf\\Exception\\NotImplementedException', 'Cross-reference streams are not supported yet'); $pdf = Pdf\PdfDocument::load(__DIR__ . '/_files/ZF-8462.pdf'); }
public function testFontExtracting() { if (PHP_OS == 'AIX') { $this->markTestSkipped('Not supported on AIX'); } $pdf = new Pdf\PdfDocument(); $fontsList = array(Pdf\Font::FONT_COURIER, Pdf\Font::FONT_HELVETICA_BOLD, Pdf\Font::FONT_TIMES_BOLD_ITALIC); foreach ($fontsList as $fontName) { // Add new page generated by Zend_PDF object (page is attached to the specified the document) $pdf->pages[] = $page = $pdf->newPage(Pdf\Page::SIZE_A4_LANDSCAPE); $font = Pdf\Font::fontWithName($fontName); $page->setFont($font, 10)->drawText($font->getFontName(Pdf\Font::NAME_POSTSCRIPT, 'en') . ':', 100, 400); $page->setFont($font, 20)->drawText("'The quick brown fox jumps over the lazy dog'", 100, 360); $type = $font->getFontType(); } $TTFFontsList = array('VeraBd.ttf', 'VeraBI.ttf', 'VeraIt.ttf', 'VeraMoBd.ttf', 'VeraMoBI.ttf', 'VeraMoIt.ttf', 'VeraMono.ttf', 'VeraSeBd.ttf', 'VeraSe.ttf', 'Vera.ttf'); foreach ($TTFFontsList as $fontName) { // Add new page generated by Zend_PDF object (page is attached to the specified the document) $pdf->pages[] = $page = $pdf->newPage(Pdf\Page::SIZE_A4_LANDSCAPE); $font = Pdf\Font::fontWithPath(__DIR__ . '/_fonts/' . $fontName); $page->setFont($font, 10)->drawText($font->getFontName(Pdf\Font::NAME_POSTSCRIPT, 'en', 'CP1252') . ':', 100, 400); $page->setFont($font, 20)->drawText("'The quick brown fox jumps over the lazy dog'", 100, 360); $type = $font->getFontType(); } $pdf->save(__DIR__ . '/_files/output.pdf'); unset($pdf); $pdf1 = Pdf\PdfDocument::load(__DIR__ . '/_files/output.pdf'); $newPages = array(); $fontList = array(); $fontNames = array(); foreach ($pdf1->pages as $page) { $pageFonts = $page->extractFonts(); foreach ($pageFonts as $font) { $fontList[] = $font; $fontNames[] = $font->getFontName(Pdf\Font::NAME_POSTSCRIPT, 'en', 'UTF-8'); } } $this->assertEquals(array(Pdf\Font::FONT_COURIER, Pdf\Font::FONT_HELVETICA_BOLD, Pdf\Font::FONT_TIMES_BOLD_ITALIC, 'BitstreamVeraSans-Bold', 'BitstreamVeraSans-BoldOblique', 'BitstreamVeraSans-Oblique', 'BitstreamVeraSansMono-Bold', 'BitstreamVeraSansMono-BoldOb', 'BitstreamVeraSansMono-Oblique', 'BitstreamVeraSansMono-Roman', 'BitstreamVeraSerif-Bold', 'BitstreamVeraSerif-Roman', 'BitstreamVeraSans-Roman'), $fontNames); $pdf1->pages[] = $page = $pdf1->newPage(Pdf\Page::SIZE_A4); $yPosition = 700; foreach ($fontList as $font) { $page->setFont($font, 15)->drawText("The quick brown fox jumps over the lazy dog", 100, $yPosition); $yPosition -= 30; } $fontNames1 = array(); foreach ($pdf1->extractFonts() as $font) { $fontNames1[] = $font->getFontName(Pdf\Font::NAME_POSTSCRIPT, 'en', 'UTF-8'); } $this->assertEquals(array(Pdf\Font::FONT_COURIER, Pdf\Font::FONT_HELVETICA_BOLD, Pdf\Font::FONT_TIMES_BOLD_ITALIC, 'BitstreamVeraSans-Bold', 'BitstreamVeraSans-BoldOblique', 'BitstreamVeraSans-Oblique', 'BitstreamVeraSansMono-Bold', 'BitstreamVeraSansMono-BoldOb', 'BitstreamVeraSansMono-Oblique', 'BitstreamVeraSansMono-Roman', 'BitstreamVeraSerif-Bold', 'BitstreamVeraSerif-Roman', 'BitstreamVeraSans-Roman'), $fontNames1); $page = reset($pdf1->pages); $font = $page->extractFont(Pdf\Font::FONT_COURIER); $this->assertTrue($font instanceof Font\Extracted); $font = $page->extractFont(Pdf\Font::FONT_TIMES_BOLD_ITALIC); $this->assertNull($font); $font = $pdf1->extractFont(Pdf\Font::FONT_TIMES_BOLD_ITALIC); $this->assertTrue($font instanceof Font\Extracted); $font = $pdf1->extractFont(Pdf\Font::FONT_TIMES_ROMAN); $this->assertNull($font); $pdf1->save(__DIR__ . '/_files/output1.pdf'); unset($pdf1); $pdf2 = Pdf\PdfDocument::load(__DIR__ . '/_files/output1.pdf'); $this->assertTrue($pdf2 instanceof Pdf\PdfDocument); unset($pdf2); unlink(__DIR__ . '/_files/output.pdf'); unlink(__DIR__ . '/_files/output1.pdf'); }
public function testCreate() { $pdf = new Pdf\PdfDocument(); $page1 = $pdf->newPage(Pdf\Page::SIZE_A4); $page2 = $pdf->newPage(Pdf\Page::SIZE_A4); $destination = Destination\Zoom::create($page2, 0, 842, 0.5); $this->assertTrue($destination instanceof Destination\Zoom); $this->assertEquals($destination->getResource()->toString(), '[4 0 R /XYZ 0 842 0.5 ]'); $destination = Destination\Fit::create($page2); $this->assertTrue($destination instanceof Destination\Fit); $this->assertEquals($destination->getResource()->toString(), '[4 0 R /Fit ]'); $destination = Destination\FitHorizontally::create($page2, 842); $this->assertTrue($destination instanceof Destination\FitHorizontally); $this->assertEquals($destination->getResource()->toString(), '[4 0 R /FitH 842 ]'); $destination = Destination\FitVertically::create(2, 0); $this->assertTrue($destination instanceof Destination\FitVertically); $this->assertEquals($destination->getResource()->toString(), '[2 /FitV 0 ]'); $destination = Destination\FitRectangle::create($page1, 0, 10, 595, 842); $this->assertTrue($destination instanceof Destination\FitRectangle); $this->assertEquals($destination->getResource()->toString(), '[3 0 R /FitR 0 10 595 842 ]'); $destination = Destination\FitBoundingBox::create(1); $this->assertTrue($destination instanceof Destination\FitBoundingBox); $this->assertEquals($destination->getResource()->toString(), '[1 /FitB ]'); $destination = Destination\FitBoundingBoxHorizontally::create($page2, 842); $this->assertTrue($destination instanceof Destination\FitBoundingBoxHorizontally); $this->assertEquals($destination->getResource()->toString(), '[4 0 R /FitBH 842 ]'); $destination = Destination\FitBoundingBoxVertically::create($page2, 0); $this->assertTrue($destination instanceof Destination\FitBoundingBoxVertically); $this->assertEquals($destination->getResource()->toString(), '[4 0 R /FitBV 0 ]'); }
public function testPageCloning() { $pdf = Pdf\PdfDocument::load(__DIR__ . '/_files/pdfarchiving.pdf'); $pdf1 = new Pdf\PdfDocument(); $srcPageCount = count($pdf->pages); $outputPageSet = array(); foreach ($pdf->pages as $srcPage) { $page = clone $srcPage; $page->saveGS(); // Create new Style $page->setFillColor(new Color\Rgb(0, 0, 0.9))->setLineColor(new Color\GrayScale(0.2))->setLineWidth(3)->setLineDashingPattern(array(3, 2, 3, 4), 1.6)->setFont(Pdf\Font::fontWithName(Pdf\Font::FONT_HELVETICA_BOLD), 32); $page->rotate(0, 0, M_PI_2 / 3); $page->drawText('Modified by Zend Framework!', 150, 0); $page->restoreGS(); $pdf1->pages[] = $page; } $pdf1->save(__DIR__ . '/_files/output.pdf'); unset($pdf); unset($pdf1); $pdf2 = Pdf\PdfDocument::load(__DIR__ . '/_files/output.pdf'); $this->assertTrue($pdf2 instanceof Pdf\PdfDocument); $this->assertEquals($srcPageCount, count($pdf2->pages)); unset($pdf2); unlink(__DIR__ . '/_files/output.pdf'); }
/** * Draw the barcode in the PDF, send headers and the PDF * @return mixed */ public function render() { $this->draw(); header("Content-Type: application/pdf"); echo $this->_resource->render(); }
public function testProcessing() { $pdf = new Pdf\PdfDocument(); $page1 = $pdf->newPage(Pdf\Page::SIZE_A4); $page2 = $pdf->newPage(Pdf\Page::SIZE_A4); $page3 = $pdf->newPage(Pdf\Page::SIZE_A4); // not actually included into pages array $pdf->pages[] = $page1; $pdf->pages[] = $page2; $this->assertTrue(count($pdf->getNamedDestinations()) == 0); $destination1 = Destination\Fit::create($page1); $destination2 = Destination\Fit::create($page2); $action1 = Action\GoToAction::create($destination1); $pdf->setNamedDestination('GoToPage1', $action1); $this->assertTrue($pdf->getNamedDestination('GoToPage1') === $action1); $this->assertTrue($pdf->getNamedDestination('GoToPage9') === null); $pdf->setNamedDestination('Page2', $destination2); $this->assertTrue($pdf->getNamedDestination('Page2') === $destination2); $this->assertTrue($pdf->getNamedDestination('Page9') === null); $pdf->setNamedDestination('Page1', $destination1); $pdf->setNamedDestination('Page1_1', Destination\Fit::create(1)); $pdf->setNamedDestination('Page9_1', Destination\Fit::create(9)); // will be egnored $action3 = Action\GoToAction::create(Destination\Fit::create($page3)); $pdf->setNamedDestination('GoToPage3', $action3); $this->assertTrue(strpos($pdf->render(), '[(GoToPage1) <</Type /Action /S /GoTo /D [3 0 R /Fit ] >> (Page1) [3 0 R /Fit ] (Page1_1) [1 /Fit ] (Page2) [4 0 R /Fit ] ]') !== false); }
public function testPageCloning() { $pdf = Pdf\PdfDocument::load(__DIR__ . '/_files/pdfarchiving.pdf'); $srcPageCount = count($pdf->pages); try { $newPage = clone reset($pdf->pages); } catch (Pdf\Exception $e) { if (strpos($e->getMessage(), 'Cloning \\Zend\\Pdf\\Page object using \'clone\' keyword is not supported.') !== 0) { throw $e; } // Exception is thrown } $outputPageSet = array(); foreach ($pdf->pages as $srcPage) { $page = new Pdf\Page($srcPage); $outputPageSet[] = $srcPage; $outputPageSet[] = $page; $page->saveGS(); // Create new Style $page->setFillColor(new Color\Rgb(0, 0, 0.9))->setLineColor(new Color\GrayScale(0.2))->setLineWidth(3)->setLineDashingPattern(array(3, 2, 3, 4), 1.6)->setFont(Pdf\Font::fontWithName(Pdf\Font::FONT_HELVETICA_BOLD), 32); $page->rotate(0, 0, M_PI_2 / 3); $page->drawText('Modified by Zend Framework!', 150, 0); $page->restoreGS(); } // Add new page generated by Zend_PDF object (page is attached to the specified the document) $pdf->pages = $outputPageSet; $pdf->save(__DIR__ . '/_files/output.pdf'); unset($pdf); $pdf1 = Pdf\PdfDocument::load(__DIR__ . '/_files/output.pdf'); $this->assertTrue($pdf1 instanceof Pdf\PdfDocument); $this->assertEquals($srcPageCount * 2, count($pdf1->pages)); unset($pdf1); unlink(__DIR__ . '/_files/output.pdf'); }