tutorialswb

TutorialsWB hadir sebagai wadah bagi teman-teman yang ingin "Belajar PHP Dasar". Sehingga kami yakin bahwa TutorialsWB akan menjadi Pusat "Belajar Bahasa Pemrograman PHP" Disamping PHP website ini menyediakan berbagai tips dan trik Blogger. Kami menerima Pemasangan iklan banner atau artikel.

Hubungi Saya

Membuat Laporan PDF menggunakan PHP FPDF

Selamat malam sobat blogger kali ini kali kami akan share bagaimana membuat laporan PDF menggunakan PHP FPDF. Pada artikel sebelumnya kami telah membahas artikel yang sama, tetapi kali ini kami akan memberikan contoh yang lebih banyak dengan beberapa format pula.

Langsung saja sebelum kalian mulai kalian bisa mendownload library FPDF diwebsite resminya FPDF. Berikut beberapa format laporan FPDF yang bisa anda gunakan dalam project anda.

1. CellMarging
cell marging


 <?php
require('fpdf17/fpdf.php');

//A4 width : 219mm
//default margin : 10mm each side
//writable horizontal : 219-(10*2)=189mm

$pdf = new FPDF('P','mm','A4');

$pdf->AddPage();

//set font to arial, bold, 14pt
$pdf->SetFont('Arial','',12);

//Cell(width , height , text , border , end line , [align] )

//normal row height=5.

$pdf->Cell(20,10,'ID',1,0); //vertically merged cell, height=2x row height=2x5=10
$pdf->Cell(50,10,'Name',1,0); //vertically merged cell
$pdf->Cell(100,5,'Score',1,0); //normal height, but occupy 4 columns (horizontally merged)
$pdf->Cell(20,10,'Passing',1,0); //vertically merged cell
$pdf->Cell(0,5,'',0,1); //dummy line ending, height=5(normal row height) width=09 should be invisible 

//second line(row)
$pdf->Cell(70,5,'',0,0); //dummy cell to align next cell, should be invisible
$pdf->Cell(25,5,'q1',1,0);
$pdf->Cell(25,5,'q2',1,0);
$pdf->Cell(25,5,'q3',1,0);
$pdf->Cell(25,5,'q4',1,1);

//data rows
$pdf->Cell(20,5,'',1,0);
$pdf->Cell(50,5,'',1,0);
$pdf->Cell(25,5,'',1,0);
$pdf->Cell(25,5,'',1,0);
$pdf->Cell(25,5,'',1,0);
$pdf->Cell(25,5,'',1,0);
$pdf->Cell(20,5,'',1,0);
//....



$pdf->Output();
?>
 

2. Chart

chart


 <?php
require('fpdf17/fpdf.php');

$pdf = new FPDF('P','mm','A4'); //use new class
$pdf->AddPage();

//   chart properties
//position
$chartX=10;
$chartY=10;

//dimension
$chartWidth=150;
$chartHeight=100;

//padding
$chartTopPadding=10;
$chartLeftPadding=20;
$chartBottomPadding=20;
$chartRightPadding=5;

//chart box
$chartBoxX=$chartX+$chartLeftPadding;
$chartBoxY=$chartY+$chartTopPadding;
$chartBoxWidth=$chartWidth-$chartLeftPadding-$chartRightPadding;
$chartBoxHeight=$chartHeight-$chartBottomPadding-$chartTopPadding;

//bar width
$barWidth=20;

//chart data
$data=Array(
 'lorem'=>[
  'color'=>[255,0,0],
  'value'=>100],
 'ipsum'=>[
  'color'=>[255,255,0],
  'value'=>300],
 'dolor'=>[
  'color'=>[50,0,255],
  'value'=>150],
 'sit'=>[
  'color'=>[255,0,255],
  'value'=>50],
 'amet'=>[
  'color'=>[0,255,0],
  'value'=>240]
 );

//$dataMax
$dataMax=0;
foreach($data as $item){
 if($item['value']>$dataMax)$dataMax=$item['value'];
}

//data step
$dataStep=50;

//set font, line width and color
$pdf->SetFont('Arial','',9);
$pdf->SetLineWidth(0.2);
$pdf->SetDrawColor(0);

//chart boundary
$pdf->Rect($chartX,$chartY,$chartWidth,$chartHeight);

//vertical axis line
$pdf->Line(
 $chartBoxX ,
 $chartBoxY , 
 $chartBoxX , 
 ($chartBoxY+$chartBoxHeight)
 );
//horizontal axis line
$pdf->Line(
 $chartBoxX-2 , 
 ($chartBoxY+$chartBoxHeight) , 
 $chartBoxX+($chartBoxWidth) , 
 ($chartBoxY+$chartBoxHeight)
 );

///vertical axis
//calculate chart's y axis scale unit
$yAxisUnits=$chartBoxHeight/$dataMax;

//draw the vertical (y) axis labels
for($i=0 ; $i<=$dataMax ; $i+=$dataStep){
 //y position
 $yAxisPos=$chartBoxY+($yAxisUnits*$i);
 //draw y axis line
 $pdf->Line(
  $chartBoxX-2 ,
  $yAxisPos ,
  $chartBoxX ,
  $yAxisPos
 );
 //set cell position for y axis labels
 $pdf->SetXY($chartBoxX-$chartLeftPadding , $yAxisPos-2);
 //$pdf->Cell($chartLeftPadding-4 , 5 , $dataMax-$i , 1);---------------
 $pdf->Cell($chartLeftPadding-4 , 5 , $dataMax-$i, 0 , 0 , 'R');
}

///horizontal axis
//set cells position
$pdf->SetXY($chartBoxX , $chartBoxY+$chartBoxHeight);

//cell's width
$xLabelWidth=$chartBoxWidth / count($data);

//$pdf->Cell($xLabelWidth , 5 , $itemName , 1 , 0 , 'C');-------------
//loop horizontal axis and draw the bar
$barXPos=0;
foreach($data as $itemName=>$item){
 //print the label
 //$pdf->Cell($xLabelWidth , 5 , $itemName , 1 , 0 , 'C');--------------
 $pdf->Cell($xLabelWidth , 5 , $itemName , 0 , 0 , 'C');
 
 ///drawing the bar
 //bar color
 $pdf->SetFillColor($item['color'][0],$item['color'][1],$item['color'][2]);
 //bar height
 $barHeight=$yAxisUnits*$item['value'];
 //bar x position
 $barX=($xLabelWidth/2)+($xLabelWidth*$barXPos);
 $barX=$barX-($barWidth/2);
 $barX=$barX+$chartBoxX;
 //bar y position
 $barY=$chartBoxHeight-$barHeight;
 $barY=$barY+$chartBoxY;
 //draw the bar
 $pdf->Rect($barX,$barY,$barWidth,$barHeight,'DF');
 //increase x position (next series)
 $barXPos++;
}

//axis labels
$pdf->SetFont('Arial','B',12);
$pdf->SetXY($chartX,$chartY);
$pdf->Cell(100,10,"Amount",0);
$pdf->SetXY(($chartWidth/2)-50+$chartX,$chartY+$chartHeight-($chartBottomPadding/2));
$pdf->Cell(100,5,"Series",0,0,'C');


$pdf->Output();
 

3. Custom Font


custom-font


 <?php
require('fpdf17/fpdf.php');

$pdf = new FPDF('P','mm','A4');

//add new freescript font
$pdf->AddFont('Freescript','','FREESCPT.php');

//add new jokerman font
$pdf->AddFont('Jokerman','','JOKERMAN.php');

//add alien league (regular)
$pdf->AddFont('Alien League','','alienleagueii.php');

//add alien league italic
$pdf->AddFont('Alien League','I','alienleagueiiital.php');

$pdf->AddPage();

//freescript font
$pdf->SetFont('Freescript','',36);
$pdf->Cell(190,20,'Freescript Font',0,1,'C');

//jokerman font
$pdf->SetFont('Jokerman','',36);
$pdf->Cell(190,20,'Jokerman Font',0,1,'C');

//alien league regular font
$pdf->SetFont('Alien League','',36);
$pdf->Cell(190,20,'Alien League Regular Font',0,1,'C');

//alien league italic font
$pdf->SetFont('Alien League','I',36);
$pdf->Cell(190,20,'Alien League Italic Font',0,1,'C');

$pdf->Output();
 

4. Gambar

gambar


 <?php
require('fpdf17/fpdf.php');

class PDF extends FPDF {
 function Header(){
  $this->SetFont('Arial','B',15);
  
  //dummy cell to put logo
  //$this->Cell(12,0,'',0,0);
  //is equivalent to:
  $this->Cell(12);
  
  //put logo
  $this->Image('logo-small.png',10,10,10);
  
  $this->Cell(100,10,'Student Scoring Report',0,1);
  
  //dummy cell to give line spacing
  //$this->Cell(0,5,'',0,1);
  //is equivalent to:
  $this->Ln(5);
  
 }
 function Footer(){
  
  //Go to 1.5 cm from bottom
  $this->SetY(-15);
    
  $this->SetFont('Arial','',8);
  
  //width = 0 means the cell is extended up to the right margin
  $this->Cell(0,10,'Page '.$this->PageNo()." / {pages}",0,0,'C');
 }
}


//A4 width : 219mm
//default margin : 10mm each side
//writable horizontal : 219-(10*2)=189mm

$pdf = new PDF('P','mm','A4'); //use new class

//define new alias for total page numbers
$pdf->AliasNbPages('{pages}');

$pdf->AddPage();

//Image( file name , x position , y position , width [optional] , height [optional] )
$pdf->Image('watermark.png',10,10,189);

//set font to arial, bold, 14pt
$pdf->SetFont('Arial','',12);

//Cell(width , height , text , border , end line , [align] )

//normal row height=5.

$pdf->Cell(20,10,'ID',1,0); //vertically merged cell, height=2x row height=2x5=10
$pdf->Cell(50,10,'Name',1,0); //vertically merged cell
$pdf->Cell(100,5,'Score',1,0); //normal height, but occupy 4 columns (horizontally merged)
$pdf->Cell(20,10,'Passing',1,0); //vertically merged cell
$pdf->Cell(0,5,'',0,1); //dummy line ending, height=5(normal row height) width=09 should be invisible 

//second line(row)
$pdf->Cell(70,5,'',0,0);   //dummy cell to align next cell, should be invisible
$pdf->Cell(25,5,'q1',1,0); 
$pdf->Cell(25,5,'q2',1,0); 
$pdf->Cell(25,5,'q3',1,0); 
$pdf->Cell(25,5,'q4',1,1); 

$pdf->SetFont('Arial','',11);
//data rows
for($i=1;$i<=20;$i++){
 $pdf->Cell(20,5,$i,'LR',0,'R');
 $pdf->Cell(50,5,'Student '.$i,'LR',0);
 $pdf->Cell(25,5,rand(70,100),'LR',0);
 $pdf->Cell(25,5,rand(70,100),'LR',0);
 $pdf->Cell(25,5,rand(70,100),'LR',0);
 $pdf->Cell(25,5,rand(70,100),'LR',0);
 $pdf->Cell(20,5,'Passed','LR',1);
}
 $pdf->Cell(190,5,'','T',1);




$pdf->AddPage();
$pdf->AddPage();
$pdf->AddPage();

$pdf->Output();
?>
 

5. Invoice

invoice


 <?php
require('fpdf17/fpdf.php');



//A4 width : 219mm
//default margin : 10mm each side
//writable horizontal : 219-(10*2)=189mm

$pdf = new FPDF('P','mm','A4');

$pdf->AddPage();

//Image( file name , x position , y position , width [optional] , height [optional] )
$pdf->Image('watermark.png',60,30,89);


//set font to arial, bold, 14pt
$pdf->SetFont('Arial','B',14);



//Cell(width , height , text , border , end line , [align] )

$pdf->Cell(130 ,5,'INVOICE TERBARU',0,0);
$pdf->Cell(59 ,5,'CHECK HARGA',0,1);//end of line

//set font to arial, regular, 12pt
$pdf->SetFont('Arial','',12);

$pdf->Cell(130 ,5,'[Street Address]',0,0);
$pdf->Cell(59 ,5,'',0,1);//end of line

$pdf->Cell(130 ,5,'[City, Country, ZIP]',0,0);
$pdf->Cell(25 ,5,'Tanggal',0,0);
$pdf->Cell(34 ,5,'[dd/mm/yyyy]',0,1);//end of line

$pdf->Cell(130 ,5,'Nomor Telp [+12345678]',0,0);
$pdf->Cell(25 ,5,'Nomor',0,0);
$pdf->Cell(34 ,5,'[1234567]',0,1);//end of line

$pdf->Cell(130 ,5,'Fax [+12345678]',0,0);
$pdf->Cell(25 ,5,'Costumer',0,0);
$pdf->Cell(34 ,5,'[1234567]',0,1);//end of line

//make a dummy empty cell as a vertical spacer
$pdf->Cell(189 ,10,'',0,1);//end of line

//billing address
$pdf->Cell(100 ,5,'Bill to',0,1);//end of line

//add dummy cell at beginning of each line for indentation
$pdf->Cell(10 ,5,'',0,0);
$pdf->Cell(90 ,5,'[Name]',0,1);

$pdf->Cell(10 ,5,'',0,0);
$pdf->Cell(90 ,5,'[Company Name]',0,1);

$pdf->Cell(10 ,5,'',0,0);
$pdf->Cell(90 ,5,'[Address]',0,1);

$pdf->Cell(10 ,5,'',0,0);
$pdf->Cell(90 ,5,'[Phone]',0,1);

//make a dummy empty cell as a vertical spacer
$pdf->Cell(189 ,10,'',0,1);//end of line

//invoice contents
$pdf->SetFont('Arial','B',12);

$pdf->Cell(130 ,5,'Description',1,0);
$pdf->Cell(25 ,5,'Taxable',1,0);
$pdf->Cell(34 ,5,'Amount',1,1);//end of line

$pdf->SetFont('Arial','',12);

//Numbers are right-aligned so we give 'R' after new line parameter

$pdf->Cell(130 ,5,'UltraCool Fridge',1,0);
$pdf->Cell(25 ,5,'-',1,0);
$pdf->Cell(34 ,5,'3,250',1,1,'R');//end of line

$pdf->Cell(130 ,5,'Supaclean Diswasher',1,0);
$pdf->Cell(25 ,5,'-',1,0);
$pdf->Cell(34 ,5,'1,200',1,1,'R');//end of line

$pdf->Cell(130 ,5,'Something Else',1,0);
$pdf->Cell(25 ,5,'-',1,0);
$pdf->Cell(34 ,5,'1,000',1,1,'R');//end of line

//summary
$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(25 ,5,'Subtotal',0,0);
$pdf->Cell(4 ,5,'$',1,0);
$pdf->Cell(30 ,5,'4,450',1,1,'R');//end of line

$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(25 ,5,'Taxable',0,0);
$pdf->Cell(4 ,5,'$',1,0);
$pdf->Cell(30 ,5,'0',1,1,'R');//end of line

$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(25 ,5,'Tax Rate',0,0);
$pdf->Cell(4 ,5,'$',1,0);
$pdf->Cell(30 ,5,'10%',1,1,'R');//end of line

$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(25 ,5,'Total Due',0,0);
$pdf->Cell(4 ,5,'$',1,0);
$pdf->Cell(30 ,5,'4,450',1,1,'R');//end of line


//make a dummy empty cell as a vertical spacer
$pdf->Cell(189 ,10,'',0,1);//end of line

//set font to arial, bold, 14pt
$pdf->SetFont('Arial','B',10);

$pdf->Cell(130 ,4,'BUKTI CHECK',0,0);
$pdf->Cell(59 ,5,'APARAT KEAMANAN',0,1);//end of line




//make a dummy empty cell as a vertical spacer
$pdf->Cell(289 ,20,'',0,1);//end of line

//set font to arial, bold, 14pt
$pdf->SetFont('Arial','B',10);

$pdf->Cell(130 ,4,'Approve',0,0);
$pdf->Cell(59 ,5,'Approve',0,1);//end of line




$pdf->Output();
?>
 

6. Text Wrapping

text-wrapping


 <?php
require('fpdf17/fpdf.php');

//A4 width : 219mm
//default margin : 10mm each side
//writable horizontal : 219-(10*2)=189mm

//dummy data
//data with medium text's length
$data1=array(
 array(
  "1",
  "Foo, overflowed text length",
  "This contains a long text. not too long but longer than cell's width.",
  "Something"
 ),
 array(
  "1",
  "Bar, normal text length",
  "This should not exceed the cell's width.",
  "Something else"
 ),
 array(
  "1",
  "Baz, overflowed text length",
  "This also contains a long text, not too long but longer than cell's width.",
  "Something else"
 )
);

//data which possibly contains long text
$data2=array(
 array(
  "1",
  "Foo, overflowed text length",
  "This is a quite long text for a cell. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
  "Something"
 ),
 array(
  "1",
  "Bar, normal length",
  "This is normal length for a cell.",
  "Something else"
 ),
 array(
  "1",
  "Baz, overflowed text length",
  "This is also a quite long text for a cell. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
  "Something else"
 ),
);

$pdf = new FPDF('P','mm','A4');

$pdf->AddPage();

$pdf->SetFont('Arial','',12);
//define standard font size
$fontSize=12;

//shrinking method
$pdf->Cell(150,5,"Font size shrinking method",0,1);
//define a temporary font size
$tempFontSize=$fontSize;
//loop the data
foreach($data1 as $item){
 $pdf->Cell(10,5,$item[0],1,0);
 $pdf->Cell(60,5,$item[1],1,0);
 
 //shrink font size until it fits the cell width
 $cellWidth=80;
 while($pdf->GetStringWidth($item[2]) > $cellWidth){ //loop until the string width is smaller than cell width
  $pdf->SetFontSize($tempFontSize -= 0.1);
 }
 $pdf->Cell($cellWidth,5,$item[2],1,0);
 //reset font size to standard
 $tempFontSize=$fontSize;
 $pdf->SetFontSize($fontSize);
 
 $pdf->Cell(40,5,$item[3],1,1);
}

$pdf->Ln(10);

//multicell method
$pdf->Cell(150,5,"MultiCell method",0,1);
foreach($data2 as $item){
 $cellWidth=80;//wrapped cell width
 $cellHeight=5;//normal one-line cell height
 
 //check whether the text is overflowing
 if($pdf->GetStringWidth($item[2]) < $cellWidth){
  //if not, then do nothing
  $line=1;
 }else{
  //if it is, then calculate the height needed for wrapped cell
  //by splitting the text to fit the cell width
  //then count how many lines are needed for the text to fit the cell
  
  $textLength=strlen($item[2]); //total text length
  $errMargin=10;  //cell width error margin, just in case
  $startChar=0;  //character start position for each line
  $maxChar=0;   //maximum character in a line, to be incremented later
  $textArray=array(); //to hold the strings for each line
  $tmpString="";  //to hold the string for a line (temporary)
  
  while($startChar < $textLength){ //loop until end of text
   //loop until maximum character reached
   while( 
   $pdf->GetStringWidth( $tmpString ) < ($cellWidth-$errMargin) &&
   ($startChar+$maxChar) < $textLength ) {
    $maxChar++;
    $tmpString=substr($item[2],$startChar,$maxChar);
   }
   //move startChar to next line
   $startChar=$startChar+$maxChar;
   //then add it into the array so we know how many line are needed
   array_push($textArray,$tmpString);
   //reset maxChar and tmpString
   $maxChar=0;
   $tmpString='';
   
  }
  //get number of line
  $line=count($textArray);
 }
 
 //write the cells
 $pdf->Cell(10,($line * $cellHeight),$item[0],1,0); //adapt height to number of lines
 $pdf->Cell(60,($line * $cellHeight),$item[1],1,0); //adapt height to number of lines
 
 //use MultiCell instead of Cell
 //but first, because MultiCell is always treated as line ending, we need to 
 //manually set the xy position for the next cell to be next to it.
 //remember the x and y position before writing the multicell
 $xPos=$pdf->GetX();
 $yPos=$pdf->GetY();
 $pdf->MultiCell($cellWidth,$cellHeight,$item[2],1);
 
 //return the position for next cell next to the multicell
 //and offset the x with multicell width
 $pdf->SetXY($xPos + $cellWidth , $yPos);
 
 $pdf->Cell(40,($line * $cellHeight),$item[3],1,1); //adapt height to number of lines
 
  
}

$pdf->Output();
?>
 

Sekian share kali ini semoga apa yang kami share bermanfaat bagi anda, kalian bisa mendownload langsung filenya dibawah ini.


Silahkan tinggalkan komentar dan mulai berdiskusi dengan kami.



Baca Juga