example_006.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. //============================================================+
  3. // File name : example_006.php
  4. // Begin : 2008-03-04
  5. // Last Update : 2013-05-14
  6. //
  7. // Description : Example 006 for TCPDF class
  8. // WriteHTML and RTL support
  9. //
  10. // Author: Nicola Asuni
  11. //
  12. // (c) Copyright:
  13. // Nicola Asuni
  14. // Tecnick.com LTD
  15. // www.tecnick.com
  16. // info@tecnick.com
  17. //============================================================+
  18. /**
  19. * Creates an example PDF TEST document using TCPDF
  20. * @package com.tecnick.tcpdf
  21. * @abstract TCPDF - Example: WriteHTML and RTL support
  22. * @author Nicola Asuni
  23. * @since 2008-03-04
  24. * @group html
  25. * @group rtl
  26. * @group pdf
  27. */
  28. // Include the main TCPDF library (search for installation path).
  29. require_once('tcpdf_include.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 006');
  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.' 006', 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 (optional)
  54. if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
  55. require_once(dirname(__FILE__).'/lang/eng.php');
  56. $pdf->setLanguageArray($l);
  57. }
  58. // ---------------------------------------------------------
  59. // set font
  60. $pdf->setFont('dejavusans', '', 10);
  61. // add a page
  62. $pdf->AddPage();
  63. // writeHTML($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='')
  64. // writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=0, $reseth=true, $align='', $autopadding=true)
  65. // create some HTML content
  66. $html = '<h1>HTML Example</h1>
  67. Some special characters: &lt; € &euro; &#8364; &amp; è &egrave; &copy; &gt; \\slash \\\\double-slash \\\\\\triple-slash
  68. <h2>List</h2>
  69. List example:
  70. <ol>
  71. <li><img src="images/logo_example.png" alt="test alt attribute" width="30" height="30" border="0" /> test image</li>
  72. <li><b>bold text</b></li>
  73. <li><i>italic text</i></li>
  74. <li><u>underlined text</u></li>
  75. <li><b>b<i>bi<u>biu</u>bi</i>b</b></li>
  76. <li><a href="http://www.tecnick.com" dir="ltr">link to http://www.tecnick.com</a></li>
  77. <li>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.<br />Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.</li>
  78. <li>SUBLIST
  79. <ol>
  80. <li>row one
  81. <ul>
  82. <li>sublist</li>
  83. </ul>
  84. </li>
  85. <li>row two</li>
  86. </ol>
  87. </li>
  88. <li><b>T</b>E<i>S</i><u>T</u> <del>line through</del></li>
  89. <li><font size="+3">font + 3</font></li>
  90. <li><small>small text</small> normal <small>small text</small> normal <sub>subscript</sub> normal <sup>superscript</sup> normal</li>
  91. </ol>
  92. <dl>
  93. <dt>Coffee</dt>
  94. <dd>Black hot drink</dd>
  95. <dt>Milk</dt>
  96. <dd>White cold drink</dd>
  97. </dl>
  98. <div style="text-align:center">IMAGES<br />
  99. <img src="images/logo_example.png" alt="test alt attribute" width="100" height="100" border="0" /><img src="images/tcpdf_box.svg" alt="test alt attribute" width="100" height="100" border="0" /><img src="images/logo_example.jpg" alt="test alt attribute" width="100" height="100" border="0" />
  100. </div>';
  101. // output the HTML content
  102. $pdf->writeHTML($html, true, false, true, false, '');
  103. // output some RTL HTML content
  104. $html = '<div style="text-align:center">The words &#8220;<span dir="rtl">&#1502;&#1494;&#1500; [mazel] &#1496;&#1493;&#1489; [tov]</span>&#8221; mean &#8220;Congratulations!&#8221;</div>';
  105. $pdf->writeHTML($html, true, false, true, false, '');
  106. // test some inline CSS
  107. $html = '<p>This is just an example of html code to demonstrate some supported CSS inline styles.
  108. <span style="font-weight: bold;">bold text</span>
  109. <span style="text-decoration: line-through;">line-trough</span>
  110. <span style="text-decoration: underline line-through;">underline and line-trough</span>
  111. <span style="color: rgb(0, 128, 64);">color</span>
  112. <span style="background-color: rgb(255, 0, 0); color: rgb(255, 255, 255);">background color</span>
  113. <span style="font-weight: bold;">bold</span>
  114. <span style="font-size: xx-small;">xx-small</span>
  115. <span style="font-size: x-small;">x-small</span>
  116. <span style="font-size: small;">small</span>
  117. <span style="font-size: medium;">medium</span>
  118. <span style="font-size: large;">large</span>
  119. <span style="font-size: x-large;">x-large</span>
  120. <span style="font-size: xx-large;">xx-large</span>
  121. </p>';
  122. $pdf->writeHTML($html, true, false, true, false, '');
  123. // reset pointer to the last page
  124. $pdf->lastPage();
  125. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  126. // Print a table
  127. // add a page
  128. $pdf->AddPage();
  129. // create some HTML content
  130. $subtable = '<table border="1" cellspacing="6" cellpadding="4"><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>';
  131. $html = '<h2>HTML TABLE:</h2>
  132. <table border="1" cellspacing="3" cellpadding="4">
  133. <tr>
  134. <th>#</th>
  135. <th align="right">RIGHT align</th>
  136. <th align="left">LEFT align</th>
  137. <th>4A</th>
  138. </tr>
  139. <tr>
  140. <td>1</td>
  141. <td bgcolor="#cccccc" align="center" colspan="2">A1 ex<i>amp</i>le <a href="http://www.tcpdf.org">link</a> column span. One two tree four five six seven eight nine ten.<br />line after br<br /><small>small text</small> normal <sub>subscript</sub> normal <sup>superscript</sup> normal bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla<ol><li>first<ol><li>sublist</li><li>sublist</li></ol></li><li>second</li></ol><small color="#FF0000" bgcolor="#FFFF00">small small small small small small small small small small small small small small small small small small small small</small></td>
  142. <td>4B</td>
  143. </tr>
  144. <tr>
  145. <td>'.$subtable.'</td>
  146. <td bgcolor="#0000FF" color="yellow" align="center">A2 € &euro; &#8364; &amp; è &egrave;<br/>A2 € &euro; &#8364; &amp; è &egrave;</td>
  147. <td bgcolor="#FFFF00" align="left"><font color="#FF0000">Red</font> Yellow BG</td>
  148. <td>4C</td>
  149. </tr>
  150. <tr>
  151. <td>1A</td>
  152. <td rowspan="2" colspan="2" bgcolor="#FFFFCC">2AA<br />2AB<br />2AC</td>
  153. <td bgcolor="#FF0000">4D</td>
  154. </tr>
  155. <tr>
  156. <td>1B</td>
  157. <td>4E</td>
  158. </tr>
  159. <tr>
  160. <td>1C</td>
  161. <td>2C</td>
  162. <td>3C</td>
  163. <td>4F</td>
  164. </tr>
  165. </table>';
  166. // output the HTML content
  167. $pdf->writeHTML($html, true, false, true, false, '');
  168. // Print some HTML Cells
  169. $html = '<span color="red">red</span> <span color="green">green</span> <span color="blue">blue</span><br /><span color="red">red</span> <span color="green">green</span> <span color="blue">blue</span>';
  170. $pdf->setFillColor(255,255,0);
  171. $pdf->writeHTMLCell(0, 0, '', '', $html, 'LRTB', 1, 0, true, 'L', true);
  172. $pdf->writeHTMLCell(0, 0, '', '', $html, 'LRTB', 1, 1, true, 'C', true);
  173. $pdf->writeHTMLCell(0, 0, '', '', $html, 'LRTB', 1, 0, true, 'R', true);
  174. // reset pointer to the last page
  175. $pdf->lastPage();
  176. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  177. // Print a table
  178. // add a page
  179. $pdf->AddPage();
  180. // create some HTML content
  181. $html = '<h1>Image alignments on HTML table</h1>
  182. <table cellpadding="1" cellspacing="1" border="1" style="text-align:center;">
  183. <tr><td><img src="images/logo_example.png" border="0" height="41" width="41" /></td></tr>
  184. <tr style="text-align:left;"><td><img src="images/logo_example.png" border="0" height="41" width="41" align="top" /></td></tr>
  185. <tr style="text-align:center;"><td><img src="images/logo_example.png" border="0" height="41" width="41" align="middle" /></td></tr>
  186. <tr style="text-align:right;"><td><img src="images/logo_example.png" border="0" height="41" width="41" align="bottom" /></td></tr>
  187. <tr><td style="text-align:left;"><img src="images/logo_example.png" border="0" height="41" width="41" align="top" /></td></tr>
  188. <tr><td style="text-align:center;"><img src="images/logo_example.png" border="0" height="41" width="41" align="middle" /></td></tr>
  189. <tr><td style="text-align:right;"><img src="images/logo_example.png" border="0" height="41" width="41" align="bottom" /></td></tr>
  190. </table>';
  191. // output the HTML content
  192. $pdf->writeHTML($html, true, false, true, false, '');
  193. // create some HTML content
  194. $html = '<h1>Embedded Images</h1>
  195. <table cellpadding="1" cellspacing="1" border="1" style="text-align:center;">
  196. <tr><td>src="@..." </td><td><img src="@DATA1@" border="0" height="41" width="41" /></td></tr>
  197. <tr><td>src="data..."</td><td><img src="@DATA2@" border="0" height="41" width="41" /></td></tr>
  198. </table>';
  199. $data = base64_encode(file_get_contents("images/logo_example.png"));
  200. $html = str_replace("@DATA1@", "@" . $data, $html);
  201. $html = str_replace("@DATA2@", "data:image/png;base64," . $data, $html);
  202. // output the HTML content
  203. $pdf->writeHTML($html, true, false, true, false, '');
  204. // reset pointer to the last page
  205. $pdf->lastPage();
  206. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  207. // Print all HTML colors
  208. // add a page
  209. $pdf->AddPage();
  210. $textcolors = '<h1>HTML Text Colors</h1>';
  211. $bgcolors = '<hr /><h1>HTML Background Colors</h1>';
  212. foreach(TCPDF_COLORS::$webcolor as $k => $v) {
  213. $textcolors .= '<span color="#'.$v.'">'.$v.'</span> ';
  214. $bgcolors .= '<span bgcolor="#'.$v.'" color="#333333">'.$v.'</span> ';
  215. }
  216. // output the HTML content
  217. $pdf->writeHTML($textcolors, true, false, true, false, '');
  218. $pdf->writeHTML($bgcolors, true, false, true, false, '');
  219. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  220. // Test word-wrap
  221. // create some HTML content
  222. $html = '<hr />
  223. <h1>Various tests</h1>
  224. <a href="#2">link to page 2</a><br />
  225. <font face="courier"><b>thisisaverylongword</b></font> <font face="helvetica"><i>thisisanotherverylongword</i></font> <font face="times"><b>thisisaverylongword</b></font> thisisanotherverylongword <font face="times">thisisaverylongword</font> <font face="courier"><b>thisisaverylongword</b></font> <font face="helvetica"><i>thisisanotherverylongword</i></font> <font face="times"><b>thisisaverylongword</b></font> thisisanotherverylongword <font face="times">thisisaverylongword</font> <font face="courier"><b>thisisaverylongword</b></font> <font face="helvetica"><i>thisisanotherverylongword</i></font> <font face="times"><b>thisisaverylongword</b></font> thisisanotherverylongword <font face="times">thisisaverylongword</font> <font face="courier"><b>thisisaverylongword</b></font> <font face="helvetica"><i>thisisanotherverylongword</i></font> <font face="times"><b>thisisaverylongword</b></font> thisisanotherverylongword <font face="times">thisisaverylongword</font> <font face="courier"><b>thisisaverylongword</b></font> <font face="helvetica"><i>thisisanotherverylongword</i></font> <font face="times"><b>thisisaverylongword</b></font> thisisanotherverylongword <font face="times">thisisaverylongword</font>';
  226. // output the HTML content
  227. $pdf->writeHTML($html, true, false, true, false, '');
  228. // Test fonts nesting
  229. $html1 = 'Default <font face="courier">Courier <font face="helvetica">Helvetica <font face="times">Times <font face="dejavusans">dejavusans </font>Times </font>Helvetica </font>Courier </font>Default';
  230. $html2 = '<small>small text</small> normal <small>small text</small> normal <sub>subscript</sub> normal <sup>superscript</sup> normal';
  231. $html3 = '<font size="10" color="#ff7f50">The</font> <font size="10" color="#6495ed">quick</font> <font size="14" color="#dc143c">brown</font> <font size="18" color="#008000">fox</font> <font size="22"><a href="http://www.tcpdf.org">jumps</a></font> <font size="22" color="#a0522d">over</font> <font size="18" color="#da70d6">the</font> <font size="14" color="#9400d3">lazy</font> <font size="10" color="#4169el">dog</font>.';
  232. $html = $html1.'<br />'.$html2.'<br />'.$html3.'<br />'.$html3.'<br />'.$html2;
  233. // output the HTML content
  234. $pdf->writeHTML($html, true, false, true, false, '');
  235. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  236. // test pre tag
  237. // add a page
  238. $pdf->AddPage();
  239. $html = <<<EOF
  240. <div style="background-color:#880000;color:white;">
  241. Hello World!<br />
  242. Hello
  243. </div>
  244. <pre style="background-color:#336699;color:white;">
  245. int main() {
  246. printf("HelloWorld");
  247. return 0;
  248. }
  249. </pre>
  250. <tt>Monospace font</tt>, normal font, <tt>monospace font</tt>, normal font.
  251. <br />
  252. <div style="background-color:#880000;color:white;">DIV LEVEL 1<div style="background-color:#008800;color:white;">DIV LEVEL 2</div>DIV LEVEL 1</div>
  253. <br />
  254. <span style="background-color:#880000;color:white;">SPAN LEVEL 1 <span style="background-color:#008800;color:white;">SPAN LEVEL 2</span> SPAN LEVEL 1</span>
  255. EOF;
  256. // output the HTML content
  257. $pdf->writeHTML($html, true, false, true, false, '');
  258. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  259. // test custom bullet points for list
  260. // add a page
  261. $pdf->AddPage();
  262. $html = <<<EOF
  263. <h1>Test custom bullet image for list items</h1>
  264. <ul style="font-size:14pt;list-style-type:img|png|4|4|images/logo_example.png">
  265. <li>test custom bullet image</li>
  266. <li>test custom bullet image</li>
  267. <li>test custom bullet image</li>
  268. <li>test custom bullet image</li>
  269. <ul>
  270. EOF;
  271. // output the HTML content
  272. $pdf->writeHTML($html, true, false, true, false, '');
  273. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  274. // reset pointer to the last page
  275. $pdf->lastPage();
  276. // ---------------------------------------------------------
  277. //Close and output PDF document
  278. $pdf->Output('example_006.pdf', 'I');
  279. //============================================================+
  280. // END OF FILE
  281. //============================================================+