Post image for Dynamically Generate PDF Files In PHP

Dynamically Generate PDF Files In PHP

by Hyder on June 10, 2010

in PHP

Recently, I was working on my final year project, and at some point I had to develop a Multiple choice Question Module and give the user the option to export their results in PDF Format. After some googling, I stumbled upon a kick ass PDF Class to generate PDF Files Dynamically. I found it so amazing that I decided to put up a small tutorial how to generate a pdf file using PHP. I have created a simple form where a user enters his details and enters his Exam results and after submission the user can download his result in a nice PDF File.

Download || Demo

Below is the result of the PDF Generated:

PDF Sample

You will need to download FPDF Library which is a class utility to generate PDF Files  with pure PHP .

Creating the HTML Form

I have created a quite simple form that contains some fields to enter a person detail . Below is a screenshot of the form :

HTML FORM

The HTML Code :

Below is the HTML Code of the Form.  The CSS Styling and the jQuery code to handle the form submission has been skipped here to keep it simple. You can download the source code from the link above with the complete code. One thing I would like to point out in the HTML Code below is that the input name for the subjects and marks end with square bracket so that we can capture these values in an Array after submission.


<form action="index.php"  method="post">
 <fieldset>
 <legend>Personal Information </legend>
 <div>
 <label for="name">Name :</label>
 <input type="text" name="name" value="Hyder Bangash" size="25" />
 </div>
 <div>
 <label for="e-mail">E-mail :</label>
 <input type="text" name="e-mail" value="admin@youhack.me" size="25" />
 </div>
 <div>
 <label for="e-mail">Address : </label>
 <input type="text" name="Address" value="Henrietta Rd" size="25" />
 </div>
 <div>
 <label for="e-mail">City  : </label>
 <input type="text" name="City" value="Vacoas" size="25" />
 </div>
 <div>
 <label for="e-mail">Country  : </label>
 <input type="text" name="Country" value="Mauritius" size="25" />
 </div>
 </fieldset>
 <fieldset>
 <legend>Results </legend>
 <div>
 <input type="text" name="subjects[]" value="Maths" size="25" />
 <input type="text" name="Marks[]" value="50" size="10" />
 </div>
 <div>
 <input type="text" name="subjects[]" value="English" size="25" />
 <input type="text" name="Marks[]" value="10" size="10" />
 </div>
 <div>
 <input type="text" name="subjects[]" value="French" size="25" />
 <input type="text" name="Marks[]" value="65" size="10" />
 </div>
 <div>
 <input type="text" name="subjects[]" value="Science" size="25" />
 <input type="text" name="Marks[]" value="80" size="10" />
 </div>
 <div></div>
 <div>
 <input type="hidden" value="submitted" />
 <button type="submit">Submit</button>
 </div>
 </fieldset>
 </form>

Extending the FPDF Class

I have extended the FPDF Class and added 3 Methods and a constructor . The Methods witll add a Header , a Footer and Generate the Tabular Data .


require ('fpdf/fpdf.php');//including the main class

class PDF_result extends FPDF
{

/*The constructor takes 4 Arguments ,

 The portrait is set to PORTRAIT ,

The units measurement is set to POINTS ,

 The Format of the PDF is set to LETTER with a Margin of 40 Points* /

function __construct($orientation = 'P', $unit = 'pt', $format = 'Letter', $margin =40)
 {

 $this->FPDF($orientation, $unit, $format);//Calling the parent Constructor with 3 Parameters
 $this->SetTopMargin($margin);//Setting the Top Margin
 $this->SetLeftMargin($margin);//Left Margin
 $this->SetRightMargin($margin);//Right Margin

 $this->SetAutoPageBreak(true, $margin);
/
 }

/*The Method Header adds an Image at the top of the page
The logo is printed with the Image()  method by  specifying its upper-left corner and its width. The height is calculated automatically to respect the image  proportions.*/
 function Header()
 {
 $this->Image('youhack.png', 100, 15, 250);
 }

/*The Footer Method add a text at the bottom of the Page */
 function Footer()
 {
 //Position at 1.5 cm from bottom
 $this->SetY(-15);
 //Arial italic 8
 $this->SetFont('Arial', 'I', 8);
 //Page number
 $this->Cell(0, 10, 'Generated at YouHack.me', 0, 0, 'C');
 }

The Method Generate_Table takes 2 Array Parameters the subjects and Marks of Student .
 function Generate_Table($subjects, $marks)
 {
 $this->SetFont('Arial', 'B', 12);//Set the Font type to Arial,Bold with size 12 Pt
 $this->SetTextColor(0);//Set the Text Color
 $this->SetFillColor(94, 188, 225);//Fill the text with RGB Color
 $this->SetLineWidth(1);//Set the Line Width to 1pt
 $this->Cell(427, 25, "Subjects", 'LTR', 0, 'C', true);
$this->Cell(100, 25, "Marks", 'LTR', 1, 'C', true);
/*cell(Width of the cell ,Heigh of the cell, The Text to be written,1 = Border Parameter ||0 = Carriage Return,Position i.e C= Centre,Boolean for  background color )
*/
 $this->SetFont('Arial', '');Set the Font and Turn The Bold Off
 $this->SetFillColor(238);
 $this->SetLineWidth(0.2);//0.2 pts
 $fill = false;

 for ($i = 0; $i < count($subjects); $i++) {
 $this->Cell(427, 20, $subjects[$i], 1, 0, 'L', $fill);
 $this->Cell(100, 20, $marks[$i], 1, 1, 'R', $fill);
 $fill = !$fill;//This variable is responsible for the alternative row colors
 }
 $this->SetX(367);

 }

}

Instantiate the class

$pdf = new PDF_result();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 12);
$pdf->SetY(100);
$pdf->Cell(100, 13, "Student Details");
$pdf->SetFont('Arial', '');
$pdf->Cell(250, 13, $_POST['name']);
$pdf->SetFont('Arial', 'B');
$pdf->Cell(50, 13, "Date:");
$pdf->SetFont('Arial', '');
$pdf->Cell(100, 13, date('F j, Y'), 0, 1);
$pdf->SetFont('Arial', 'I');
$pdf->SetX(140);
$pdf->Cell(200, 15, $_POST['e-mail'], 0, 2);
$pdf->Cell(200, 15, $_POST['Address'] . ',' . $_POST['City'], 0, 2);
$pdf->Cell(200, 15, $_POST['Country'], 0, 2);
$pdf->Ln(100);
$pdf->Generate_Table($_POST['subjects'], $_POST['Marks']);
$pdf->Ln(50);
$message = "Congratulation , you have successfully passed your exams .For More Information Contact us at : ";
$pdf->MultiCell(0, 15, $message);
$pdf->SetFont('Arial', 'U', 12);
$pdf->SetTextColor(1, 162, 232);
$pdf->Write(13, "admin@youhack.me", "mailto:example@example.com");
$pdf->Output('result.pdf', 'F');

As you have seen it’s quite easy to use the class . You can check out the list of all built in methods from the documentation here. This is the only PDF Class Library i
have been working with so far . You might consider using TCPDF, mPDF and HTML 2 PDF as well to generate PDF Documents . IF you know any other PDF Class For PHP or have worked with share your experience in the comment section below :)

cell(Width of the cell ,x points high , text message ,border parameter =1,0 = carriage return,’center’ l ,c,r ,boolean background color )

Related Posts

  1. Username Availability Check in Registration Form using Jquery/PHP
  2. 8 awesome plug in to Generate Chart/Graph in Web Application

{ 3 comments… read them below or add one }

Codeforest June 11, 2010 at 7:19 am

I worked with TCPDF and it is much easier to generate PDFs as you just pass the HTML markup to the function and it generates. It is also using fPDF as the PDF library.

Nice tut.

Orson June 20, 2010 at 10:20 pm

Great jobm but download link not work!

Hyder June 20, 2010 at 11:11 pm

Thanks codeforest , i’ll check out TCPDF when i have some free time .:)
@Orson : The download link has been fixed .

Leave a Comment

Previous post:

Next post: