Post image for Dynamically Generate PDF Files In PHP

Dynamically Generate PDF Files In PHP

by Hyder

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 :


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">
 <legend>Personal Information </legend>
 <label for="name">Name :</label>
 <input type="text" name="name" value="Hyder Bangash" size="25" />
 <label for="e-mail">E-mail :</label>
 <input type="text" name="e-mail" value="" size="25" />
 <label for="e-mail">Address : </label>
 <input type="text" name="Address" value="Henrietta Rd" size="25" />
 <label for="e-mail">City  : </label>
 <input type="text" name="City" value="Vacoas" size="25" />
 <label for="e-mail">Country  : </label>
 <input type="text" name="Country" value="Mauritius" size="25" />
 <legend>Results </legend>
 <input type="text" name="subjects[]" value="Maths" size="25" />
 <input type="text" name="Marks[]" value="50" size="10" />
 <input type="text" name="subjects[]" value="English" size="25" />
 <input type="text" name="Marks[]" value="10" size="10" />
 <input type="text" name="subjects[]" value="French" size="25" />
 <input type="text" name="Marks[]" value="65" size="10" />
 <input type="text" name="subjects[]" value="Science" size="25" />
 <input type="text" name="Marks[]" value="80" size="10" />
 <input type="hidden" value="submitted" />
 <button type="submit">Submit</button>

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
 //Arial italic 8
 $this->SetFont('Arial', 'I', 8);
 //Page number
 $this->Cell(0, 10, 'Generated at', 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->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



Instantiate the class

$pdf = new PDF_result();
$pdf->SetFont('Arial', 'B', 12);
$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->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->Generate_Table($_POST['subjects'], $_POST['Marks']);
$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, "", "");
$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. Create fancy contact form with CSS 3 and jQuery
  4. An Alternative to Pagination : Facebook and Twitter Style
After Post

{ 22 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 .


Viku October 7, 2010 at 3:22 am

Hi Hyder,

Great Work Genius. I have similar project.I am doing an assignment where I am creating an application with PHP and Mysql. Its a Human resource management program. Now with my assignment I have to do a task which requires me to generate report from the database say for example whenever Manager assign new role to one of the available employee for new project or more employees for new projects and want to produce report for that. here the issue is I dont have clue from where to start and how should I design that report.
Any help will be highly appreciated.



Sharmin November 30, 2010 at 2:35 pm

I can’t make fpdf work in my code! How to you install or make FPDF work? Where do you need to paste the extracted folder?



gowtham January 13, 2011 at 12:15 pm

The article and given examples are working fine.all the best hyder.. it helped me a lot really

thanks and keep working


Mike August 18, 2011 at 4:21 pm

This looks awesome! Do you think this will work on a site? Thanks.


Hyder August 18, 2011 at 6:41 pm

if you can upload php files at , then you can . if not , you can still find a free hosting provider to host the script or you can just host the script on xampp and play with it locally :)


Mike August 18, 2011 at 10:57 pm

Thanks. I personally don’t have access to the server that the site is hosted on. I found a WP plugin that allows you to execute PHP code within the context of a post or page. I’m wondering if that would work with this?

I’m a bit of noobie at this. I have been looking to find a WP plugin that does this. So far no luck.

If I’m able to figure it out I would like to make an html form where the user can choose check boxes of what content they would like to include in the document, they would be able to upload their own logo and then it would enter in their brand name throughout the document. Sort of like a free online legal document generator.

And then when it is done they can either download it or it will email them the document. I know this has to be possible some how but I just can’t seem to figure out how it would all work out.

Thanks again, for the tips.


tak chin September 28, 2011 at 6:14 am

Greetings Hyder;

Thank You for sharing your code.

I got it to work on a local web server:
Server2go -
Apache/2.2.15 (Win32)
SQLite 2
MySQL 5.1.46


QUESTION: How did you get the HTML Form to automatically display? (Can’t figure that out).

Normally when I launch the Server2go web service, the Server2go splash page appears first.

Thanks again.




Hyder September 28, 2011 at 6:20 am

The HTML form code is found in the index.php file .it appears first because it’s name is ‘index.php’ .Apache looks for this file name ,if it’s found it loads it first.If not found it will display the whole content of the folder. I’m not sure if i answered your question ?


tak chin September 29, 2011 at 3:56 am

Yes it does (answer my question).

Thanks for the prompt response.



Hyder September 29, 2011 at 6:04 am

Great :) Please also note that if you have index.html and index.php in a folder ,apache gives priority to the index.html.


Koos January 19, 2012 at 8:26 am


Would you possibly know how I could customize this so that one could upload an image in the form and that would appear in the final pdf, and so that the generated pdf could be sent to an email address specified in the form?



asela January 31, 2012 at 3:27 am

i want to create this form using my native language , can any help me to do that ?


Helen Neely February 1, 2012 at 10:02 am

Thanks for this awesome example, it really helped with one of the projects I’m working on. Submitting a HTML form now generates Pdf that the admin can print and send out in a post.


Ayo David April 13, 2012 at 11:34 am

Thanks guy! this is really helpful


Hiren Adhiya July 19, 2012 at 10:50 am

Thanks guy! this is really helpful


pathum October 11, 2012 at 3:37 am

Great this is really helpful thanks….


mohan November 1, 2012 at 6:05 pm

How if many user access same time… because the link pointing to result.pdf if the second user press submit button simultaneously there will be error..can explain to me im not understand.



Cody January 2, 2014 at 2:42 am

Dear Hyder,

Thanks for the script. I have the script working fine except it will not post the .pdf file. I’m wondering if you changed your CHMOD settings that generates the results.pdf? Please help.

Appreciate Everything


Manali Killedar January 9, 2014 at 10:28 am

Thank you for uploading the project files. It has been a great help for a project of mine.
Thank you again.



Leave a Comment

Previous post:

Next post: