public function testImplodePdfsReturnNotEmptyPdf() { /** @var $pdf \ZendPdf\PdfDocument */ $pdf = implodePdfs(array(\ZendPdf\PdfDocument::load(__DIR__ . '/data/pdf1.pdf'), \ZendPdf\PdfDocument::load(__DIR__ . '/data/pdf2.pdf'))); $this->assertGreaterThan(20000, strlen($pdf->render())); $this->assertSame(3, count($pdf->pages)); }
/** * Generates certificate of exellence and * triggers an event when the file is generated * @param \User\Model\Entity\User $user * @param string $examName */ public function generateCertificate($user, $examName) { $config = $this->services->get('config'); $pdf = PdfDocument::load($config['pdf']['exam_certificate']); // get the first page $page = $pdf->pages[0]; // Extract the AdineKirnberg-Script font included in the PDF sample $font = $page->extractFont('AdineKirnberg-Script'); $page->setFont($font, 80); // and write the name of the user with it $page->drawText($user->getName(), 200, 280); // after that use Time Bold to write the name of the exam $font = PdfFont::fontWithName(PdfFont::FONT_TIMES_BOLD); $page->setFont($font, 40); $page->drawText($examName, 200, 120); // We use the png image from the public/images folder $imageFile = 'public/images/zf2-logo.png'; // get the right size to do some calculations $size = getimagesize($imageFile); // load the image $image = \ZendPdf\Image::imageWithPath($imageFile); $x = 580; $y = 440; // and finally draw the image $page->drawImage($image, $x, $y, $x + $size[0], $y + $size[1]); return $pdf; }
/** * @param $file */ public function loadFromFile($file) { if (file_exists($file)) { $pdf = \ZendPdf\PdfDocument::load($file); foreach ($pdf->pages as $page) { $this->pdf->pages[] = clone $page; } } }
* * @category Default * @package None * @author Kei Nakayama <*****@*****.**> * @license MIT https://opensource.org/licenses/MIT * @link None */ require_once __DIR__ . "/../vendor/autoload.php"; use ZendPdf\PdfDocument; if (count($argv) < 3) { echo "USAGE: php {$argv['0']} " . "<pdf_file1> <pdf_file2> [<pdf_file3> [...]] <output_pdf_file>" . PHP_EOL; exit; } $pdfs = []; for ($i = 1; $i <= count($argv) - 2; $i++) { echo "loading: " . $argv[$i] . PHP_EOL; $pdfs[] = PdfDocument::load($argv[$i]); echo "loaded: " . $argv[$i] . PHP_EOL; } $outPdfFn = $argv[count($argv) - 1]; echo "creating: " . $outPdfFn . PHP_EOL; $outPdf = new PdfDocument(); foreach ($pdfs as $pdf) { foreach ($pdf->pages as $page) { $outPdf->pages[] = clone $page; } } echo "saving: " . $outPdfFn . PHP_EOL; $outPdf->save($outPdfFn); echo "created: " . $outPdfFn . PHP_EOL; exit;
/** * Test capture request saves file to * disk with correct orientation. * * @access public * @return void */ public function testPdfRequestSavesFileToDiskWithCorrectOrientation() { $this->filename = 'test.pdf'; $file = $this->directory . '/' . $this->filename; $client = $this->getClient(); $request = $client->getMessageFactory()->createPdfRequest(); $response = $client->getMessageFactory()->createResponse(); $request->setMethod('GET'); $request->setUrl('http://jonnyw.kiwi/tests/test-capture.php'); $request->setOutputFile($file); $request->setFormat('A4'); $request->setOrientation('landscape'); $request->setMargin('0cm'); $client->send($request, $response); $pdf = \ZendPdf\PdfDocument::load($file); $pdfWidth = round($pdf->pages[0]->getWidth() * 0.0352777778); $pdfHeight = round($pdf->pages[0]->getHeight() * 0.0352777778); $this->assertEquals(30, $pdfWidth); $this->assertEquals(21, $pdfHeight); }
/** * If the book defines a custom PDF cover, this method prepends it * to the PDF book. * * @param string $bookFilePath The path of the original PDF book without the cover * @param string $coverFilePath The path of the PDF file which will be displayed as the cover of the book */ public function addBookCover($bookFilePath, $coverFilePath) { if (!empty($coverFilePath)) { $pdfBook = PdfDocument::load($bookFilePath); $pdfCover = PdfDocument::load($coverFilePath); $pdfCover = clone $pdfCover->pages[0]; array_unshift($pdfBook->pages, $pdfCover); $pdfBook->save($bookFilePath, true); } }
public function loadEngine($file, $encoding) { if (isset(self::$loadedEngines[$file])) { return self::$loadedEngines[$file]; } if (!is_readable($file)) { throw InvalidResourceException::fileDosntExistException($file); } try { $pdf = PdfDocument::load($file); $engine = new self($pdf, $this->unitConverter); foreach ($pdf->pages as $page) { $gc = new GraphicsContext($engine, $page, $encoding); $engine->attachGraphicsContext($gc); } self::$loadedEngines[$file] = $engine; return $engine; } catch (\ZendPdf\Exception $e) { throw InvalidResourceException::invalidPdfFileException($file, $e); } }
public function testAddBookCover() { $app = new Application(); $tmpDir = $app['app.dir.cache'] . '/' . uniqid('phpunit_'); $app['filesystem']->mkdir($tmpDir); $coverFilePath = $tmpDir . '/cover.pdf'; $bookFilePath = $tmpDir . '/book.pdf'; $this->createPdfFile($coverFilePath, 'EASYBOOK COVER'); $this->createPdfFile($bookFilePath, 'easybook contents'); $publisher = new PdfPublisher($app); $publisher->addBookCover($bookFilePath, $coverFilePath); $resultingPdfBook = PdfDocument::load($bookFilePath); $this->assertCount(2, $resultingPdfBook->pages, 'The cover page has been added to the book.'); $this->assertFileExists($coverFilePath, 'The cover PDF file is NOT deleted after adding it to the book.'); $this->assertContains('EASYBOOK COVER', $resultingPdfBook->render(), 'The resulting book contains the cover text.'); $this->assertContains('easybook contents', $resultingPdfBook->render(), 'The resulting book contains the original book contents.'); $app['filesystem']->remove($tmpDir); }
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'); }
/** * @group ZF-8462 */ public function testPhpVersionBug() { $this->setExpectedException('\\ZendPdf\\Exception\\NotImplementedException', 'Cross-reference streams are not supported yet'); $pdf = Pdf\PdfDocument::load(__DIR__ . '/_files/ZF-8462.pdf'); }
/** * Create PDF file * * @param string $filename Filename * * @return mixed Returns PDF instance on success or FALSE on failure. */ public function open_file($filename) { $this->_filename = $filename; if (file_exists($filename)) { try { $this->_zpdf = Zend_Pdf::load($filename); } catch (\Exception $e) { $this->_errmsg = $e->getMessage(); return false; } } else { $this->_zpdf = new Zend_Pdf(); } return true; }