MY mENU


Tuesday, 25 September 2012

Dynamically Generate PDF Files In PHP


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.
01<form action="index.php"  method="post">
02 <fieldset>
03 <legend>Personal Information </legend>
04 <div>
05 <label for="name">Name :</label>
06 <input type="text" name="name" value="Hyder Bangash" size="25" />
07 </div>
08 <div>
09 <label for="e-mail">E-mail :</label>
10 <input type="text" name="e-mail" value="admin@youhack.me" size="25" />
11 </div>
12 <div>
13 <label for="e-mail">Address : </label>
14 <input type="text" name="Address" value="Henrietta Rd" size="25" />
15 </div>
16 <div>
17 <label for="e-mail">City  : </label>
18 <input type="text" name="City" value="Vacoas" size="25" />
19 </div>
20 <div>
21 <label for="e-mail">Country  : </label>
22 <input type="text" name="Country" value="Mauritius" size="25" />
23 </div>
24 </fieldset>
25 <fieldset>
26 <legend>Results </legend>
27 <div>
28 <input type="text" name="subjects[]" value="Maths" size="25" />
29 <input type="text" name="Marks[]" value="50" size="10" />
30 </div>
31 <div>
32 <input type="text" name="subjects[]" value="English" size="25" />
33 <input type="text" name="Marks[]" value="10" size="10" />
34 </div>
35 <div>
36 <input type="text" name="subjects[]" value="French" size="25" />
37 <input type="text" name="Marks[]" value="65" size="10" />
38 </div>
39 <div>
40 <input type="text" name="subjects[]" value="Science" size="25" />
41 <input type="text" name="Marks[]" value="80" size="10" />
42 </div>
43 <div></div>
44 <div>
45 <input type="hidden" value="submitted" />
46 <button type="submit">Submit</button>
47 </div>
48 </fieldset>
49 </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 .
01require ('fpdf/fpdf.php');//including the main class
02 
03class PDF_result extends FPDF
04{
05 
06/*The constructor takes 4 Arguments ,
07 
08 The portrait is set to PORTRAIT ,
09 
10The units measurement is set to POINTS ,
11 
12 The Format of the PDF is set to LETTER with a Margin of 40 Points* /
13 
14function __construct($orientation = 'P', $unit = 'pt', $format = 'Letter', $margin =40)
15 {
16 
17 $this->FPDF($orientation, $unit, $format);//Calling the parent Constructor with 3 Parameters
18 $this->SetTopMargin($margin);//Setting the Top Margin
19 $this->SetLeftMargin($margin);//Left Margin
20 $this->SetRightMargin($margin);//Right Margin
21 
22 $this->SetAutoPageBreak(true, $margin);
23/
24 }
25 
26/*The Method Header adds an Image at the top of the page
27The 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.*/
28 function Header()
29 {
30 $this->Image('youhack.png', 100, 15, 250);
31 }
32 
33/*The Footer Method add a text at the bottom of the Page */
34 function Footer()
35 {
36 //Position at 1.5 cm from bottom
37 $this->SetY(-15);
38 //Arial italic 8
39 $this->SetFont('Arial''I', 8);
40 //Page number
41 $this->Cell(0, 10, 'Generated at YouHack.me', 0, 0, 'C');
42 }
43 
44The Method Generate_Table takes 2 Array Parameters the subjects and Marks of Student .
45 function Generate_Table($subjects$marks)
46 {
47 $this->SetFont('Arial''B', 12);//Set the Font type to Arial,Bold with size 12 Pt
48 $this->SetTextColor(0);//Set the Text Color
49 $this->SetFillColor(94, 188, 225);//Fill the text with RGB Color
50 $this->SetLineWidth(1);//Set the Line Width to 1pt
51 $this->Cell(427, 25, "Subjects"'LTR', 0, 'C', true);
52$this->Cell(100, 25, "Marks"'LTR', 1, 'C', true);
53/*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 )
54*/
55 $this->SetFont('Arial''');Set the Font and Turn The Bold Off
56 $this->SetFillColor(238);
57 $this->SetLineWidth(0.2);//0.2 pts
58 $fill = false;
59 
60 for ($i = 0; $i count($subjects); $i++) {
61 $this->Cell(427, 20, $subjects[$i], 1, 0, 'L'$fill);
62 $this->Cell(100, 20, $marks[$i], 1, 1, 'R'$fill);
63 $fill = !$fill;//This variable is responsible for the alternative row colors
64 }
65 $this->SetX(367);
66 
67 }
68 
69}

Instantiate the class

01$pdf new PDF_result();
02$pdf->AddPage();
03$pdf->SetFont('Arial''B', 12);
04$pdf->SetY(100);
05$pdf->Cell(100, 13, "Student Details");
06$pdf->SetFont('Arial''');
07$pdf->Cell(250, 13, $_POST['name']);
08$pdf->SetFont('Arial''B');
09$pdf->Cell(50, 13, "Date:");
10$pdf->SetFont('Arial''');
11$pdf->Cell(100, 13, date('F j, Y'), 0, 1);
12$pdf->SetFont('Arial''I');
13$pdf->SetX(140);
14$pdf->Cell(200, 15, $_POST['e-mail'], 0, 2);
15$pdf->Cell(200, 15, $_POST['Address'] . ',' $_POST['City'], 0, 2);
16$pdf->Cell(200, 15, $_POST['Country'], 0, 2);
17$pdf->Ln(100);
18$pdf->Generate_Table($_POST['subjects'], $_POST['Marks']);
19$pdf->Ln(50);
20$message "Congratulation , you have successfully passed your exams .For More Information Contact us at : ";
21$pdf->MultiCell(0, 15, $message);
22$pdf->SetFont('Arial''U', 12);
23$pdf->SetTextColor(1, 162, 232);
24$pdf->Write(13, "admin@youhack.me""mailto:example@example.com");
25$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 :)

No comments:

Post a Comment