We shall create PDF using FPDF class from php. Using FPDF to create PDF from php offers some benefits like lots of functions come with it as built in. therefore you can easily modify them to create your PDF from php. Let us start.
$pdf= new FPDF();
By default FPDF creates A4 page with portrait orientation in millimeter measurement. If you need to change it, put the right corresponding letter for orientation, measurement unit and page size; like below where we are creating landscape orientation based A5 page size with centimeters as measuring unit:
$pdf= new FPDF (‘L’, ‘ cm’, ‘A5’);
Similarly you can add page number, decide the font and cell frame dimensions and send the output to the browser. Review following examples to learn how to modify them here:
$pdf->AddPage(5); // will create 5 pages pdf
$pdf->SetFont(‘Calibri’, ‘B’, 16); // will set the font to be Calibri, bold the fonts with 16 point size
$pdf->Cell(30,15, ‘How Are You’, 1) // will create 1 cell with 30x15 frame dimension with ‘How Are You’ text. You can control the text alignment by putting either L – Left aligned, R – Right aligned, C - Centered next in the command after the cell count. If you require multiple cells you can do it by adding the numbers to it like 0,1,2,3,4,5 etc.
$pdf->Output(); //will send the file to your browser.
This is the way one can make PDF using php. Now if you would like to further modify it, you can do it easily like:
Add an author using $pdf->SetAuthor(‘Faisal Farrukh’);
Add a title to your document $pdf->SetTitle(‘Rays Tutorial’);
Add color to your texts $pdf->SetTextColor(40,30,20);
Add 100% zoom able view to your text or document $pdf->SetDisplayMode(real, ‘default’);
Add border color to your document $pdf->SetDrawColor(40, 50, 60);
Add an image to your PDF $pdf->Image(‘filename.jpeg’, 5,6,7,8);
Add link to your PDF $pdf->Link(‘http://www.twitter.com/’);
If the link contains image or you want to insert image from a link make sure to define the size in link insertion.
Add a name to your document for output $pdf->Output(‘php Tutorial.pdf’, ‘I’);
Add texts to your document $pdf->Write(‘It is a backendbuddy production.’);
This way a complete php code to create PDF files will look similar to this when you are not creating any cell and the document contains texts and images only. It combines them all detailing every step:
//create an object
//get document properties
//fix font for the entire document
//get up a page
//insert an image - make it a link
//display the title with a border around it
//Set x and y position for the main text, reduce font size and write content
$pdf->Write(5'It is a backendbuddy production.');
//Output the document
For now you are done with creating a PDF using the php. It does not take a lot but few basic coding to do it all correct. However, our requirements are changing always and whatever we used in making a basic PDF is just not enough. So here we stuff some more tips to conclude the tutorial and to ensure better quality PDF creation.
Get margins for your document with $pdf->SetMargins(1”,1”,1”)
Insert page break to your text $pdf->SetAutoPageBreak(true,0)
Insert page numbers $pdf->AliasNGPages();