PHP Script to create PDF document using FPDF

Download FPDF from here.
https://www.fpdf.org
Keep the fpdf.php file in your working directory, keep the font directory in the same place.
Directory with fpdf class and font file

Run this code , you are ready with your first pdf file created from FPDF class.
<?Php
require('fpdf.php');
$pdf = new FPDF(); 
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(80,10,'Hello World!');
$pdf->Output('my_file.pdf','I'); // Send to browser and display
?>

Check the output to display the string in PDF

Above code will display the string, we can manage this output by adding border , alignment, colour , link etc..
<?Php
require('fpdf.php');
$pdf = new FPDF(); 
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(80,10,'Hello World!',1,0,R,false,'https://www.plus2net.com');
$pdf->Output('my_file.pdf','I'); // send to browser and display
?>

String with border , alignment, background fill in PDF

In the above code we have used cell to add our text to the pdf file with border, alignment , background fill and a link. Here is the details of Cell
Cell for creating pdf document
Try to change the script by adding backgroud color like this.
$pdf->SetFillColor(1,255,255);
$pdf->Cell(80,10,'Hello World!',1,0,R,true,'https://www.plus2net.com');
$pdf->Output('my_file.pdf','D'); 

Output with background color is here

Note : Cell function background fill option must be set to true

Adding font colour by SetTextColor()

<?Php
require('fpdf.php');
$pdf = new FPDF(); 
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->SetFillColor(1,99,255); // input R ,G , B 
$pdf->SetTextColor(255,254,254);// input R , G , B 
$pdf->Cell(80,10,'Hello World!',1,0,C,true,'https://www.plus2net.com');
$pdf->Output('my_file.pdf','I'); // Send to browser and display
?>

Output with backgrond and Font color is here

With border colour by SetDrawColor() and border size by SetLineWidth()

<?Php
require('fpdf.php');
$pdf = new FPDF(); 
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->SetFillColor(1,99,255); // input R ,G , B 
$pdf->SetTextColor(255,254,254);// input R , G , B 
$pdf->SetDrawColor(255,1,1);// input R , G , B 
$pdf->SetLineWidth(1);
$pdf->Cell(80,10,'Hello World!',1,0,C,true,'https://www.plus2net.com');
$pdf->Output('my_file.pdf','I'); // Send to browser and display
?>

Output with border width and colour is here

We can add border in any side or in all sides or we can remove the borders of the cell by assigning different values.

Example : To add border to Left , Right and top we can give the option as LRT, similarly we can assign border to bottom only by giving the option as B.

Check the border option value with different sides to add border.

Saving the PDF file

We used $pdf->Output('my_file','D'); to get output directly. This line will force download to user machine as pdf file my_file.pdf. We can change this output to save the file in local ( server ) side machine.
$pdf->Output('my_file.pdf','I'); // Send to browser and display
$pdf->Output('my_file.pdf','D'); //  Force file download with name given ( name can be changed )
$pdf->Output('my_file.pdf','F'); //  Saved in local computer with the file name given
$pdf->Output('my_file.pdf','S'); //  Return the document as a string.  

Cell and MultiCell

When we are not aware about the length of the string we are going to use inside Cell, then the string may cross the cell boarder and goes out. In MultiCell the text will wrap and will add line breaks to remain within the boundaries of the MulitCell.
Visitors Rating
Your Rating




Google+

Post Comment This is for short comments only. Use the forum for more discussions.




HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2018 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer