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.

138 lines
3.8 KiB

4 years ago
  1. <?php
  2. //============================================================+
  3. // File name : example_011.php
  4. // Begin : 2008-03-04
  5. // Last Update : 2013-05-14
  6. //
  7. // Description : Example 011 for TCPDF class
  8. // Colored Table (very simple table)
  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: Colored Table
  22. * @author Nicola Asuni
  23. * @since 2008-03-04
  24. */
  25. // Include the main TCPDF library (search for installation path).
  26. require_once('tcpdf_include.php');
  27. // extend TCPF with custom functions
  28. class MYPDF extends TCPDF {
  29. // Load table data from file
  30. public function LoadData($file) {
  31. // Read file lines
  32. $lines = file($file);
  33. $data = array();
  34. foreach($lines as $line) {
  35. $data[] = explode(';', chop($line));
  36. }
  37. return $data;
  38. }
  39. // Colored table
  40. public function ColoredTable($header,$data) {
  41. // Colors, line width and bold font
  42. $this->SetFillColor(255, 0, 0);
  43. $this->SetTextColor(255);
  44. $this->SetDrawColor(128, 0, 0);
  45. $this->SetLineWidth(0.3);
  46. $this->SetFont('', 'B');
  47. // Header
  48. $w = array(40, 35, 40, 45);
  49. $num_headers = count($header);
  50. for($i = 0; $i < $num_headers; ++$i) {
  51. $this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
  52. }
  53. $this->Ln();
  54. // Color and font restoration
  55. $this->SetFillColor(224, 235, 255);
  56. $this->SetTextColor(0);
  57. $this->SetFont('');
  58. // Data
  59. $fill = 0;
  60. foreach($data as $row) {
  61. $this->Cell($w[0], 6, $row[0], 'LR', 0, 'L', $fill);
  62. $this->Cell($w[1], 6, $row[1], 'LR', 0, 'L', $fill);
  63. $this->Cell($w[2], 6, number_format($row[2]), 'LR', 0, 'R', $fill);
  64. $this->Cell($w[3], 6, number_format($row[3]), 'LR', 0, 'R', $fill);
  65. $this->Ln();
  66. $fill=!$fill;
  67. }
  68. $this->Cell(array_sum($w), 0, '', 'T');
  69. }
  70. }
  71. // create new PDF document
  72. $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  73. // set document information
  74. $pdf->SetCreator(PDF_CREATOR);
  75. $pdf->SetAuthor('Nicola Asuni');
  76. $pdf->SetTitle('TCPDF Example 011');
  77. $pdf->SetSubject('TCPDF Tutorial');
  78. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  79. // set default header data
  80. $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 011', PDF_HEADER_STRING);
  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. // set default monospaced font
  85. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  86. // set margins
  87. $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  88. $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
  89. $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
  90. // set auto page breaks
  91. $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  92. // set image scale factor
  93. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  94. // set some language-dependent strings (optional)
  95. if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
  96. require_once(dirname(__FILE__).'/lang/eng.php');
  97. $pdf->setLanguageArray($l);
  98. }
  99. // ---------------------------------------------------------
  100. // set font
  101. $pdf->SetFont('helvetica', '', 12);
  102. // add a page
  103. $pdf->AddPage();
  104. // column titles
  105. $header = array('Country', 'Capital', 'Area (sq km)', 'Pop. (thousands)');
  106. // data loading
  107. $data = $pdf->LoadData('data/table_data_demo.txt');
  108. // print colored table
  109. $pdf->ColoredTable($header, $data);
  110. // ---------------------------------------------------------
  111. // close and output PDF document
  112. $pdf->Output('example_011.pdf', 'I');
  113. //============================================================+
  114. // END OF FILE
  115. //============================================================+