public function pdfAction() { $iso2 = $this->params()->fromRoute('iso2'); $table = $this->getServiceLocator()->get('city-codes-table'); $viewModel = new ViewModel(array('cityList' => $table->getListByCountry($iso2), 'countryCode' => $iso2)); $viewModel->setTemplate('form-demo/application/index/pdf-cities'); $viewRender = $this->getServiceLocator()->get('ViewRenderer'); $list = explode(PHP_EOL, $viewRender->render($viewModel)); $font = Font::fontWithPath('/usr/share/fonts/truetype/freefont/FreeMono.ttf'); $pdf = new PdfDocument(); // top left 1" from top 1" from left $x = 72; $y = 720; $dec = 18; foreach ($list as $index => $row) { if (substr($row, 0, 1) == '=' || $y <= 72) { if ($index > 0) { $pdf->pages[] = $page; } $page = new Page(Page::SIZE_LETTER); $page->setFont($font, 12); $y = 720; } $page->drawText($row, $x, $y); $y -= $dec; } $pdf->pages[] = $page; // retrieve the response object $response = $this->getResponse(); // set the header $response->getHeaders()->addHeaders(array('Content-Type' => 'application/pdf')); $response->setContent($pdf->render()); return $response; }
/** * Output text at given position * Prints text in the current font. * * @param string $text Text * @param float $x X * @param float $y Y * * @return bool Returns TRUE on success or FALSE on failure. */ public function show_xy($text, $x, $y) { if (!$this->_page) { $this->_errmsg = 'No Page'; return false; } try { $this->_page->drawText($text, $x, $y, $this->_encoding); $this->_x = $x; $this->_y = $y; return true; } catch (\Exception $e) { $this->_errmsg = $e->getMessage(); } return false; }
/** * Wrapper taking custom units and margins into account * @see \ZendPdf\Page::drawText() for documentation */ public function drawText($text, $x, $y, $charEncoding = 'WINDOWS-1252') { $this->convertCoordinatesFromUserSpace($x, $y); $y -= $this->getFontSize(); return parent::drawText($text, $x, $y, $charEncoding); }
public function drawHtml(Page $page, $html, $x1, $y1, $x2 = null, $y2 = null, $lineHeight = 1.3, $charEncoding = '', $debug = false) { if (is_null($x2)) { $x2 = $page->getWidth(); } if (is_null($y2)) { $y2 = 0; } if ($debug === true) { var_dump(array('x1' => $x1, 'y1' => $y1, 'x2' => $x2, 'y2' => $y2)); } $x = $x1; $y = $y1; if (is_null($page->getFont())) { $page->setFont(Font::fontWithName(Font::FONT_HELVETICA), 10); } $defaultFont = $page->getFont(); $defaultfontSize = $page->getFontSize(); $maxFontSize = $defaultfontSize; $words = array(); $elements = $this->getParser()->parse($html); if ($debug === true) { var_dump($elements); } foreach ($elements as $element) { if ($element instanceof DataElement) { $font = $element->getFont($defaultFont); $fontSize = $element->getFontSize($defaultfontSize); $maxFontSize = $fontSize > $maxFontSize ? $fontSize : $maxFontSize; $rawWords = explode(' ', $element->getValue()); foreach ($rawWords as $rawWord) { $rawWord .= $rawWord ? ' ' : ''; $wordWith = $this->widthForStringUsingFontSize($rawWord, $font, $fontSize); if ($x + $wordWith > $x2) { $x = $x1; if ($y1 < $y2) { $y += $maxFontSize * $lineHeight; } else { $y -= $maxFontSize * $lineHeight; } $maxFontSize = $fontSize; } $words[] = new Word($rawWord, $x, $y, $font, $fontSize); $x += $wordWith; } } elseif ($element instanceof ControllElement) { if ($element->isBlockElement()) { if ($x != $x1) { $x = $x1; if ($y1 < $y2) { $y += $maxFontSize * $lineHeight; } else { $y -= $maxFontSize * $lineHeight; } $maxFontSize = $defaultfontSize; } if ($element instanceof StartElement) { if (!is_null($element->marginTop())) { if ($y1 < $y2) { $y += $element->marginTop(); } else { $y -= $element->marginTop(); } } if (!is_null($element->marginLeft())) { if (!is_null($element->getListSign())) { $words[] = new Word($element->getListSign(), $x, $y, $element->getFont($defaultFont), $element->getFontSize($defaultfontSize)); } $x1 += $element->marginLeft(); $x = $x1; } } if ($element instanceof StopElement) { if (!is_null($element->marginBottom())) { if ($y1 < $y2) { $y += $element->marginBottom(); } else { $y -= $element->marginBottom(); } } if (!is_null($element->marginLeft())) { $x1 -= $element->marginLeft(); $x = $x1; } } } } } if ($debug === true) { var_dump($words); } foreach ($words as $word) { /** @var Word $word */ $page->setFont($word->getFont(), $word->getFontSize()); $page->drawText($word->getText(), $word->getX(), $word->getY(), $charEncoding); } if ($debug === true) { die; } return array($x, $y); }
private function createPdfFile($filePath, $contents) { $pdf = new PdfDocument(); $page = new Page(Page::SIZE_A4); $font = Font::fontWithName(Font::FONT_HELVETICA); $page->setFont($font, 18); $page->drawText($contents, 50, 780); $pdf->pages[] = $page; $pdf->save($filePath); }
public function testPageDuplicating() { $pdf = Pdf\PdfDocument::load(__DIR__ . '/_files/pdfarchiving.pdf'); $srcPageCount = count($pdf->pages); $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'); }
/** * @{inheritDoc} */ public function drawTextOnPage(Page $page, $text, $left, $bottom, $encoding = 'UTF-8') { return $page->drawText($text, $left, $bottom, $encoding); }