You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
6.5 KiB

3 years ago
  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. */
  25. // Include the main TCPDF library (search for installation path).
  26. require_once('tcpdf_include.php');
  27. /**
  28. * TCPDF class extension with custom header and footer for TOC page
  29. */
  30. class TOC_TCPDF extends TCPDF {
  31. /**
  32. * Overwrite Header() method.
  33. * @public
  34. */
  35. public function Header() {
  36. if ($this->tocpage) {
  37. // *** replace the following parent::Header() with your code for TOC page
  38. parent::Header();
  39. } else {
  40. // *** replace the following parent::Header() with your code for normal pages
  41. parent::Header();
  42. }
  43. }
  44. /**
  45. * Overwrite Footer() method.
  46. * @public
  47. */
  48. public function Footer() {
  49. if ($this->tocpage) {
  50. // *** replace the following parent::Footer() with your code for TOC page
  51. parent::Footer();
  52. } else {
  53. // *** replace the following parent::Footer() with your code for normal pages
  54. parent::Footer();
  55. }
  56. }
  57. } // end of class
  58. // create new PDF document
  59. $pdf = new TOC_TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  60. // set document information
  61. $pdf->SetCreator(PDF_CREATOR);
  62. $pdf->SetAuthor('Nicola Asuni');
  63. $pdf->SetTitle('TCPDF Example 059');
  64. $pdf->SetSubject('TCPDF Tutorial');
  65. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  66. // set default header data
  67. $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 059', PDF_HEADER_STRING);
  68. // set header and footer fonts
  69. $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
  70. $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
  71. // set default monospaced font
  72. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  73. // set margins
  74. $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  75. $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
  76. $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
  77. // set auto page breaks
  78. $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  79. // set image scale factor
  80. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  81. // set some language-dependent strings (optional)
  82. if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
  83. require_once(dirname(__FILE__).'/lang/eng.php');
  84. $pdf->setLanguageArray($l);
  85. }
  86. // set font
  87. $pdf->SetFont('helvetica', '', 10);
  88. // ---------------------------------------------------------
  89. // create some content ...
  90. // add a page
  91. $pdf->AddPage();
  92. // set a bookmark for the current position
  93. $pdf->Bookmark('Chapter 1', 0, 0, '', 'B', array(0,64,128));
  94. // print a line using Cell()
  95. $pdf->Cell(0, 10, 'Chapter 1', 0, 1, 'L');
  96. $pdf->AddPage();
  97. $pdf->Bookmark('Paragraph 1.1', 1, 0, '', '', array(128,0,0));
  98. $pdf->Cell(0, 10, 'Paragraph 1.1', 0, 1, 'L');
  99. $pdf->AddPage();
  100. $pdf->Bookmark('Paragraph 1.2', 1, 0, '', '', array(128,0,0));
  101. $pdf->Cell(0, 10, 'Paragraph 1.2', 0, 1, 'L');
  102. $pdf->AddPage();
  103. $pdf->Bookmark('Sub-Paragraph 1.2.1', 2, 0, '', 'I', array(0,128,0));
  104. $pdf->Cell(0, 10, 'Sub-Paragraph 1.2.1', 0, 1, 'L');
  105. $pdf->AddPage();
  106. $pdf->Bookmark('Paragraph 1.3', 1, 0, '', '', array(128,0,0));
  107. $pdf->Cell(0, 10, 'Paragraph 1.3', 0, 1, 'L');
  108. // add some pages and bookmarks
  109. for ($i = 2; $i < 12; $i++) {
  110. $pdf->AddPage();
  111. $pdf->Bookmark('Chapter '.$i, 0, 0, '', 'B', array(0,64,128));
  112. $pdf->Cell(0, 10, 'Chapter '.$i, 0, 1, 'L');
  113. }
  114. // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
  115. // add a new page for TOC
  116. $pdf->addTOCPage();
  117. // write the TOC title and/or other elements on the TOC page
  118. $pdf->SetFont('times', 'B', 16);
  119. $pdf->MultiCell(0, 0, 'Table Of Content', 0, 'C', 0, 1, '', '', true, 0);
  120. $pdf->Ln();
  121. $pdf->SetFont('helvetica', '', 10);
  122. // define styles for various bookmark levels
  123. $bookmark_templates = array();
  124. /*
  125. * The key of the $bookmark_templates array represent the bookmark level (from 0 to n).
  126. * The following templates will be replaced with proper content:
  127. * #TOC_DESCRIPTION# this will be replaced with the bookmark description;
  128. * #TOC_PAGE_NUMBER# this will be replaced with page number.
  129. *
  130. * NOTES:
  131. * 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.
  132. * The following is just an example, you can get various styles by combining various HTML elements.
  133. */
  134. // A monospaced font for the page number is mandatory to get the right alignment
  135. $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>';
  136. $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>';
  137. $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>';
  138. // add other bookmark level templates here ...
  139. // add table of content at page 1
  140. // (check the example n. 45 for a text-only TOC
  141. $pdf->addHTMLTOC(1, 'INDEX', $bookmark_templates, true, 'B', array(128,0,0));
  142. // end of TOC page
  143. $pdf->endTOCPage();
  144. // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
  145. // ---------------------------------------------------------
  146. //Close and output PDF document
  147. $pdf->Output('example_059.pdf', 'D');
  148. //============================================================+
  149. // END OF FILE
  150. //============================================================+