Autor wpisu: Diabl0, dodany: 20.11.2008 09:46, tagi: php, zend_framework
Ostatnio po raz kolejny w czasie prac nad jednym z projektów stanąłem przed problemem generowania dokumentów do druku. I po raz kolejny automatycznie wybór padł na PDF. I znowu ( ) po raz kolejny stanąłem przed problemem jakiej klasy użyć aby ułatwić sobie zadanie. Tym razem mój wzrok padł na TCPDF i jak na razie spełnia on pokładane w nim nadzieje.
Poniżej natomiast znajdziecie mały wrapper aby korzystanie z TCPDF w Zend Framework było proste, wygodne i zgodne z standardami ZF.
Samą klasę znajdziecie na SF.net: http://sourceforge.net/project/showfiles.php?group_id=128076 - wystarczy ją pobrać i wrzucić do katalogu library/Mao/TCPDF/ (oczywiście Mao zamieniacie na własną nazwę).
Następnie wystarczy utworzyć prostą klasę Mao_TCPDF w library/Mao/TCPDF.php , a w niej:
<?php /** * This is a Zend Framework wrapper for TCPDF class for generating PDF documents without requiring external extensions. * * {@link http://www.tcpdf.org TCPDF project} has been originally derived in 2002 from the Public Domain {@link http://www.fpdf.org FPDF class} by Olivier Plathey, but now is almost entirely rewritten. * * * @category Mao * @package Mao_TCPDF * @author Krzysztof 'Diabl0' Szatanik * @copyright Copyright (c) 2008, MAO Group * @link http://www.tcpdf.org */ Zend_Loader::loadFile( 'tcpdf.php', '../library/Mao/TCPDF/', true ); /** * This is a Zend Framework wrapper for TCPDF class for generating PDF documents without requiring external extensions. * * {@link http://www.tcpdf.org TCPDF project} has been originally derived in 2002 from the Public Domain {@link http://www.fpdf.org FPDF class} by Olivier Plathey, but now is almost entirely rewritten. * * @category Mao * @package Mao_TCPDF * @author Krzysztof 'Diabl0' Szatanik * @copyright Copyright (c) 2008, MAO Group * @link http://www.tcpdf.org */ class Mao_TCPDF extends TCPDF { /** * @var alias for total number of pages */ protected $AliasNbPages = '{nb}'; /** * This is the class constructor. * * It allows to set up the page format, the orientation and the measure unit used in all the methods (except for the font sizes). * * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or Portrait (default)</li><li>L or Landscape</li></ul> * @param string $unit User measure unit. Possible values are:<ul><li>pt: point</li><li>mm: millimeter (default)</li><li>cm: centimeter</li><li>in: inch</li></ul><br />A point equals 1/72 of inch, that is to say about 0.35 mm (an inch being 2.54 cm). This is a very common unit in typography; font sizes are expressed in that unit. * @param mixed $format The format used for pages. It can be either one of the following values (case insensitive) or a custom format in the form of a two-element array containing the width and the height (expressed in the unit given by unit).<ul><li>4A0</li><li>2A0</li><li>A0</li><li>A1</li><li>A2</li><li>A3</li><li>A4 (default)</li><li>A5</li><li>A6</li><li>A7</li><li>A8</li><li>A9</li><li>A10</li><li>B0</li><li>B1</li><li>B2</li><li>B3</li><li>B4</li><li>B5</li><li>B6</li><li>B7</li><li>B8</li><li>B9</li><li>B10</li><li>C0</li><li>C1</li><li>C2</li><li>C3</li><li>C4</li><li>C5</li><li>C6</li><li>C7</li><li>C8</li><li>C9</li><li>C10</li><li>RA0</li><li>RA1</li><li>RA2</li><li>RA3</li><li>RA4</li><li>SRA0</li><li>SRA1</li><li>SRA2</li><li>SRA3</li><li>SRA4</li><li>LETTER</li><li>LEGAL</li><li>EXECUTIVE</li><li>FOLIO</li></ul> * @param boolean $unicode TRUE means that the input text is unicode (default = true) * @param String $encoding charset encoding; default is UTF-8 */ public function __construct( $orientation = 'P' , $unit = 'mm' , $format = 'A4' , $unicode = true , $encoding = "UTF-8" ) { parent::__construct( $orientation, $unit, $format, $unicode, $encoding ); // Zawsze o tym zapominam, więc poleciało do konstruktora $this->AliasNbPages(); } /** * Zwraca szerokość strony * * @return float */ public function getPageWidth() { return $this->w - $this->rMargin - $this->x; } /** * Prints a cell (rectangular area) with optional borders, background color and character string. The upper-left corner of the cell corresponds to the current position. The text can be aligned or centered. After the call, the current position moves to the right or to the next line. It is possible to put a link on the text.<br /> * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting. * @param float $w Cell width. If 0, the cell extends up to the right margin. * @param float $h Cell height. Default value: 0. * @param string $txt String to print. Default value: empty string. * @param mixed $border Indicates if borders must be drawn around the cell. The value can be either a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul>or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> * @param int $ln Indicates where the current position should go after the call. Possible values are:<ul><li>0: to the right (or left for RTL languages)</li><li>1: to the beginning of the next line</li><li>2: below</li></ul> Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0. * @param string $align Allows to center or align the text. Possible values are:<ul><li>L or empty string: left align (default value)</li><li>C: center</li><li>R: right align</li><li>J: justify</li></ul> * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0. * @param mixed $link URL or identifier returned by AddLink(). * @param int $stretch stretch carachter mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if necessary</li><li>2 = forced horizontal scaling</li><li>3 = character spacing only if necessary</li><li>4 = forced character spacing</li></ul> * @since 1.0 * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), AddLink(), Ln(), MultiCell(), Write(), SetAutoPageBreak() */ public function Cell( $w , $h = 0 , $txt = '' , $border = 0 , $ln = 0 , $align = '' , $fill = 0 , $link = '' , $stretch = 0 ) { //html_entity_decode("&amp;euro;", ENT_COMPAT, "UTF-8"); if( $txt != '' ) { $txt = str_replace( '&amp;euro;', html_entity_decode( "&amp;euro;", ENT_COMPAT, "UTF-8" ), $txt ); } parent::Cell( $w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch ); } } ?>
W zasadzie wszystko powinno być jasne, ale na koniec jeszcze 2 uwagi.
1 - Własnego konstruktora używam z 2 powodów - AliasNbPages o którym zawsze zapominam w samym kodzie, i ładowanie własnego konfiga z Zend_Config (ten fragment dla czytelności pominąłem na powyższym listingu).
2 - metoda Cell - została lekko nadpisana z powodu często używanego przeze mnie znaku €. Po prostu wygodnie jest mi w kodzie używać znaku entity & euro ;.