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:
<?php
/**
* @author Achmad Solichin
* @website //achmatim.net
* @email
*/
require_once("fpdf17/fpdf.php");
class FPDF_AutoWrapTable extends FPDF {
private $data = array();
private $options = array(
'filename' => '',
'destinationfile' => '',
'paper_size'=>'F4',
'orientation'=>'P'
);
function __construct($data = array(), $options = array()) {
parent::__construct();
$this->data = $data;
$this->options = $options;
}
public function rptDetailData () {
//
$border = 0;
$this->AddPage();
$this->SetAutoPageBreak(true,60);
$this->AliasNbPages();
$left = 25;
//header
$this->SetFont("", "B", 15);
$this->MultiCell(0, 12, 'PT. ACHMATIM DOT NET');
$this->Cell(0, 1, " ", "B");
$this->Ln(10);
$this->SetFont("", "B", 12);
$this->SetX($left); $this->Cell(0, 10, 'LAPORAN DATA KARYAWAN', 0, 1,'C');
$this->Ln(10);
$h = 13;
$left = 40;
$top = 80;
#tableheader
$this->SetFillColor(200,200,200);
$left = $this->GetX();
$this->Cell(20,$h,'NO',1,0,'L',true);
$this->SetX($left += 20); $this->Cell(75, $h, 'NIP', 1, 0, 'C',true);
$this->SetX($left += 75); $this->Cell(100, $h, 'NAMA', 1, 0, 'C',true);
$this->SetX($left += 100); $this->Cell(150, $h, 'ALAMAT', 1, 0, 'C',true);
$this->SetX($left += 150); $this->Cell(100, $h, 'EMAIL', 1, 0, 'C',true);
$this->SetX($left += 100); $this->Cell(100, $h, 'WEBSITE', 1, 1, 'C',true);
//$this->Ln(20);
$this->SetFont('Arial','',9);
$this->SetWidths(array(20,75,100,150,100,100));
$this->SetAligns(array('C','L','L','L','L','L'));
$no = 1; $this->SetFillColor(255);
foreach ($this->data as $baris) {
$this->Row(
$baris['nip'],
$baris['nama'],
$baris['alamat'],
$baris['email'],
$baris['website']
));
}
}
public function printPDF () {
if ($this->options['paper_size'] == "F4") {
$a = 8.3 * 72; //1 inch = 72 pt
$b = 13.0 * 72;
$this->FPDF($this->options['orientation'], "pt", array($a,$b));
} else {
$this->FPDF($this->options['orientation'], "pt", $this->options['paper_size']);
}
$this->SetAutoPageBreak(false);
$this->AliasNbPages();
$this->SetFont("helvetica", "B", 10);
//$this->AddPage();
$this->rptDetailData();
$this->Output($this->options['filename'],$this->options['destinationfile']);
}
private $widths;
private $aligns;
function SetWidths($w)
{
//Set the array of column widths
$this->widths=$w;
}
function SetAligns($a)
{
//Set the array of column alignments
$this->aligns=$a;
}
function Row($data)
{
//Calculate the height of the row
$nb=0;
for($i=0;$i<count($data);$i++)
$nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
$h=10*$nb;
//Issue a page break first if needed
$this->CheckPageBreak($h);
//Draw the cells of the row
for($i=0;$i<count($data);$i++)
{
$w=$this->widths[$i];
$a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
//Save the current position
$x=$this->GetX();
$y=$this->GetY();
//Draw the border
$this->Rect($x,$y,$w,$h);
//Print the text
$this->MultiCell($w,10,$data[$i],0,$a);
//Put the position to the right of the cell
$this->SetXY($x+$w,$y);
}
//Go to the next line
$this->Ln($h);
}
function CheckPageBreak($h)
{
//If the height h would cause an overflow, add a new page immediately
if($this->GetY()+$h>$this->PageBreakTrigger)
$this->AddPage($this->CurOrientation);
}
function NbLines($w,$txt)
{
//Computes the number of lines a MultiCell of width w will take
$cw=&$this->CurrentFont['cw'];
if($w==0)
$w=$this->w-$this->rMargin-$this->x;
$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
if($nb>0 and $s[$nb-1]=="\n")
$nb--;
$sep=-1;
$i=0;
$j=0;
$l=0;
$nl=1;
while($i<$nb)
{
$c=$s[$i];
if($c=="\n")
{
$i++;
$sep=-1;
$j=$i;
$l=0;
$nl++;
continue;
}
if($c==' ')
$sep=$i;
$l+=$cw[$c];
if($l>$wmax)
{
if($sep==-1)
{
if($i==$j)
$i++;
}
else
$i=$sep+1;
$sep=-1;
$j=$i;
$l=0;
$nl++;
}
else
$i++;
}
return $nl;
}
} //end of class
//contoh penggunaan
'nip' => '0111500382',
'nama' => 'ACHMAD SOLICHIN',
'alamat' => 'Jalan Ciledug Raya No 99, Petukangan Utara, Jakarta Selatan 12260, DKI Jakarta',
'email' => '',
'website' => '//achmatim.net'
),
'nip' => '0411500101',
'nama' => 'CHOTIMATUL MUSYAROFAH',
'alamat' => 'Komplek Japos RT 002/015 Kelurahan Peninggilan, Kec. Ciledug, Tangerang',
'email' => '',
'website' => '//contohprogram.info'
),
'nip' => '1111500200',
'nama' => 'MUHAMMAD LINTANG',
'alamat' => 'Jl. Raya Caplin, Kec. Ciledug, Tangerang, Banten',
'email' => '',
'website' => '//ebook.achmatim.net'
)
);
//pilihan
'filename' => '', //nama file penyimpanan, kosongkan jika output ke browser
'destinationfile' => '', //I=inline browser (default), F=local file, D=download
'paper_size'=>'F4', //paper size: F4, A3, A4, A5, Letter, Legal
'orientation'=>'P' //orientation: P=portrait, L=landscape
);
$tabel = new FPDF_AutoWrapTable($data, $options);
$tabel->printPDF();
?>
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:
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).
<?php
/**
* @author Achmad Solichin
* @website //achmatim.net
* @email
*/
require_once("fpdf17/fpdf.php");
class FPDF_AutoWrapTable extends FPDF {
private $data = array();
private $options = array(
'filename' => '',
'destinationfile' => '',
'paper_size'=>'F4',
'orientation'=>'P'
);
function __construct($data = array(), $options = array()) {
parent::__construct();
$this->data = $data;
$this->options = $options;
}
public function rptDetailData () {
//
$border = 0;
$this->AddPage();
$this->SetAutoPageBreak(true,60);
$this->AliasNbPages();
$left = 25;
//header
$this->SetFont("", "B", 15);
$this->MultiCell(0, 12, 'PT. ACHMATIM DOT NET');
$this->Cell(0, 1, " ", "B");
$this->Ln(10);
$this->SetFont("", "B", 12);
$this->SetX($left); $this->Cell(0, 10, 'LAPORAN DATA KARYAWAN', 0, 1,'C');
$this->Ln(10);
$h = 13;
$left = 40;
$top = 80;
#tableheader
$this->SetFillColor(200,200,200);
$left = $this->GetX();
$this->Cell(20,$h,'NO',1,0,'L',true);
$this->SetX($left += 20); $this->Cell(75, $h, 'NIP', 1, 0, 'C',true);
$this->SetX($left += 75); $this->Cell(100, $h, 'NAMA', 1, 0, 'C',true);
$this->SetX($left += 100); $this->Cell(150, $h, 'ALAMAT', 1, 0, 'C',true);
$this->SetX($left += 150); $this->Cell(100, $h, 'EMAIL', 1, 0, 'C',true);
$this->SetX($left += 100); $this->Cell(100, $h, 'WEBSITE', 1, 1, 'C',true);
//$this->Ln(20);
$this->SetFont('Arial','',9);
$this->SetWidths(array(20,75,100,150,100,100));
$this->SetAligns(array('C','L','L','L','L','L'));
$no = 1; $this->SetFillColor(255);
foreach ($this->data as $baris) {
$this->Row(
$baris['nip'],
$baris['nama'],
$baris['alamat'],
$baris['email'],
$baris['website']
));
}
}
public function printPDF () {
if ($this->options['paper_size'] == "F4") {
$a = 8.3 * 72; //1 inch = 72 pt
$b = 13.0 * 72;
$this->FPDF($this->options['orientation'], "pt", array($a,$b));
} else {
$this->FPDF($this->options['orientation'], "pt", $this->options['paper_size']);
}
$this->SetAutoPageBreak(false);
$this->AliasNbPages();
$this->SetFont("helvetica", "B", 10);
//$this->AddPage();
$this->rptDetailData();
$this->Output($this->options['filename'],$this->options['destinationfile']);
}
private $widths;
private $aligns;
function SetWidths($w)
{
//Set the array of column widths
$this->widths=$w;
}
function SetAligns($a)
{
//Set the array of column alignments
$this->aligns=$a;
}
function Row($data)
{
//Calculate the height of the row
$nb=0;
for($i=0;$i<count($data);$i++)
$nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
$h=10*$nb;
//Issue a page break first if needed
$this->CheckPageBreak($h);
//Draw the cells of the row
for($i=0;$i<count($data);$i++)
{
$w=$this->widths[$i];
$a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
//Save the current position
$x=$this->GetX();
$y=$this->GetY();
//Draw the border
$this->Rect($x,$y,$w,$h);
//Print the text
$this->MultiCell($w,10,$data[$i],0,$a);
//Put the position to the right of the cell
$this->SetXY($x+$w,$y);
}
//Go to the next line
$this->Ln($h);
}
function CheckPageBreak($h)
{
//If the height h would cause an overflow, add a new page immediately
if($this->GetY()+$h>$this->PageBreakTrigger)
$this->AddPage($this->CurOrientation);
}
function NbLines($w,$txt)
{
//Computes the number of lines a MultiCell of width w will take
$cw=&$this->CurrentFont['cw'];
if($w==0)
$w=$this->w-$this->rMargin-$this->x;
$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
if($nb>0 and $s[$nb-1]=="\n")
$nb--;
$sep=-1;
$i=0;
$j=0;
$l=0;
$nl=1;
while($i<$nb)
{
$c=$s[$i];
if($c=="\n")
{
$i++;
$sep=-1;
$j=$i;
$l=0;
$nl++;
continue;
}
if($c==' ')
$sep=$i;
$l+=$cw[$c];
if($l>$wmax)
{
if($sep==-1)
{
if($i==$j)
$i++;
}
else
$i=$sep+1;
$sep=-1;
$j=$i;
$l=0;
$nl++;
}
else
$i++;
}
return $nl;
}
} //end of class
/* contoh penggunaan dengan data diambil dari database mysql
*
* 1. buatlah database di mysql
* 2. buatlah tabel 'pegawai' dengan field: nip, nama, alamat, email dan website
* 3. isikan beberapa contoh data ke tabel pegawai tersebut.
*
* */
#koneksi ke database (disederhanakan)
#ambil data dari DB dan masukkan ke array
$query = "SELECT nip, nama, alamat, email, website FROM pegawai ORDER BY nama";
}
//pilihan
'filename' => '', //nama file penyimpanan, kosongkan jika output ke browser
'destinationfile' => '', //I=inline browser (default), F=local file, D=download
'paper_size'=>'F4', //paper size: F4, A3, A4, A5, Letter, Legal
'orientation'=>'P' //orientation: P=portrait, L=landscape
);
$tabel = new FPDF_AutoWrapTable($data, $options);
$tabel->printPDF();
?>
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: