example_059.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. //============================================================+
  3. // File name : example_059.php
  4. // Begin : 2010-05-06
  5. // Last Update : 2011-04-15
  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 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: Table Of Content using HTML templates.
  25. * @author Nicola Asuni
  26. * @since 2010-05-06
  27. */
  28. require_once('../config/lang/eng.php');
  29. require_once('../tcpdf.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
  85. $pdf->setLanguageArray($l);
  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', 'I');
  148. //============================================================+
  149. // END OF FILE
  150. //============================================================+