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; }
private function getResourceByStyle($style) { try { if (!isset($this->fonts[$style])) { $data = $this->fontResources[$style]; if ($this->isNamedFont($data)) { $name = $this->retrieveFontName($data); $this->fonts[$style] = ZendFont::fontWithName($name); } else { $this->fonts[$style] = ZendFont::fontWithPath($data); } } return $this->fonts[$style]; } catch (\ZendPdf\Exception\ExceptionInterface $e) { throw InvalidResourceException::invalidFontException($this->fontResources[$style], $e); } }
/** * Draw a polygon in the rendering resource * @param string $text * @param float $size * @param array $position * @param string $font * @param int $color * @param string $alignment * @param float $orientation */ protected function drawText($text, $size, $position, $font, $color, $alignment = 'center', $orientation = 0) { $page = $this->resource->pages[$this->page]; $color = new Color\Rgb((($color & 0xff0000) >> 16) / 255.0, (($color & 0xff00) >> 8) / 255.0, ($color & 0xff) / 255.0); $page->setLineColor($color); $page->setFillColor($color); $page->setFont(Font::fontWithPath($font), $size * $this->moduleSize * 1.2); $width = $this->widthForStringUsingFontSize($text, Font::fontWithPath($font), $size * $this->moduleSize); $angle = pi() * $orientation / 180; $left = $position[0] * $this->moduleSize + $this->leftOffset; $top = $page->getHeight() - $position[1] * $this->moduleSize - $this->topOffset; switch ($alignment) { case 'center': $left -= $width / 2 * cos($angle); $top -= $width / 2 * sin($angle); break; case 'right': $left -= $width; break; } $page->rotate($left, $top, $angle); $page->drawText($text, $left, $top); $page->rotate($left, $top, -$angle); }