example_015.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. //============================================================+
  3. // File name : example_015.php
  4. // Begin : 2008-03-04
  5. // Last Update : 2013-05-14
  6. //
  7. // Description : Example 015 for TCPDF class
  8. // Bookmarks (Table of Content)
  9. // and Named Destinations.
  10. //
  11. // Author: Nicola Asuni
  12. //
  13. // (c) Copyright:
  14. // Nicola Asuni
  15. // Tecnick.com LTD
  16. // www.tecnick.com
  17. // info@tecnick.com
  18. //============================================================+
  19. /**
  20. * Creates an example PDF TEST document using TCPDF
  21. * @package com.tecnick.tcpdf
  22. * @abstract TCPDF - Example: Bookmarks (Table of Content)
  23. * @author Nicola Asuni
  24. * @since 2008-03-04
  25. * @group toc
  26. * @group bookmark
  27. * @group pdf
  28. */
  29. // Include the main TCPDF library (search for installation path).
  30. require_once('tcpdf_include.php');
  31. // create new PDF document
  32. $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  33. // set document information
  34. $pdf->setCreator(PDF_CREATOR);
  35. $pdf->setAuthor('Nicola Asuni');
  36. $pdf->setTitle('TCPDF Example 015');
  37. $pdf->setSubject('TCPDF Tutorial');
  38. $pdf->setKeywords('TCPDF, PDF, example, test, guide');
  39. // set default header data
  40. $pdf->setHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 015', PDF_HEADER_STRING);
  41. // set header and footer fonts
  42. $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
  43. $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
  44. // set default monospaced font
  45. $pdf->setDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  46. // set margins
  47. $pdf->setMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  48. $pdf->setHeaderMargin(PDF_MARGIN_HEADER);
  49. $pdf->setFooterMargin(PDF_MARGIN_FOOTER);
  50. // set auto page breaks
  51. $pdf->setAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  52. // set image scale factor
  53. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  54. // set some language-dependent strings (optional)
  55. if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
  56. require_once(dirname(__FILE__).'/lang/eng.php');
  57. $pdf->setLanguageArray($l);
  58. }
  59. // ---------------------------------------------------------
  60. // Bookmark($txt, $level=0, $y=-1, $page='', $style='', $color=array(0,0,0))
  61. // set font
  62. $pdf->setFont('times', 'B', 20);
  63. // add a page
  64. $pdf->AddPage();
  65. // set a bookmark for the current position
  66. $pdf->Bookmark('Chapter 1', 0, 0, '', 'B', array(0,64,128));
  67. // print a line using Cell()
  68. $pdf->Cell(0, 10, 'Chapter 1', 0, 1, 'L');
  69. $pdf->setFont('times', 'I', 14);
  70. $pdf->Write(0, 'You can set PDF Bookmarks using the Bookmark() method.
  71. You can set PDF Named Destinations using the setDestination() method.');
  72. $pdf->setFont('times', 'B', 20);
  73. // add other pages and bookmarks
  74. $pdf->AddPage();
  75. $pdf->Bookmark('Paragraph 1.1', 1, 0, '', '', array(0,0,0));
  76. $pdf->Cell(0, 10, 'Paragraph 1.1', 0, 1, 'L');
  77. $pdf->AddPage();
  78. $pdf->Bookmark('Paragraph 1.2', 1, 0, '', '', array(0,0,0));
  79. $pdf->Cell(0, 10, 'Paragraph 1.2', 0, 1, 'L');
  80. $pdf->AddPage();
  81. $pdf->Bookmark('Sub-Paragraph 1.2.1', 2, 0, '', 'I', array(0,0,0));
  82. $pdf->Cell(0, 10, 'Sub-Paragraph 1.2.1', 0, 1, 'L');
  83. $pdf->AddPage();
  84. $pdf->Bookmark('Paragraph 1.3', 1, 0, '', '', array(0,0,0));
  85. $pdf->Cell(0, 10, 'Paragraph 1.3', 0, 1, 'L');
  86. $pdf->AddPage();
  87. // add a named destination so you can open this document at this page using the link: "example_015.pdf#chapter2"
  88. $pdf->setDestination('chapter2', 0, '');
  89. // add a bookmark that points to a named destination
  90. $pdf->Bookmark('Chapter 2', 0, 0, '', 'BI', array(128,0,0), -1, '#chapter2');
  91. $pdf->Cell(0, 10, 'Chapter 2', 0, 1, 'L');
  92. $pdf->setFont('times', 'I', 14);
  93. $pdf->Write(0, 'Once saved, you can open this document at this page using the link: "example_015.pdf#chapter2".');
  94. $pdf->AddPage();
  95. $pdf->setDestination('chapter3', 0, '');
  96. $pdf->setFont('times', 'B', 20);
  97. $pdf->Bookmark('Chapter 3', 0, 0, '', 'B', array(0,64,128));
  98. $pdf->Cell(0, 10, 'Chapter 3', 0, 1, 'L');
  99. $pdf->AddPage();
  100. $pdf->setDestination('chapter4', 0, '');
  101. $pdf->setFont('times', 'B', 20);
  102. $pdf->Bookmark('Chapter 4', 0, 0, '', 'B', array(0,64,128));
  103. $pdf->Cell(0, 10, 'Chapter 4', 0, 1, 'L');
  104. $pdf->AddPage();
  105. $pdf->Bookmark('Chapter 5', 0, 0, '', 'B', array(0,128,0));
  106. $pdf->Cell(0, 10, 'Chapter 5', 0, 1, 'L');
  107. $txt = 'Example of File Attachment.
  108. Double click on the icon to open the attached file.';
  109. $pdf->setFont('helvetica', '', 10);
  110. $pdf->Write(0, $txt, '', 0, 'L', true, 0, false, false, 0);
  111. // attach an external file TXT file
  112. $pdf->Annotation(20, 50, 5, 5, 'TXT file', array('Subtype'=>'FileAttachment', 'Name' => 'PushPin', 'FS' => 'data/utf8test.txt'));
  113. // attach an external file
  114. $pdf->Annotation(50, 50, 5, 5, 'PDF file', array('Subtype'=>'FileAttachment', 'Name' => 'PushPin', 'FS' => 'example_012.pdf'));
  115. // add a bookmark that points to an embedded file
  116. // NOTE: prefix the file name with the * character for generic file and with % character for PDF file
  117. $pdf->Bookmark('TXT file', 0, 0, '', 'B', array(128,0,255), -1, '*utf8test.txt');
  118. // add a bookmark that points to an embedded file
  119. // NOTE: prefix the file name with the * character for generic file and with % character for PDF file
  120. $pdf->Bookmark('PDF file', 0, 0, '', 'B', array(128,0,255), -1, '%example_012.pdf');
  121. // add a bookmark that points to an external URL
  122. $pdf->Bookmark('External URL', 0, 0, '', 'B', array(0,0,255), -1, 'http://www.tcpdf.org');
  123. // ---------------------------------------------------------
  124. //Close and output PDF document
  125. $pdf->Output('example_015.pdf', 'D');
  126. //============================================================+
  127. // END OF FILE
  128. //============================================================+