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" > |
03 | < legend >Personal Information </ legend > |
05 | < label for = "name" >Name :</ label > |
06 | < input type = "text" name = "name" value = "Hyder Bangash" size = "25" /> |
09 | < label for = "e-mail" >E-mail :</ label > |
10 | < input type = "text" name = "e-mail" value = "admin@youhack.me" size = "25" /> |
13 | < label for = "e-mail" >Address : </ label > |
14 | < input type = "text" name = "Address" value = "Henrietta Rd" size = "25" /> |
17 | < label for = "e-mail" >City : </ label > |
18 | < input type = "text" name = "City" value = "Vacoas" size = "25" /> |
21 | < label for = "e-mail" >Country : </ label > |
22 | < input type = "text" name = "Country" value = "Mauritius" size = "25" /> |
26 | < legend >Results </ legend > |
28 | < input type = "text" name = "subjects[]" value = "Maths" size = "25" /> |
29 | < input type = "text" name = "Marks[]" value = "50" size = "10" /> |
32 | < input type = "text" name = "subjects[]" value = "English" size = "25" /> |
33 | < input type = "text" name = "Marks[]" value = "10" size = "10" /> |
36 | < input type = "text" name = "subjects[]" value = "French" size = "25" /> |
37 | < input type = "text" name = "Marks[]" value = "65" size = "10" /> |
40 | < input type = "text" name = "subjects[]" value = "Science" size = "25" /> |
41 | < input type = "text" name = "Marks[]" value = "80" size = "10" /> |
45 | < input type = "hidden" value = "submitted" /> |
46 | < 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 .
01 | require ( 'fpdf/fpdf.php' ); |
03 | class PDF_result extends FPDF |
30 | $this ->Image( 'youhack.png' , 100, 15, 250); |
39 | $this ->SetFont( 'Arial' , 'I' , 8); |
41 | $this ->Cell(0, 10, 'Generated at YouHack.me' , 0, 0, 'C' ); |
44 | The Method Generate_Table takes 2 Array Parameters the subjects and Marks of Student . |
45 | function Generate_Table( $subjects , $marks ) |
47 | $this ->SetFont( 'Arial' , 'B' , 12); |
48 | $this ->SetTextColor(0); |
49 | $this ->SetFillColor(94, 188, 225); |
50 | $this ->SetLineWidth(1); |
51 | $this ->Cell(427, 25, "Subjects" , 'LTR' , 0, 'C' , true); |
52 | $this ->Cell(100, 25, "Marks" , 'LTR' , 1, 'C' , true); |
55 | $this ->SetFont( 'Arial' , '' );Set the Font and Turn The Bold Off |
56 | $this ->SetFillColor(238); |
57 | $this ->SetLineWidth(0.2); |
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 ); |
Instantiate the class
01 | $pdf = new PDF_result(); |
03 | $pdf ->SetFont( 'Arial' , 'B' , 12); |
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' ); |
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); |
18 | $pdf ->Generate_Table( $_POST [ 'subjects' ], $_POST [ 'Marks' ]); |
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