example_061.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. //============================================================+
  3. // File name : example_061.php
  4. // Begin : 2010-05-24
  5. // Last Update : 2010-08-08
  6. //
  7. // Description : Example 061 for TCPDF class
  8. // XHTML + CSS
  9. //
  10. // Author: Nicola Asuni
  11. //
  12. // (c) Copyright:
  13. // Nicola Asuni
  14. // Tecnick.com s.r.l.
  15. // Via Della Pace, 11
  16. // 09044 Quartucciu (CA)
  17. // ITALY
  18. // www.tecnick.com
  19. // info@tecnick.com
  20. //============================================================+
  21. /**
  22. * Creates an example PDF TEST document using TCPDF
  23. * @package com.tecnick.tcpdf
  24. * @abstract TCPDF - Example: XHTML + CSS
  25. * @author Nicola Asuni
  26. * @since 2010-05-25
  27. */
  28. require_once('../config/lang/eng.php');
  29. require_once('../tcpdf.php');
  30. // create new PDF document
  31. $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  32. // set document information
  33. $pdf->SetCreator(PDF_CREATOR);
  34. $pdf->SetAuthor('Nicola Asuni');
  35. $pdf->SetTitle('TCPDF Example 061');
  36. $pdf->SetSubject('TCPDF Tutorial');
  37. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  38. // set default header data
  39. $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 061', PDF_HEADER_STRING);
  40. // set header and footer fonts
  41. $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
  42. $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
  43. // set default monospaced font
  44. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  45. //set margins
  46. $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  47. $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
  48. $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
  49. //set auto page breaks
  50. $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  51. //set image scale factor
  52. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  53. //set some language-dependent strings
  54. $pdf->setLanguageArray($l);
  55. // ---------------------------------------------------------
  56. // set font
  57. $pdf->SetFont('helvetica', '', 10);
  58. // add a page
  59. $pdf->AddPage();
  60. /* NOTE:
  61. * *********************************************************
  62. * You can load external XHTML using :
  63. *
  64. * $html = file_get_contents('/path/to/your/file.html');
  65. *
  66. * External CSS files will be automatically loaded.
  67. * Sometimes you need to fix the path of the external CSS.
  68. * *********************************************************
  69. */
  70. // define some HTML content with style
  71. $html = <<<EOF
  72. <!-- EXAMPLE OF CSS STYLE -->
  73. <style>
  74. h1 {
  75. color: navy;
  76. font-family: times;
  77. font-size: 24pt;
  78. text-decoration: underline;
  79. }
  80. p.first {
  81. color: #003300;
  82. font-family: helvetica;
  83. font-size: 12pt;
  84. }
  85. p.first span {
  86. color: #006600;
  87. font-style: italic;
  88. }
  89. p#second {
  90. color: rgb(00,63,127);
  91. font-family: times;
  92. font-size: 12pt;
  93. text-align: justify;
  94. }
  95. p#second > span {
  96. background-color: #FFFFAA;
  97. }
  98. table.first {
  99. color: #003300;
  100. font-family: helvetica;
  101. font-size: 8pt;
  102. border-left: 3px solid red;
  103. border-right: 3px solid #FF00FF;
  104. border-top: 3px solid green;
  105. border-bottom: 3px solid blue;
  106. background-color: #ccffcc;
  107. }
  108. td {
  109. border: 2px solid blue;
  110. background-color: #ffffee;
  111. }
  112. td.second {
  113. border: 2px dashed green;
  114. }
  115. div.test {
  116. color: #CC0000;
  117. background-color: #FFFF66;
  118. font-family: helvetica;
  119. font-size: 10pt;
  120. border-style: solid solid solid solid;
  121. border-width: 2px 2px 2px 2px;
  122. border-color: green #FF00FF blue red;
  123. text-align: center;
  124. }
  125. </style>
  126. <h1 class="title">Example of <i style="color:#990000">XHTML + CSS</i></h1>
  127. <p class="first">Example of paragraph with class selector. <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sed imperdiet lectus. Phasellus quis velit velit, non condimentum quam. Sed neque urna, ultrices ac volutpat vel, laoreet vitae augue. Sed vel velit erat. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Cras eget velit nulla, eu sagittis elit. Nunc ac arcu est, in lobortis tellus. Praesent condimentum rhoncus sodales. In hac habitasse platea dictumst. Proin porta eros pharetra enim tincidunt dignissim nec vel dolor. Cras sapien elit, ornare ac dignissim eu, ultricies ac eros. Maecenas augue magna, ultrices a congue in, mollis eu nulla. Nunc venenatis massa at est eleifend faucibus. Vivamus sed risus lectus, nec interdum nunc.</span></p>
  128. <p id="second">Example of paragraph with ID selector. <span>Fusce et felis vitae diam lobortis sollicitudin. Aenean tincidunt accumsan nisi, id vehicula quam laoreet elementum. Phasellus egestas interdum erat, et viverra ipsum ultricies ac. Praesent sagittis augue at augue volutpat eleifend. Cras nec orci neque. Mauris bibendum posuere blandit. Donec feugiat mollis dui sit amet pellentesque. Sed a enim justo. Donec tincidunt, nisl eget elementum aliquam, odio ipsum ultrices quam, eu porttitor ligula urna at lorem. Donec varius, eros et convallis laoreet, ligula tellus consequat felis, ut ornare metus tellus sodales velit. Duis sed diam ante. Ut rutrum malesuada massa, vitae consectetur ipsum rhoncus sed. Suspendisse potenti. Pellentesque a congue massa.</span></p>
  129. <div class="test">example of DIV with border and fill.<br />Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sed imperdiet lectus.</div>
  130. <br />
  131. <table class="first" cellpadding="4" cellspacing="6">
  132. <tr>
  133. <td width="30" align="center"><b>No.</b></td>
  134. <td width="140" align="center" bgcolor="#FFFF00"><b>XXXX</b></td>
  135. <td width="140" align="center"><b>XXXX</b></td>
  136. <td width="80" align="center"> <b>XXXX</b></td>
  137. <td width="80" align="center"><b>XXXX</b></td>
  138. <td width="45" align="center"><b>XXXX</b></td>
  139. </tr>
  140. <tr>
  141. <td width="30" align="center">1.</td>
  142. <td width="140" rowspan="6" class="second">XXXX<br />XXXX<br />XXXX<br />XXXX<br />XXXX<br />XXXX<br />XXXX<br />XXXX</td>
  143. <td width="140">XXXX<br />XXXX</td>
  144. <td width="80">XXXX<br />XXXX</td>
  145. <td width="80">XXXX</td>
  146. <td align="center" width="45">XXXX<br />XXXX</td>
  147. </tr>
  148. <tr>
  149. <td width="30" align="center" rowspan="3">2.</td>
  150. <td width="140" rowspan="3">XXXX<br />XXXX</td>
  151. <td width="80">XXXX<br />XXXX</td>
  152. <td width="80">XXXX<br />XXXX</td>
  153. <td align="center" width="45">XXXX<br />XXXX</td>
  154. </tr>
  155. <tr>
  156. <td width="80">XXXX<br />XXXX<br />XXXX<br />XXXX</td>
  157. <td width="80">XXXX<br />XXXX</td>
  158. <td align="center" width="45">XXXX<br />XXXX</td>
  159. </tr>
  160. <tr>
  161. <td width="80" rowspan="2" >XXXX<br />XXXX<br />XXXX<br />XXXX<br />XXXX<br />XXXX<br />XXXX<br />XXXX</td>
  162. <td width="80">XXXX<br />XXXX</td>
  163. <td align="center" width="45">XXXX<br />XXXX</td>
  164. </tr>
  165. <tr>
  166. <td width="30" align="center">3.</td>
  167. <td width="140">XXXX<br />XXXX</td>
  168. <td width="80">XXXX<br />XXXX</td>
  169. <td align="center" width="45">XXXX<br />XXXX</td>
  170. </tr>
  171. <tr bgcolor="#FFFF80">
  172. <td width="30" align="center">4.</td>
  173. <td width="140" bgcolor="#00CC00" color="#FFFF00">XXXX<br />XXXX</td>
  174. <td width="80">XXXX<br />XXXX</td>
  175. <td width="80">XXXX<br />XXXX</td>
  176. <td align="center" width="45">XXXX<br />XXXX</td>
  177. </tr>
  178. </table>
  179. EOF;
  180. // output the HTML content
  181. $pdf->writeHTML($html, true, false, true, false, '');
  182. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  183. // *******************************************************************
  184. // HTML TIPS & TRICKS
  185. // *******************************************************************
  186. // REMOVE CELL PADDING
  187. //
  188. // $pdf->SetCellPadding(0);
  189. //
  190. // This is used to remove any additional vertical space inside a
  191. // single cell of text.
  192. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  193. // REMOVE TAG TOP AND BOTTOM MARGINS
  194. //
  195. // $tagvs = array('p' => array(0 => array('h' => 0, 'n' => 0), 1 => array('h' => 0, 'n' => 0)));
  196. // $pdf->setHtmlVSpace($tagvs);
  197. //
  198. // Since the CSS margin command is not yet implemented on TCPDF, you
  199. // need to set the spacing of block tags using the following method.
  200. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  201. // SET LINE HEIGHT
  202. //
  203. // $pdf->setCellHeightRatio(1.25);
  204. //
  205. // You can use the following method to fine tune the line height
  206. // (the number is a percentage relative to font height).
  207. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  208. // CHANGE THE PIXEL CONVERSION RATIO
  209. //
  210. // $pdf->setImageScale(0.47);
  211. //
  212. // This is used to adjust the conversion ratio between pixels and
  213. // document units. Increase the value to get smaller objects.
  214. // Since you are using pixel unit, this method is important to set the
  215. // right zoom factor.
  216. //
  217. // Suppose that you want to print a web page larger 1024 pixels to
  218. // fill all the available page width.
  219. // An A4 page is larger 210mm equivalent to 8.268 inches, if you
  220. // subtract 13mm (0.512") of margins for each side, the remaining
  221. // space is 184mm (7.244 inches).
  222. // The default resolution for a PDF document is 300 DPI (dots per
  223. // inch), so you have 7.244 * 300 = 2173.2 dots (this is the maximum
  224. // number of points you can print at 300 DPI for the given width).
  225. // The conversion ratio is approximatively 1024 / 2173.2 = 0.47 px/dots
  226. // If the web page is larger 1280 pixels, on the same A4 page the
  227. // conversion ratio to use is 1280 / 2173.2 = 0.59 pixels/dots
  228. // *******************************************************************
  229. // reset pointer to the last page
  230. $pdf->lastPage();
  231. // ---------------------------------------------------------
  232. //Close and output PDF document
  233. $pdf->Output('example_061.pdf', 'I');
  234. //============================================================+
  235. // END OF FILE
  236. //============================================================+