Create PDF with PHP using FPDF
If you want to create PDF file with PHP, you do not need to dig a lot. There are many good PDF libraries that allow you to easily create PDF file. Not just simple PDF files, but you can also create the PDF files with proper formatting.
If you are looking for a PDF library to handle PDF files with PHP, I will recommend to use FPDF library. FPDF is a php class file which makes the pdf generation easier than before. It has all the built in functions for creating pdf. It helps in creating documents at run time with PHP.
Download this call file from the offical website http://www.fpdf.org/
FPDF has many advantages which are not available in other PDF libraries. This class also supports many other languages other than english which makes it different from other libraries. You can add a header, footer, page break, image, colors, and tables like thing in the PDF pages. You can also customize the font and text sizes.
See the sample code below
<?php require('fpdf.php'); $pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('Arial','B',16); $pdf->Cell(40,10,'Hello World!'); $pdf->Output(); ?>
In this code I have included the class file downloaded from the website. Then create a new object of FPDF. default pages for the pdf are in A4 portrait and all the units are in milimeter.
This PHP code will generate a PDF file that will have a single line “Hello World!”
$pdf->SetFont(‘Arial’,’B’,16); // this line sets the font, style, size to the text. here B stands for bold.
Use $pdf->Output(); at the end of the file to generate PDF file.
You can find more tutorials about FPDF on the official website.
Tutorials are very easy to understand. So, I don’t think there is any need to explain the code. If you still find any of those codes hard to understand, you can always comment below. I hope you find this article useful.
Follow us on Facebook for all the latest updates.