example_011.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. //============================================================+
  3. // File name : example_011.php
  4. // Begin : 2008-03-04
  5. // Last Update : 2010-08-08
  6. //
  7. // Description : Example 011 for TCPDF class
  8. // Colored Table
  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: Colored Table
  25. * @author Nicola Asuni
  26. * @since 2008-03-04
  27. */
  28. require_once('../config/lang/eng.php');
  29. require_once('../tcpdf.php');
  30. // extend TCPF with custom functions
  31. class MYPDF extends TCPDF {
  32. // Load table data from file
  33. public function LoadData($file) {
  34. // Read file lines
  35. $lines = file($file);
  36. $data = array();
  37. foreach($lines as $line) {
  38. $data[] = explode(';', chop($line));
  39. }
  40. return $data;
  41. }
  42. // Colored table
  43. public function ColoredTable($header,$data) {
  44. // Colors, line width and bold font
  45. $this->SetFillColor(255, 0, 0);
  46. $this->SetTextColor(255);
  47. $this->SetDrawColor(128, 0, 0);
  48. $this->SetLineWidth(0.3);
  49. $this->SetFont('', 'B');
  50. // Header
  51. $w = array(40, 35, 40, 45);
  52. $num_headers = count($header);
  53. for($i = 0; $i < $num_headers; ++$i) {
  54. $this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
  55. }
  56. $this->Ln();
  57. // Color and font restoration
  58. $this->SetFillColor(224, 235, 255);
  59. $this->SetTextColor(0);
  60. $this->SetFont('');
  61. // Data
  62. $fill = 0;
  63. foreach($data as $row) {
  64. $this->Cell($w[0], 6, $row[0], 'LR', 0, 'L', $fill);
  65. $this->Cell($w[1], 6, $row[1], 'LR', 0, 'L', $fill);
  66. $this->Cell($w[2], 6, number_format($row[2]), 'LR', 0, 'R', $fill);
  67. $this->Cell($w[3], 6, number_format($row[3]), 'LR', 0, 'R', $fill);
  68. $this->Ln();
  69. $fill=!$fill;
  70. }
  71. $this->Cell(array_sum($w), 0, '', 'T');
  72. }
  73. }
  74. // create new PDF document
  75. $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  76. // set document information
  77. $pdf->SetCreator(PDF_CREATOR);
  78. $pdf->SetAuthor('Nicola Asuni');
  79. $pdf->SetTitle('TCPDF Example 011');
  80. $pdf->SetSubject('TCPDF Tutorial');
  81. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  82. // set default header data
  83. $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 011', PDF_HEADER_STRING);
  84. // set header and footer fonts
  85. $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
  86. $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
  87. // set default monospaced font
  88. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  89. //set margins
  90. $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  91. $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
  92. $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
  93. //set auto page breaks
  94. $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  95. //set image scale factor
  96. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  97. //set some language-dependent strings
  98. $pdf->setLanguageArray($l);
  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('../cache/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. //============================================================+