[307d09d] | 1 | <?php |
---|
| 2 | //============================================================+ |
---|
| 3 | // File name : example_059.php |
---|
| 4 | // Begin : 2010-05-06 |
---|
| 5 | // Last Update : 2010-09-13 |
---|
| 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 | /** |
---|
| 23 | * Creates an example PDF TEST document using TCPDF |
---|
| 24 | * @package com.tecnick.tcpdf |
---|
| 25 | * @abstract TCPDF - Example: Table Of Content using HTML templates. |
---|
| 26 | * @author Nicola Asuni |
---|
| 27 | * @since 2010-05-06 |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | require_once('../config/lang/eng.php'); |
---|
| 31 | require_once('../tcpdf.php'); |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * TCPDF class extension with custom header and footer for TOC page |
---|
| 35 | */ |
---|
| 36 | class TOC_TCPDF extends TCPDF { |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * Overwrite Header() method. |
---|
| 40 | * @public |
---|
| 41 | */ |
---|
| 42 | public function Header() { |
---|
| 43 | if ($this->tocpage) { |
---|
| 44 | // *** replace the following parent::Header() with your code for TOC page |
---|
| 45 | parent::Header(); |
---|
| 46 | } else { |
---|
| 47 | // *** replace the following parent::Header() with your code for normal pages |
---|
| 48 | parent::Header(); |
---|
| 49 | } |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * Overwrite Footer() method. |
---|
| 54 | * @public |
---|
| 55 | */ |
---|
| 56 | public function Footer() { |
---|
| 57 | if ($this->tocpage) { |
---|
| 58 | // *** replace the following parent::Footer() with your code for TOC page |
---|
| 59 | parent::Footer(); |
---|
| 60 | } else { |
---|
| 61 | // *** replace the following parent::Footer() with your code for normal pages |
---|
| 62 | parent::Footer(); |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | } // end of class |
---|
| 67 | |
---|
| 68 | // create new PDF document |
---|
| 69 | $pdf = new TOC_TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); |
---|
| 70 | |
---|
| 71 | // set document information |
---|
| 72 | $pdf->SetCreator(PDF_CREATOR); |
---|
| 73 | $pdf->SetAuthor('Nicola Asuni'); |
---|
| 74 | $pdf->SetTitle('TCPDF Example 059'); |
---|
| 75 | $pdf->SetSubject('TCPDF Tutorial'); |
---|
| 76 | $pdf->SetKeywords('TCPDF, PDF, example, test, guide'); |
---|
| 77 | |
---|
| 78 | // set default header data |
---|
| 79 | $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 059', PDF_HEADER_STRING); |
---|
| 80 | |
---|
| 81 | // set header and footer fonts |
---|
| 82 | $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); |
---|
| 83 | $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); |
---|
| 84 | |
---|
| 85 | // set default monospaced font |
---|
| 86 | $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); |
---|
| 87 | |
---|
| 88 | //set margins |
---|
| 89 | $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); |
---|
| 90 | $pdf->SetHeaderMargin(PDF_MARGIN_HEADER); |
---|
| 91 | $pdf->SetFooterMargin(PDF_MARGIN_FOOTER); |
---|
| 92 | |
---|
| 93 | //set auto page breaks |
---|
| 94 | $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); |
---|
| 95 | |
---|
| 96 | //set image scale factor |
---|
| 97 | $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); |
---|
| 98 | |
---|
| 99 | //set some language-dependent strings |
---|
| 100 | $pdf->setLanguageArray($l); |
---|
| 101 | |
---|
| 102 | // set font |
---|
| 103 | $pdf->SetFont('helvetica', '', 10); |
---|
| 104 | |
---|
| 105 | // --------------------------------------------------------- |
---|
| 106 | |
---|
| 107 | // create some content ... |
---|
| 108 | |
---|
| 109 | // add a page |
---|
| 110 | $pdf->AddPage(); |
---|
| 111 | |
---|
| 112 | // set a bookmark for the current position |
---|
| 113 | $pdf->Bookmark('Chapter 1', 0, 0); |
---|
| 114 | |
---|
| 115 | // print a line using Cell() |
---|
| 116 | $pdf->Cell(0, 10, 'Chapter 1', 0, 1, 'L'); |
---|
| 117 | |
---|
| 118 | $pdf->AddPage(); |
---|
| 119 | $pdf->Bookmark('Paragraph 1.1', 1, 0); |
---|
| 120 | $pdf->Cell(0, 10, 'Paragraph 1.1', 0, 1, 'L'); |
---|
| 121 | |
---|
| 122 | $pdf->AddPage(); |
---|
| 123 | $pdf->Bookmark('Paragraph 1.2', 1, 0); |
---|
| 124 | $pdf->Cell(0, 10, 'Paragraph 1.2', 0, 1, 'L'); |
---|
| 125 | |
---|
| 126 | $pdf->AddPage(); |
---|
| 127 | $pdf->Bookmark('Sub-Paragraph 1.2.1', 2, 0); |
---|
| 128 | $pdf->Cell(0, 10, 'Sub-Paragraph 1.2.1', 0, 1, 'L'); |
---|
| 129 | |
---|
| 130 | $pdf->AddPage(); |
---|
| 131 | $pdf->Bookmark('Paragraph 1.3', 1, 0); |
---|
| 132 | $pdf->Cell(0, 10, 'Paragraph 1.3', 0, 1, 'L'); |
---|
| 133 | |
---|
| 134 | for ($i = 2; $i < 12; ++$i) { |
---|
| 135 | $pdf->AddPage(); |
---|
| 136 | $pdf->Bookmark('Chapter '.$i, 0, 0); |
---|
| 137 | $pdf->Cell(0, 10, 'Chapter '.$i, 0, 1, 'L'); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | // add a new page for TOC |
---|
| 145 | $pdf->addTOCPage(); |
---|
| 146 | |
---|
| 147 | // write the TOC title and/or other elements on the TOC page |
---|
| 148 | $pdf->SetFont('times', 'B', 16); |
---|
| 149 | $pdf->MultiCell(0, 0, 'Table Of Content', 0, 'C', 0, 1, '', '', true, 0); |
---|
| 150 | $pdf->Ln(); |
---|
| 151 | $pdf->SetFont('helvetica', '', 10); |
---|
| 152 | |
---|
| 153 | // define styles for various bookmark levels |
---|
| 154 | $bookmark_templates = array(); |
---|
| 155 | |
---|
| 156 | /* |
---|
| 157 | * The key of the $bookmark_templates array represent the bookmark level (from 0 to n). |
---|
| 158 | * The following templates will be replaced with proper content: |
---|
| 159 | * #TOC_DESCRIPTION# this will be replaced with the bookmark description; |
---|
| 160 | * #TOC_PAGE_NUMBER# this will be replaced with page number. |
---|
| 161 | * |
---|
| 162 | * NOTES: |
---|
| 163 | * 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. |
---|
| 164 | * The following is just an example, you can get various styles by combining various HTML elements. |
---|
| 165 | */ |
---|
| 166 | |
---|
| 167 | // A monospaced font for the page number is mandatory to get the right alignment |
---|
| 168 | $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>'; |
---|
| 169 | $bookmark_templates[1] = '<table border="0" cellpadding="0" cellspacing="0"><tr><td width="5mm"> </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>'; |
---|
| 170 | $bookmark_templates[2] = '<table border="0" cellpadding="0" cellspacing="0"><tr><td width="10mm"> </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>'; |
---|
| 171 | // add other bookmark level templates here ... |
---|
| 172 | |
---|
| 173 | // add table of content at page 1 |
---|
| 174 | // (check the example n. 45 for a text-only TOC |
---|
| 175 | $pdf->addHTMLTOC($page=1, $toc_name='INDEX', $bookmark_templates, $correct_align=true); |
---|
| 176 | |
---|
| 177 | // end of TOC page |
---|
| 178 | $pdf->endTOCPage(); |
---|
| 179 | |
---|
| 180 | // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
---|
| 181 | |
---|
| 182 | // --------------------------------------------------------- |
---|
| 183 | |
---|
| 184 | //Close and output PDF document |
---|
| 185 | $pdf->Output('example_059.pdf', 'I'); |
---|
| 186 | |
---|
| 187 | //============================================================+ |
---|
| 188 | // END OF FILE |
---|
| 189 | //============================================================+ |
---|