Penggunaan fungsi ANTIALIASE pada PHP

Semalem saya dapet pertanyaan dari salah satu pengunjung website ini terkait dengan tutorial membuat laporan PDF dengan FPDF yang pernah saya posting. Dia sudah mencobanya namun saat isi data pada suatu kolom melebihi lebar dari kolom tersebut, maka tulisannya akan kepotong alias ga keliatan. Saya juga sering mendapatkan pertanyaan serupa baik dari beberapa mahasiswa, pengunjung website achmatim.net, kawan-kawan di facebook maupun follower di twitter. Sehingga saya mencoba untuk membuat contoh program PHP sederhana mengenai membuat auto wrap content pada tabel ini.

Bagi Anda yang belum pernah menggunakan library FPDF sebaiknya mencoba tutorial dasarnya terlebih dahulu sebelum mencoba tutorial ini. Dan untuk menyederhanakan tutorial, data yang ditampilkan diambil dari sebuah array. Anda dapat memodifikasinya dengan mudah jika ingin dikoneksikan dengan sebuah database. Contoh Program PHP pada tutorial ini menggunakan library FPDF versi 1.7 yang dapat Anda download di situs resminya.

Source Code Program

Berikut ini contoh program beserta penjelasan singkatnya:

  1. <?php

  2. /**

  3.  * @author Achmad Solichin

  4.  * @website http://achmatim.net

  5.  * @email

  6.  */

  7. require_once("fpdf17/fpdf.php");

  8. class FPDF_AutoWrapTable extends FPDF {

  9. private $data = array();

  10. private $options = array(

  11. 'filename' => '',

  12. 'destinationfile' => '',

  13. 'paper_size'=>'F4',

  14. 'orientation'=>'P'

  15. );

  16. function __construct($data = array(), $options = array()) {

  17. parent::__construct();

  18. $this->data = $data;

  19. $this->options = $options;

  20. }

  21. public function rptDetailData () {

  22. //

  23. $border = 0;

  24. $this->AddPage();

  25. $this->SetAutoPageBreak(true,60);

  26. $this->AliasNbPages();

  27. $left = 25;

  28. //header

  29. $this->SetFont("", "B", 15);

  30. $this->MultiCell(0, 12, 'PT. ACHMATIM DOT NET');

  31. $this->Cell(0, 1, " ", "B");

  32. $this->Ln(10);

  33. $this->SetFont("", "B", 12);

  34. $this->SetX($left); $this->Cell(0, 10, 'LAPORAN DATA KARYAWAN', 0, 1,'C');

  35. $this->Ln(10);

  36. $h = 13;

  37. $left = 40;

  38. $top = 80;

  39. #tableheader

  40. $this->SetFillColor(200,200,200);

  41. $left = $this->GetX();

  42. $this->Cell(20,$h,'NO',1,0,'L',true);

  43. $this->SetX($left += 20); $this->Cell(75, $h, 'NIP', 1, 0, 'C',true);

  44. $this->SetX($left += 75); $this->Cell(100, $h, 'NAMA', 1, 0, 'C',true);

  45. $this->SetX($left += 100); $this->Cell(150, $h, 'ALAMAT', 1, 0, 'C',true);

  46. $this->SetX($left += 150); $this->Cell(100, $h, 'EMAIL', 1, 0, 'C',true);

  47. $this->SetX($left += 100); $this->Cell(100, $h, 'WEBSITE', 1, 1, 'C',true);

  48. //$this->Ln(20);

  49. $this->SetFont('Arial','',9);

  50. $this->SetWidths(array(20,75,100,150,100,100));

  51. $this->SetAligns(array('C','L','L','L','L','L'));

  52. $no = 1; $this->SetFillColor(255);

  53. foreach ($this->data as $baris) {

  54. $this->Row(

  55. $baris['nip'],

  56. $baris['nama'],

  57. $baris['alamat'],

  58. $baris['email'],

  59. $baris['website']

  60. ));

  61. }

  62. }

  63. public function printPDF () {

  64. if ($this->options['paper_size'] == "F4") {

  65. $a = 8.3 * 72; //1 inch = 72 pt

  66. $b = 13.0 * 72;

  67. $this->FPDF($this->options['orientation'], "pt", array($a,$b));

  68. } else {

  69. $this->FPDF($this->options['orientation'], "pt", $this->options['paper_size']);

  70. }

  71. $this->SetAutoPageBreak(false);

  72. $this->AliasNbPages();

  73. $this->SetFont("helvetica", "B", 10);

  74. //$this->AddPage();

  75. $this->rptDetailData();

  76. $this->Output($this->options['filename'],$this->options['destinationfile']);

  77. }

  78. private $widths;

  79. private $aligns;

  80. function SetWidths($w)

  81. {

  82. //Set the array of column widths

  83. $this->widths=$w;

  84. }

  85. function SetAligns($a)

  86. {

  87. //Set the array of column alignments

  88. $this->aligns=$a;

  89. }

  90. function Row($data)

  91. {

  92. //Calculate the height of the row

  93. $nb=0;

  94. for($i=0;$i<count($data);$i++)

  95. $nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));

  96. $h=10*$nb;

  97. //Issue a page break first if needed

  98. $this->CheckPageBreak($h);

  99. //Draw the cells of the row

  100. for($i=0;$i<count($data);$i++)

  101. {

  102. $w=$this->widths[$i];

  103. $a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';

  104. //Save the current position

  105. $x=$this->GetX();

  106. $y=$this->GetY();

  107. //Draw the border

  108. $this->Rect($x,$y,$w,$h);

  109. //Print the text

  110. $this->MultiCell($w,10,$data[$i],0,$a);

  111. //Put the position to the right of the cell

  112. $this->SetXY($x+$w,$y);

  113. }

  114. //Go to the next line

  115. $this->Ln($h);

  116. }

  117. function CheckPageBreak($h)

  118. {

  119. //If the height h would cause an overflow, add a new page immediately

  120. if($this->GetY()+$h>$this->PageBreakTrigger)

  121. $this->AddPage($this->CurOrientation);

  122. }

  123. function NbLines($w,$txt)

  124. {

  125. //Computes the number of lines a MultiCell of width w will take

  126. $cw=&$this->CurrentFont['cw'];

  127. if($w==0)

  128. $w=$this->w-$this->rMargin-$this->x;

  129. $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;

  130. if($nb>0 and $s[$nb-1]=="\n")

  131. $nb--;

  132. $sep=-1;

  133. $i=0;

  134. $j=0;

  135. $l=0;

  136. $nl=1;

  137. while($i<$nb)

  138. {

  139. $c=$s[$i];

  140. if($c=="\n")

  141. {

  142. $i++;

  143. $sep=-1;

  144. $j=$i;

  145. $l=0;

  146. $nl++;

  147. continue;

  148. }

  149. if($c==' ')

  150. $sep=$i;

  151. $l+=$cw[$c];

  152. if($l>$wmax)

  153. {

  154. if($sep==-1)

  155. {

  156. if($i==$j)

  157. $i++;

  158. }

  159. else

  160. $i=$sep+1;

  161. $sep=-1;

  162. $j=$i;

  163. $l=0;

  164. $nl++;

  165. }

  166. else

  167. $i++;

  168. }

  169. return $nl;

  170. }

  171. } //end of class

  172. //contoh penggunaan

  173. 'nip' => '0111500382',

  174. 'nama' => 'ACHMAD SOLICHIN',

  175. 'alamat' => 'Jalan Ciledug Raya No 99, Petukangan Utara, Jakarta Selatan 12260, DKI Jakarta',

  176. 'email' => '',

  177. 'website' => 'http://achmatim.net'

  178. ),

  179. 'nip' => '0411500101',

  180. 'nama' => 'CHOTIMATUL MUSYAROFAH',

  181. 'alamat' => 'Komplek Japos RT 002/015 Kelurahan Peninggilan, Kec. Ciledug, Tangerang',

  182. 'email' => '',

  183. 'website' => 'http://contohprogram.info'

  184. ),

  185. 'nip' => '1111500200',

  186. 'nama' => 'MUHAMMAD LINTANG',

  187. 'alamat' => 'Jl. Raya Caplin, Kec. Ciledug, Tangerang, Banten',

  188. 'email' => '',

  189. 'website' => 'http://ebook.achmatim.net'

  190. )

  191. );

  192. //pilihan

  193. 'filename' => '', //nama file penyimpanan, kosongkan jika output ke browser

  194. 'destinationfile' => '', //I=inline browser (default), F=local file, D=download

  195. 'paper_size'=>'F4', //paper size: F4, A3, A4, A5, Letter, Legal

  196. 'orientation'=>'P' //orientation: P=portrait, L=landscape

  197. );

  198. $tabel = new FPDF_AutoWrapTable($data, $options);

  199. $tabel->printPDF();

  200. ?>

Penjelasan singkat program:

  • Baris 8. Menyertakan library FPDF. Pada tutorial ini, menggunakan library FPDF versi 1.7 dan diletakkan di folder /fpdf17
  • Baris 10-197. Mendefinisikan class FPDF_AutoWrapTable yang merupakan turunan dari class FPDF.
  • Baris 11-17. Mendeklarasikan variabel $data berisi data yang akan ditampilkan dan $options berisi beberapa opsi tampilan PDF seperti ukuran kertas dan orientasi kertas.
  • Baris 19-23. Konstruktor dari class yang menangkap 2 parameter yaitu $data dan $options
  • Baris 34-40. Mengatur header / judul laporan, termasuk ukuran font (baris 35 dan 38)
  • Baris 43-54. Mengatur judul tabel (kolom-kolom tabel) termasuk teks judul, lebar kolom dan perataan kolom.
  • Baris 57-70. Menampilkan data tabel dengan memanggil fungsi Row(). Pada baris 58 diatur lebar untuk setiap kolom dan pada baris 59 diatur perataan untuk setiap kolom.
  • Baris 75-93. Fungsi untuk menggenerate output PDF.
  • Baris 200-222. Setting data yang akan ditampilkan. Jika ingin dikoneksikan dengan database, ganti baris ini dengan perintah untuk mengambil data.
  • Baris 232-233. Pembentukan object class FPDF_AutoWrapTable dan pemanggilan fungsi untuk meng-generate file PDF.

Hasil dari program tersebut kurang lebih sebagai berikut:

Penggunaan fungsi ANTIALIASE pada PHP

Download dan Demo Program

  • Klik disini untuk mendownload contoh program diatas beserta library FPDF versi 1.7
  • Klik disini untuk melihat demo program di atas.

Menampilkan Data MySQL ke PDF

Pada contoh program di atas, saya menggunakan data berupa array yang diisikan di dalam program. Anda bisa saja menggantinya dengan data yang berasal dari database seperti MySQL. Dan berhubung cukup banyak pengunjung yang bertanya mengenai bagaimana jika ingin menampilkan data dari database ke PDF, saya buatkan contoh programnya di bawah ini. Jangan lupa, siapkan terlebih dahulu database dan tabel yang diperlukan (lihat penjelasan di code program).

  1. <?php

  2. /**

  3.  * @author Achmad Solichin

  4.  * @website http://achmatim.net

  5.  * @email

  6.  */

  7. require_once("fpdf17/fpdf.php");

  8. class FPDF_AutoWrapTable extends FPDF {

  9. private $data = array();

  10. private $options = array(

  11. 'filename' => '',

  12. 'destinationfile' => '',

  13. 'paper_size'=>'F4',

  14. 'orientation'=>'P'

  15. );

  16. function __construct($data = array(), $options = array()) {

  17. parent::__construct();

  18. $this->data = $data;

  19. $this->options = $options;

  20. }

  21. public function rptDetailData () {

  22. //

  23. $border = 0;

  24. $this->AddPage();

  25. $this->SetAutoPageBreak(true,60);

  26. $this->AliasNbPages();

  27. $left = 25;

  28. //header

  29. $this->SetFont("", "B", 15);

  30. $this->MultiCell(0, 12, 'PT. ACHMATIM DOT NET');

  31. $this->Cell(0, 1, " ", "B");

  32. $this->Ln(10);

  33. $this->SetFont("", "B", 12);

  34. $this->SetX($left); $this->Cell(0, 10, 'LAPORAN DATA KARYAWAN', 0, 1,'C');

  35. $this->Ln(10);

  36. $h = 13;

  37. $left = 40;

  38. $top = 80;

  39. #tableheader

  40. $this->SetFillColor(200,200,200);

  41. $left = $this->GetX();

  42. $this->Cell(20,$h,'NO',1,0,'L',true);

  43. $this->SetX($left += 20); $this->Cell(75, $h, 'NIP', 1, 0, 'C',true);

  44. $this->SetX($left += 75); $this->Cell(100, $h, 'NAMA', 1, 0, 'C',true);

  45. $this->SetX($left += 100); $this->Cell(150, $h, 'ALAMAT', 1, 0, 'C',true);

  46. $this->SetX($left += 150); $this->Cell(100, $h, 'EMAIL', 1, 0, 'C',true);

  47. $this->SetX($left += 100); $this->Cell(100, $h, 'WEBSITE', 1, 1, 'C',true);

  48. //$this->Ln(20);

  49. $this->SetFont('Arial','',9);

  50. $this->SetWidths(array(20,75,100,150,100,100));

  51. $this->SetAligns(array('C','L','L','L','L','L'));

  52. $no = 1; $this->SetFillColor(255);

  53. foreach ($this->data as $baris) {

  54. $this->Row(

  55. $baris['nip'],

  56. $baris['nama'],

  57. $baris['alamat'],

  58. $baris['email'],

  59. $baris['website']

  60. ));

  61. }

  62. }

  63. public function printPDF () {

  64. if ($this->options['paper_size'] == "F4") {

  65. $a = 8.3 * 72; //1 inch = 72 pt

  66. $b = 13.0 * 72;

  67. $this->FPDF($this->options['orientation'], "pt", array($a,$b));

  68. } else {

  69. $this->FPDF($this->options['orientation'], "pt", $this->options['paper_size']);

  70. }

  71. $this->SetAutoPageBreak(false);

  72. $this->AliasNbPages();

  73. $this->SetFont("helvetica", "B", 10);

  74. //$this->AddPage();

  75. $this->rptDetailData();

  76. $this->Output($this->options['filename'],$this->options['destinationfile']);

  77. }

  78. private $widths;

  79. private $aligns;

  80. function SetWidths($w)

  81. {

  82. //Set the array of column widths

  83. $this->widths=$w;

  84. }

  85. function SetAligns($a)

  86. {

  87. //Set the array of column alignments

  88. $this->aligns=$a;

  89. }

  90. function Row($data)

  91. {

  92. //Calculate the height of the row

  93. $nb=0;

  94. for($i=0;$i<count($data);$i++)

  95. $nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));

  96. $h=10*$nb;

  97. //Issue a page break first if needed

  98. $this->CheckPageBreak($h);

  99. //Draw the cells of the row

  100. for($i=0;$i<count($data);$i++)

  101. {

  102. $w=$this->widths[$i];

  103. $a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';

  104. //Save the current position

  105. $x=$this->GetX();

  106. $y=$this->GetY();

  107. //Draw the border

  108. $this->Rect($x,$y,$w,$h);

  109. //Print the text

  110. $this->MultiCell($w,10,$data[$i],0,$a);

  111. //Put the position to the right of the cell

  112. $this->SetXY($x+$w,$y);

  113. }

  114. //Go to the next line

  115. $this->Ln($h);

  116. }

  117. function CheckPageBreak($h)

  118. {

  119. //If the height h would cause an overflow, add a new page immediately

  120. if($this->GetY()+$h>$this->PageBreakTrigger)

  121. $this->AddPage($this->CurOrientation);

  122. }

  123. function NbLines($w,$txt)

  124. {

  125. //Computes the number of lines a MultiCell of width w will take

  126. $cw=&$this->CurrentFont['cw'];

  127. if($w==0)

  128. $w=$this->w-$this->rMargin-$this->x;

  129. $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;

  130. if($nb>0 and $s[$nb-1]=="\n")

  131. $nb--;

  132. $sep=-1;

  133. $i=0;

  134. $j=0;

  135. $l=0;

  136. $nl=1;

  137. while($i<$nb)

  138. {

  139. $c=$s[$i];

  140. if($c=="\n")

  141. {

  142. $i++;

  143. $sep=-1;

  144. $j=$i;

  145. $l=0;

  146. $nl++;

  147. continue;

  148. }

  149. if($c==' ')

  150. $sep=$i;

  151. $l+=$cw[$c];

  152. if($l>$wmax)

  153. {

  154. if($sep==-1)

  155. {

  156. if($i==$j)

  157. $i++;

  158. }

  159. else

  160. $i=$sep+1;

  161. $sep=-1;

  162. $j=$i;

  163. $l=0;

  164. $nl++;

  165. }

  166. else

  167. $i++;

  168. }

  169. return $nl;

  170. }

  171. } //end of class

  172. /* contoh penggunaan dengan data diambil dari database mysql

  173.  *

  174.  * 1. buatlah database di mysql

  175.  * 2. buatlah tabel 'pegawai' dengan field: nip, nama, alamat, email dan website

  176.  * 3. isikan beberapa contoh data ke tabel pegawai tersebut.

  177.  *

  178.  * */

  179. #koneksi ke database (disederhanakan)

  180. #ambil data dari DB dan masukkan ke array

  181. $query = "SELECT nip, nama, alamat, email, website FROM pegawai ORDER BY nama";

  182. }

  183. //pilihan

  184. 'filename' => '', //nama file penyimpanan, kosongkan jika output ke browser

  185. 'destinationfile' => '', //I=inline browser (default), F=local file, D=download

  186. 'paper_size'=>'F4', //paper size: F4, A3, A4, A5, Letter, Legal

  187. 'orientation'=>'P' //orientation: P=portrait, L=landscape

  188. );

  189. $tabel = new FPDF_AutoWrapTable($data, $options);

  190. $tabel->printPDF();

  191. ?>

Referensi Tutorial

  • Step by Step PHP Membuat Laporan PDF dengan FPDF
  • Dokumentasi FPDF
  • Table with Multicells

Jika tutorial ini bermanfaat, berbagilah ke sebanyak-banyaknya orang maka Anda akan mendapatkan manfaat yang lebih banyak. Semoga bermanfaat dan maju terus ilmu pengetahuan Indonesia.

Please follow and like us:

Penggunaan fungsi ANTIALIASE pada PHP