example_003.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. //============================================================+
  3. // File name : example_003.php
  4. // Begin : 2008-03-04
  5. // Last Update : 2013-05-14
  6. //
  7. // Description : Example 003 for TCPDF class
  8. // Custom Header and Footer
  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: Custom Header and Footer
  22. * @author Nicola Asuni
  23. * @since 2008-03-04
  24. * @group header
  25. * @group footer
  26. * @group page
  27. * @group pdf
  28. */
  29. // Include the main TCPDF library (search for installation path).
  30. require_once('tcpdf_include.php');
  31. // Extend the TCPDF class to create custom Header and Footer
  32. class MYPDF extends TCPDF {
  33. //Page header
  34. public function Header() {
  35. // Logo
  36. $image_file = K_PATH_IMAGES.'logo_example.jpg';
  37. $this->Image($image_file, 10, 10, 15, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false);
  38. // Set font
  39. $this->setFont('helvetica', 'B', 20);
  40. // Title
  41. $this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M');
  42. }
  43. // Page footer
  44. public function Footer() {
  45. // Position at 15 mm from bottom
  46. $this->setY(-15);
  47. // Set font
  48. $this->setFont('helvetica', 'I', 8);
  49. // Page number
  50. $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
  51. }
  52. }
  53. // create new PDF document
  54. $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  55. // set document information
  56. $pdf->setCreator(PDF_CREATOR);
  57. $pdf->setAuthor('Nicola Asuni');
  58. $pdf->setTitle('TCPDF Example 003');
  59. $pdf->setSubject('TCPDF Tutorial');
  60. $pdf->setKeywords('TCPDF, PDF, example, test, guide');
  61. // set default header data
  62. $pdf->setHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
  63. // set header and footer fonts
  64. $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
  65. $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
  66. // set default monospaced font
  67. $pdf->setDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  68. // set margins
  69. $pdf->setMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  70. $pdf->setHeaderMargin(PDF_MARGIN_HEADER);
  71. $pdf->setFooterMargin(PDF_MARGIN_FOOTER);
  72. // set auto page breaks
  73. $pdf->setAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  74. // set image scale factor
  75. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  76. // set some language-dependent strings (optional)
  77. if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
  78. require_once(dirname(__FILE__).'/lang/eng.php');
  79. $pdf->setLanguageArray($l);
  80. }
  81. // ---------------------------------------------------------
  82. // set font
  83. $pdf->setFont('times', 'BI', 12);
  84. // add a page
  85. $pdf->AddPage();
  86. // set some text to print
  87. $txt = <<<EOD
  88. TCPDF Example 003
  89. Custom page header and footer are defined by extending the TCPDF class and overriding the Header() and Footer() methods.
  90. EOD;
  91. // print a block of text using Write()
  92. $pdf->Write(0, $txt, '', 0, 'C', true, 0, false, false, 0);
  93. // ---------------------------------------------------------
  94. //Close and output PDF document
  95. $pdf->Output('example_003.pdf', 'I');
  96. //============================================================+
  97. // END OF FILE
  98. //============================================================+