example_059.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. //============================================================+
  3. // File name : example_059.php
  4. // Begin : 2010-05-06
  5. // Last Update : 2013-05-14
  6. //
  7. // Description : Example 059 for TCPDF class
  8. // Table Of Content using HTML templates.
  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: Table Of Content using HTML templates.
  22. * @author Nicola Asuni
  23. * @since 2010-05-06
  24. * @group toc
  25. * @group html
  26. * @group pdf
  27. */
  28. // Include the main TCPDF library (search for installation path).
  29. require_once('tcpdf_include.php');
  30. /**
  31. * TCPDF class extension with custom header and footer for TOC page
  32. */
  33. class TOC_TCPDF extends TCPDF {
  34. /**
  35. * Overwrite Header() method.
  36. * @public
  37. */
  38. public function Header() {
  39. if ($this->tocpage) {
  40. // *** replace the following parent::Header() with your code for TOC page
  41. parent::Header();
  42. } else {
  43. // *** replace the following parent::Header() with your code for normal pages
  44. parent::Header();
  45. }
  46. }
  47. /**
  48. * Overwrite Footer() method.
  49. * @public
  50. */
  51. public function Footer() {
  52. if ($this->tocpage) {
  53. // *** replace the following parent::Footer() with your code for TOC page
  54. parent::Footer();
  55. } else {
  56. // *** replace the following parent::Footer() with your code for normal pages
  57. parent::Footer();
  58. }
  59. }
  60. } // end of class
  61. // create new PDF document
  62. $pdf = new TOC_TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  63. // set document information
  64. $pdf->setCreator(PDF_CREATOR);
  65. $pdf->setAuthor('Nicola Asuni');
  66. $pdf->setTitle('TCPDF Example 059');
  67. $pdf->setSubject('TCPDF Tutorial');
  68. $pdf->setKeywords('TCPDF, PDF, example, test, guide');
  69. // set default header data
  70. $pdf->setHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 059', PDF_HEADER_STRING);
  71. // set header and footer fonts
  72. $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
  73. $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
  74. // set default monospaced font
  75. $pdf->setDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  76. // set margins
  77. $pdf->setMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  78. $pdf->setHeaderMargin(PDF_MARGIN_HEADER);
  79. $pdf->setFooterMargin(PDF_MARGIN_FOOTER);
  80. // set auto page breaks
  81. $pdf->setAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  82. // set image scale factor
  83. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  84. // set some language-dependent strings (optional)
  85. if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
  86. require_once(dirname(__FILE__).'/lang/eng.php');
  87. $pdf->setLanguageArray($l);
  88. }
  89. // set font
  90. $pdf->setFont('helvetica', '', 10);
  91. // ---------------------------------------------------------
  92. // create some content ...
  93. // add a page
  94. $pdf->AddPage();
  95. // set a bookmark for the current position
  96. $pdf->Bookmark('Chapter 1', 0, 0, '', 'B', array(0,64,128));
  97. // print a line using Cell()
  98. $pdf->Cell(0, 10, 'Chapter 1', 0, 1, 'L');
  99. $pdf->AddPage();
  100. $pdf->Bookmark('Paragraph 1.1', 1, 0, '', '', array(128,0,0));
  101. $pdf->Cell(0, 10, 'Paragraph 1.1', 0, 1, 'L');
  102. $pdf->AddPage();
  103. $pdf->Bookmark('Paragraph 1.2', 1, 0, '', '', array(128,0,0));
  104. $pdf->Cell(0, 10, 'Paragraph 1.2', 0, 1, 'L');
  105. $pdf->AddPage();
  106. $pdf->Bookmark('Sub-Paragraph 1.2.1', 2, 0, '', 'I', array(0,128,0));
  107. $pdf->Cell(0, 10, 'Sub-Paragraph 1.2.1', 0, 1, 'L');
  108. $pdf->AddPage();
  109. $pdf->Bookmark('Paragraph 1.3', 1, 0, '', '', array(128,0,0));
  110. $pdf->Cell(0, 10, 'Paragraph 1.3', 0, 1, 'L');
  111. // add some pages and bookmarks
  112. for ($i = 2; $i < 12; $i++) {
  113. $pdf->AddPage();
  114. $pdf->Bookmark('Chapter '.$i, 0, 0, '', 'B', array(0,64,128));
  115. $pdf->Cell(0, 10, 'Chapter '.$i, 0, 1, 'L');
  116. }
  117. // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
  118. // add a new page for TOC
  119. $pdf->addTOCPage();
  120. // write the TOC title and/or other elements on the TOC page
  121. $pdf->setFont('times', 'B', 16);
  122. $pdf->MultiCell(0, 0, 'Table Of Content', 0, 'C', 0, 1, '', '', true, 0);
  123. $pdf->Ln();
  124. $pdf->setFont('helvetica', '', 10);
  125. // define styles for various bookmark levels
  126. $bookmark_templates = array();
  127. /*
  128. * The key of the $bookmark_templates array represent the bookmark level (from 0 to n).
  129. * The following templates will be replaced with proper content:
  130. * #TOC_DESCRIPTION# this will be replaced with the bookmark description;
  131. * #TOC_PAGE_NUMBER# this will be replaced with page number.
  132. *
  133. * NOTES:
  134. * If you want to align the page number on the right you have to use a monospaced font like courier, otherwise you can left align using any font type.
  135. * The following is just an example, you can get various styles by combining various HTML elements.
  136. */
  137. // A monospaced font for the page number is mandatory to get the right alignment
  138. $bookmark_templates[0] = '<table border="0" cellpadding="0" cellspacing="0" style="background-color:#EEFAFF"><tr><td width="155mm"><span style="font-family:times;font-weight:bold;font-size:12pt;color:black;">#TOC_DESCRIPTION#</span></td><td width="25mm"><span style="font-family:courier;font-weight:bold;font-size:12pt;color:black;" align="right">#TOC_PAGE_NUMBER#</span></td></tr></table>';
  139. $bookmark_templates[1] = '<table border="0" cellpadding="0" cellspacing="0"><tr><td width="5mm">&nbsp;</td><td width="150mm"><span style="font-family:times;font-size:11pt;color:green;">#TOC_DESCRIPTION#</span></td><td width="25mm"><span style="font-family:courier;font-weight:bold;font-size:11pt;color:green;" align="right">#TOC_PAGE_NUMBER#</span></td></tr></table>';
  140. $bookmark_templates[2] = '<table border="0" cellpadding="0" cellspacing="0"><tr><td width="10mm">&nbsp;</td><td width="145mm"><span style="font-family:times;font-size:10pt;color:#666666;"><i>#TOC_DESCRIPTION#</i></span></td><td width="25mm"><span style="font-family:courier;font-weight:bold;font-size:10pt;color:#666666;" align="right">#TOC_PAGE_NUMBER#</span></td></tr></table>';
  141. // add other bookmark level templates here ...
  142. // add table of content at page 1
  143. // (check the example n. 45 for a text-only TOC
  144. $pdf->addHTMLTOC(1, 'INDEX', $bookmark_templates, true, 'B', array(128,0,0));
  145. // end of TOC page
  146. $pdf->endTOCPage();
  147. // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
  148. // ---------------------------------------------------------
  149. //Close and output PDF document
  150. $pdf->Output('example_059.pdf', 'D');
  151. //============================================================+
  152. // END OF FILE
  153. //============================================================+