In this tutorial, we will produce PDF document with HTML information utilizing FPDF library. In this library, there are elements to include a large portion of PDF pages with required size like A4, A3, letter pad and much more, set content display font style, border, images and etc.
Using FPDF, we can generate any content as PDF format and use anywhere. It is used to generate Bills, Tutorial content, E-Books, Vouchers etc. After generating PDF file we can download a file, mail it to anyone, or save into server with path information.
In FPDF library also provide secured feature for any confidential data in it. It will enable with password PDF with a particular user known key information like account no, Combination of profile tow field information’s, DOB with name etc. We can able to set any content as a PDF password key.
Step 1: Download the FPDF library and include the fpdf.php file with our page.
1 |
require('./fpdf/fpdf.php'); |
Step 2: Create the PDF object from FPDF() class.
1 |
$pdf = new FPDF_protection(); |
Step 3: Add a new page with required page sizes like A4, A3 or Custom size with the help of AddPage() method.
1 2 3 |
$pdf->AddPage(); // For A4 Page size $pdf = new FPDF('L','mm',array(205,100)); // For custom page size |
Step 4: Add new line using Ln() method.
1 |
$pdf->Ln(); |
Step 5:Â Set the font what you like to display the content font style using this command.
1 |
$pdf->SetFont('font_name','font_style',font_size); |
Here font_style as Bold, Underline, Italic and font_size like integers,
Step 6: Now we can add content to a PDF file like image, text line and paragraphs using Image(), Cell() and MultiCell() methods.
1 2 3 4 5 |
$pdf->Image('image_name', X1, Y1, X2, Y2); $pdf->Cell(50,5,'single_line_text'); $pdf->MultiCell(190,5,'paragraph_text',0,'L',0); |
Step 7: Then create a PDF file with the help of Output() method.
1 |
$pdf->Output(); |
Step 8: If you want to provide secured PDF means just add the password for generating PDF. It will do with the help of FPDF Protection Class fpdf_protection.php. Just include this file in our page instead of including fpdf.php file.
1 |
require('./fpdf/fpdf_protection.php'); |
Step 9: setProtection() method use to set the password. Here we can set admin and user password with a different kind of password.
1 |
$pdf->setProtection(array(),'password_key'); // to set password for generating PDF file |
Step 10:Â Finally the complete code for generating PDF usign FPDF library.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php require('./fpdf/fpdf.php'); require('./fpdf/fpdf_protection.php'); $pdf = new FPDF_protection(); $pdf->AddPage(); $pdf->Ln(); $pdf->Image('./LI_Watermak.jpg', 70, 20, 50, 20); $pdf->setxy(60,50); $pdf->SetFont('Arial','BUI',15); $pdf->Cell(50,5,'PHP PDF Generation using FPDF'); $pdf->setxy(60,60); $pdf->SetFont('Arial','',12); $pdf->Ln(); $pdf->MultiCell(190,5,'In this tutorial, we will produce PDF document with html information utilizing FPDF library. In this library, there are elements to include a large portion of PDF pages with required size like A4, A3, letter pad and much more, set content display font style, border, images and etc.',0,'L',0); $pdf->Ln(); $pdf->MultiCell(190,5,'Using FPDF, we can generate the any content as PDF format and use anywhere. It is use to generate Bills, Tutorial content, E-Books, Vouchers etc. After generating PDF file we can download a file, mail it to anyone, or save into server with path information.',0,'L',0); $pdf->Ln(); $pdf->MultiCell(190,5,'In FPDF library also provide secured feature for any confidential data in it. It will enable with password PDF with particular user known key information like account no, Combination of profile tow field information\'s, DOB with name etc. We can able to set any content as a PDF password key.',0,'L',0); $pdf->setProtection(array(),'learninfinity'); // to set password for generating PDF file $pdf->Output(); ?> |