PHP Script to create PDF document using FPDF


PHP PDF generator 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

Installation and generating PDF document in PHP using FPDF class and showing Hello World


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

FPDF generating PDF file with Cell() & Output() with the options to manage border, file save in PHP

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.
MultiCell Drawing Line in PDF
Adding Image to PDF File
Title Thumbnail
#1Installation and generating PDF document in PHP using FPDF class and showing Hello World
https://youtu.be/gNH6NNyhJLk
#2FPDF generating PDF file with Cell() & Output() with the options to manage border, file save in PHP
https://youtu.be/CrBtpoKwzNo
#3Adding image to fpdf class PDF documents by using Image function with header and footer of all pages
https://youtu.be/1gUgZTz0k10
#4MultiCell in FPDF PDF generator to add multiline text with auto ward wrap and increase the height
https://youtu.be/Vum46ssYIus
#5Line() method to draw lines using coordinates and by measuring page width and height
https://youtu.be/UjQL73TwuoE
#6Drawing patterns of lines on PDF document by FPDF class in PHP using color and width of the line.
https://youtu.be/QRBLe29Rxgo
#7Adding table to PDF document by using Cell with equal width by using alignment & position after cell
https://youtu.be/4fCrlgesY5w
#8Displaying Data from MySQL table in PDF document by using Cell with alternate background color
https://youtu.be/EYYZFRRdR6A
#9Generating PDF document using record from MySQL database with unique id in PHP using FPDF class
https://youtu.be/ZLdzDxbnh0M


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    20-11-2020

    Excellent post. Nice work keep it up. Thanks for sharing the knowledge.

    26-11-2021

    Excellent post. Nice work keep it up. Thanks for sharing the knowledge.

    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer