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; }
/** * Set font * * @param string $font Font * @param int $size Font size * @param string $encoding Encoding * * @return bool Returns TRUE on success or FALSE on failure. */ public function set_font($font, $size, $encoding) { $this->_fontSize = $size; $fonts = array('courier' => Zend_Pdf_Font::FONT_COURIER, 'courier-bold' => Zend_Pdf_Font::FONT_COURIER_BOLD, 'courier-oblique' => Zend_Pdf_Font::FONT_COURIER_ITALIC, 'courier-italic' => Zend_Pdf_Font::FONT_COURIER_ITALIC, 'courier-bold-italic' => Zend_Pdf_Font::FONT_COURIER_BOLD_ITALIC, 'courier-boldoblique' => Zend_Pdf_Font::FONT_COURIER_BOLD_ITALIC, 'times' => Zend_Pdf_Font::FONT_TIMES, 'times-bold' => Zend_Pdf_Font::FONT_TIMES_BOLD, 'times-italic' => Zend_Pdf_Font::FONT_TIMES_ITALIC, 'times-bold-italic' => Zend_Pdf_Font::FONT_TIMES_BOLD_ITALIC, 'times-bolditalic' => Zend_Pdf_Font::FONT_TIMES_BOLD_ITALIC, 'times-roman' => Zend_Pdf_Font::FONT_TIMES, 'times-roman-bold' => Zend_Pdf_Font::FONT_TIMES_BOLD, 'times-roman-italic' => Zend_Pdf_Font::FONT_TIMES_ITALIC, 'times-roman-bold-italic' => Zend_Pdf_Font::FONT_TIMES_BOLD_ITALIC, 'helvetica' => Zend_Pdf_Font::FONT_HELVETICA, 'helvetica-bold' => Zend_Pdf_Font::FONT_HELVETICA_BOLD, 'helvetica-italic' => Zend_Pdf_Font::FONT_HELVETICA_ITALIC, 'helvetica-oblique' => Zend_Pdf_Font::FONT_HELVETICA_ITALIC, 'helvetica-bold-italic' => Zend_Pdf_Font::FONT_HELVETICA_BOLD_ITALIC, 'helvetica-boldoblique' => Zend_Pdf_Font::FONT_HELVETICA_BOLD_ITALIC, 'symbol' => Zend_Pdf_Font::FONT_SYMBOL, 'zapfdingbats' => Zend_Pdf_Font::FONT_ZAPFDINGBATS); try { $font = isset($fonts[strtolower($font)]) ? $fonts[strtolower($font)] : false; if ($font === false) { $this->_errmsg = "could not find font '{$font}'"; return false; } $this->_font = Zend_Pdf_Font::fontWithName($font); if ($this->_page) { $this->_page->setFont($this->_font, $size); } return true; } catch (\Exception $e) { $this->_errmsg = $e->getMessage(); } return false; }
/** * Change the font size, without changing the font family * * @param float $fontSize new font size to use */ public function setFontSize($fontSize) { return parent::setFont($this->getFont(), $fontSize); }
/** * @param ZendPage $page * @param PathText $element * * @throws \Exception */ private function drawPathText(ZendPage $page, PathText $element) { $fontStyle = $element->getFontStyle(); $font = $this->getZendFont($fontStyle); $anchorPath = new AnchorPath($element->getPath()); list($chars, $pathPos, $yShift) = $this->preparePathText($element->getText(), $anchorPath, $font, $fontStyle); $page->saveGS(); $page->setFont($font, $fontStyle->getSize()); foreach ($chars as list($char, $width)) { $anchor = $anchorPath->getAnchor($pathPos + $width / 2.0); $pathPos += $width; if (null === $anchor) { // skip chars out of path continue; } $page->saveGS(); $this->translateAndRotateToAnchor($page, $anchor); $this->drawFormattedText($page, $char, -$width / 2.0, -$yShift, $element); $page->restoreGS(); } $page->restoreGS(); }
private function generatePdf(Invoice $invoice) { $pdf = new PdfDocument(); // Add new page generated by ZendPdf\Page object $page1 = new Page(Page::SIZE_A4); $pdf->pages[] = $page1; // Load the needed fonts $fontHelvetica = Font::fontWithName(Font::FONT_HELVETICA); $fontHelveticaBold = Font::fontWithName(Font::FONT_HELVETICA_BOLD); $fontTimes = Font::fontWithName(Font::FONT_TIMES); $fontTimesBold = Font::fontWithName(Font::FONT_TIMES_BOLD); $colorWhite = new \ZendPdf\Color\GrayScale(1); $colorBlack = new \ZendPdf\Color\GrayScale(0); $pdf->properties['Author'] = 'Marinvägen IT'; $pdf->properties['Creator'] = 'Marinvägen IT'; $pdf->properties['Title'] = 'Invoice'; $pdf->properties['Subject'] = 'Invoice'; $page1->setFillColor($colorWhite)->setLineColor($colorBlack)->drawRectangle(50, 747, 295, 787)->drawRectangle(300, 747, 545, 787)->drawRectangle(50, 622, 295, 742)->drawRectangle(300, 622, 545, 742)->drawRectangle(50, 95, 545, 617)->drawRectangle(50, 597, 545, 617)->drawRectangle(50, 30, 545, 90); // Draw header $page1->setFont($fontTimes, 10)->setFillColor($colorBlack)->drawText('Faktura nr', 305, 775)->drawText('Kund nr', 540 - $this->widthForStringUsingFontSize('Kund nr', $fontTimes, 10), 775)->drawText('Fakturadatum', 55, 775)->drawText('Förfallodatum', 290 - $this->widthForStringUsingFontSize('Förfallodatum', $fontTimes, 10), 775)->drawText('Fakturaadress', 305, 730)->drawText('Er referens:', 55, 730)->drawText($invoice->getClientReference(), 290 - $this->widthForStringUsingFontSize($invoice->getClientReference(), $fontTimes, 10), 730)->drawText('Vår referens:', 55, 719)->drawText($invoice->getOwnReference(), 290 - $this->widthForStringUsingFontSize($invoice->getOwnReference(), $fontTimes, 10), 719)->drawText('Betalningsvillkor:', 55, 708)->drawText($invoice->getDueDays() . ' dagar netto', 290 - $this->widthForStringUsingFontSize($invoice->getDueDays() . ' dagar netto', $fontTimes, 10), 708)->drawText('Dröjsmålsränta:', 55, 697)->drawText('9,50 %', 290 - $this->widthForStringUsingFontSize('9,50 %', $fontTimes, 10), 697)->drawText('Innehar F-skattebevis', 290 - $this->widthForStringUsingFontSize('Innehar F-skattebevis', $fontTimes, 10), 627)->drawText('Adress:', 55, 80)->drawText('Telefon:', 178, 80)->drawText('Bankgiro:', 178, 52)->drawText('E-post:', 301, 80)->drawText('Organisationsnr:', 301, 52)->drawText('Hemsida:', 424, 80)->drawText('Momsreg.nr:', 424, 52)->drawText('Netto', 125 - $this->widthForStringUsingFontSize('Netto', $fontTimes, 10), 122)->drawText('Moms kr', 330 - $this->widthForStringUsingFontSize('Moms kr', $fontTimes, 10), 122)->drawText('Avrundning', 410 - $this->widthForStringUsingFontSize('Avrundning', $fontTimes, 10), 122); // Footer texts $page1->setFont($fontTimes, 12)->setFillColor($colorBlack)->drawText($this->settingTable->getSettingByName('CompanyName'), 55, 68)->drawText($this->settingTable->getSettingByName('CompanyAddress'), 55, 56)->drawText($this->settingTable->getSettingByName('CompanyPostAddress'), 55, 44)->drawText($this->settingTable->getSettingByName('CompanyCellphone'), 178, 68)->drawText($this->settingTable->getSettingByName('CompanyBankGiro'), 178, 40)->drawText($this->settingTable->getSettingByName('CompanyEmail'), 301, 68)->drawText($this->settingTable->getSettingByName('CompanyRegistrationNumber'), 301, 40)->drawText($this->settingTable->getSettingByName('CompanyWebpage'), 424, 68)->drawText($this->settingTable->getSettingByName('CompanyVATNumber'), 424, 40); // Invoice headers $page1->setFont($fontTimes, 14)->setFillColor($colorBlack)->drawText('Benämning', 55, 603)->drawText('Antal', 350 - $this->widthForStringUsingFontSize('Antal', $fontTimes, 14), 603)->drawText('á-pris', 450 - $this->widthForStringUsingFontSize('á-pris', $fontTimes, 14), 603)->drawText('Summa', 540 - $this->widthForStringUsingFontSize('Summa', $fontTimes, 14), 603); // Invoice summary text $page1->setFont($fontTimes, 16)->setFillColor($colorBlack)->drawText('ATT BETALA', 540 - $this->widthForStringUsingFontSize('ATT BETALA', $fontTimes, 16), 126)->drawText($invoice->getNumber(), 310, 755)->drawText($invoice->getClientId(), 535 - $this->widthForStringUsingFontSize($invoice->getClientId(), $fontTimes, 16), 755)->drawText(date("Y-m-d", strtotime($invoice->getBaseDate())), 60, 755)->drawText(date("Y-m-d", strtotime($invoice->getBaseDate() . ' +' . $invoice->getDueDays() . ' days')), 285 - $this->widthForStringUsingFontSize($invoice->getBaseDate(), $fontTimes, 16), 755); // Draw header $page1->setFont($fontTimes, 22)->setFillColor($colorBlack)->drawText('Faktura', 478, 792); // Draw company name $page1->setFont($fontHelveticaBold, 32)->setFillColor($colorBlack)->drawText($this->settingTable->getSettingByName('CompanyName'), 50, 792); $intPosition = 582; $intTotalSum = 0; $intTotalVat = 0; $intTotalSumWithVat = 0; foreach ($invoice->getRows() as $row) { $intRowPrice = number_format($row->getPrice(), 2, ',', ' '); $intRowSum = number_format($row->getQuantity() * $row->getPrice(), 2, ',', ' '); $intTotalSum = $intTotalSum + $row->getQuantity() * $row->getPrice(); $intTotalVat = $intTotalVat + $row->getQuantity() * $row->getPrice() * ($row->getVat() / 100); $page1->setFont($fontTimes, 12)->setFillColor($colorBlack)->drawText($row->getText(), 55, $intPosition)->drawText($row->getQuantity(), 350 - $this->widthForStringUsingFontSize($row->getQuantity(), $fontTimes, 12), $intPosition)->drawText($intRowPrice, 450 - $this->widthForStringUsingFontSize($intRowPrice, $fontTimes, 12), $intPosition)->drawText($intRowSum, 540 - $this->widthForStringUsingFontSize($intRowSum, $fontTimes, 12), $intPosition); $intPosition = $intPosition - 14; } $intTotalSumWithVat = $intTotalSum + $intTotalVat; $intRound = round($intTotalSumWithVat) - $intTotalSumWithVat; $intTotalSumWithVat = $intTotalSumWithVat + $intRound; $intTotalSum = number_format($intTotalSum, 2, ',', ' '); $intTotalVat = number_format($intTotalVat, 2, ',', ' '); $intRound = number_format($intRound, 2, ',', ' '); $intTotalSumWithVat = number_format($intTotalSumWithVat, 2, ',', ' '); $page1->setFont($fontTimes, 12)->setFillColor($colorBlack)->drawText($intTotalSum, 125 - $this->widthForStringUsingFontSize($intTotalSum, $fontTimes, 12), 110)->drawText($intTotalVat, 330 - $this->widthForStringUsingFontSize($intTotalVat, $fontTimes, 12), 110)->drawText($intRound, 410 - $this->widthForStringUsingFontSize($intRound, $fontTimes, 12), 110); $page1->setFont($fontTimes, 16)->setFillColor($colorBlack)->drawText($intTotalSumWithVat, 540 - $this->widthForStringUsingFontSize($intTotalSumWithVat, $fontTimes, 16), 110); // Draw address $intAddressPosition = 710; foreach (explode(';', $invoice->getAddress()) as $part) { $page1->setFont($fontTimes, 16)->setFillColor($colorBlack)->drawText($part, 315, $intAddressPosition); $intAddressPosition = $intAddressPosition - 20; } try { $pdf->save('data/invoice/invoice-' . sprintf('%06d', $invoice->getNumber()) . '.pdf'); } catch (\ZendPdf\Exception\IOException $e) { throw $e; } }
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 testModify() { $pdf = Pdf\PdfDocument::load(__DIR__ . '/_files/pdfarchiving.pdf'); // Reverse page order $pdf->pages = array_reverse($pdf->pages); // Mark page as modified foreach ($pdf->pages as $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)->drawText('Modified by Zend Framework!', 150, 0)->restoreGS(); } // Add new page generated by \ZendPdf\PdfDocument object (page is attached to the specified the document) $pdf->pages[] = $page1 = $pdf->newPage('A4'); // Add new page generated by \ZendPdf\Page object (page is not attached to the document) $pdf->pages[] = $page2 = new Pdf\Page(Pdf\Page::SIZE_LETTER_LANDSCAPE); // Create new font $font = Pdf\Font::fontWithName(Pdf\Font::FONT_HELVETICA); // Apply font and draw text $page1->setFont($font, 36)->setFillColor(Color\Html::color('#9999cc'))->drawText('Helvetica 36 text string', 60, 500); // Use font object for another page $page2->setFont($font, 24)->drawText('Helvetica 24 text string', 60, 500); // Use another font $page2->setFont(Pdf\Font::fontWithName(Pdf\Font::FONT_TIMES), 32)->drawText('Times-Roman 32 text string', 60, 450); // Draw rectangle $page2->setFillColor(new Color\GrayScale(0.8))->setLineColor(new Color\GrayScale(0.2))->setLineDashingPattern(array(3, 2, 3, 4), 1.6)->drawRectangle(60, 400, 500, 350); // Draw rounded rectangle $page2->setFillColor(new Color\GrayScale(0.9))->setLineColor(new Color\GrayScale(0.5))->setLineDashingPattern(Pdf\Page::LINE_DASHING_SOLID)->drawRoundedRectangle(425, 350, 475, 400, 20); // Draw circle $page2->setLineDashingPattern(Pdf\Page::LINE_DASHING_SOLID)->setFillColor(new Color\Rgb(1, 0, 0))->drawCircle(85, 375, 25); // Draw sectors $page2->drawCircle(200, 375, 25, 2 * M_PI / 3, -M_PI / 6)->setFillColor(new Color\Cmyk(1, 0, 0, 0))->drawCircle(200, 375, 25, M_PI / 6, 2 * M_PI / 3)->setFillColor(new Color\Rgb(1, 1, 0))->drawCircle(200, 375, 25, -M_PI / 6, M_PI / 6); // Draw ellipse $page2->setFillColor(new Color\Rgb(1, 0, 0))->drawEllipse(250, 400, 400, 350)->setFillColor(new Color\Cmyk(1, 0, 0, 0))->drawEllipse(250, 400, 400, 350, M_PI / 6, 2 * M_PI / 3)->setFillColor(new Color\Rgb(1, 1, 0))->drawEllipse(250, 400, 400, 350, -M_PI / 6, M_PI / 6); // Draw and fill polygon $page2->setFillColor(new Color\Rgb(1, 0, 1)); $x = array(); $y = array(); for ($count = 0; $count < 8; $count++) { $x[] = 140 + 25 * cos(3 * M_PI_4 * $count); $y[] = 375 + 25 * sin(3 * M_PI_4 * $count); } $page2->drawPolygon($x, $y, Pdf\Page::SHAPE_DRAW_FILL_AND_STROKE, Pdf\Page::FILL_METHOD_EVEN_ODD); // Draw line $page2->setLineWidth(0.5)->drawLine(60, 375, 500, 375); $pdf->save(__DIR__ . '/_files/output.pdf'); unset($pdf); $pdf1 = Pdf\PdfDocument::load(__DIR__ . '/_files/output.pdf'); $this->assertTrue($pdf1 instanceof Pdf\PdfDocument); unset($pdf1); unlink(__DIR__ . '/_files/output.pdf'); }
/** * @{inheritDoc} */ public function setPageFont(Page $page, $fontName, $fontSize) { $font = $this->getFontByName($fontName); return $page->setFont($font, $fontSize); }