Dear Reader, here we are going to give you the way to “Generate PDF File using PHP”.
Portable Document Format (PDF) is a file format used to represent documents in a manner independent of application software, hardware, and operating system. Almost everyone has a pdf reader software on their PC or mobiles so sharing documents becomes very easy with pdf files. In this tutorial, we will take you to the tour of how to generate a pdf file with php.
Where do I use this script?
Take a situation where you have created a school/college/employee (any other) Management System and there comes a point where you have to generate reports in pdf formats. Or most commonly you have an e-commerce site and you need to generate invoices in pdf format. The situation may be different but the requirement is the same. So this script can prove very handy at that time.
Let’s generate a simple pdf file
The library that I will be using is mpdf which generates pdf files from HTML. You can visit the mpdf official site here for more info.
Follow these three steps to generate a quick pdf file.
- Download the latest zip file from the mpdf official site( click here ).
- Extract the zip file to your server and rename it to mpdf (you can give any other name)
- Attach the class file to your file and write down the script given below.
Features provide by mpdf library 5.7
- Accept UTF-8 encoded.
- Right-to-left languages are supported.
- Bookmarks and Meta tag information are supported in all character sets.
- A single stylesheet can be used for all pages.
- Table cell padding and borders are supported.
- Text-indent for 1st line of the paragraph.
- List indenting can be defined.
- Custom tags added – PAGEBREAK, COLUMN BREAK, INDEXENTRY.
- Table of contents can be generated automatically.
- Watermark can be done.
- Intelligent word-wrapping.
- And many more
Commonly used methods of mpdf:
Add a stylesheet to the document.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php include("./mpdf/mpdf.php"); $mpdf=new mPDF(); $stylesheet = file_get_contents('pdf_style.css'); // Read the css file $mpdf->WriteHTML($stylesheet,1); // The parameter 1 tells that this is css/style only and no body/html/text $mpdf->WriteHTML($html); $mpdf->Output(); exit; ?> |
1 2 3 4 5 |
<?php $mpdf=new mPDF('utf-8', 'A4-L'); // Define a Landscape page size/format by name $mpdf=new mPDF('utf-8', 'A5'); // Define a A5 page $mpdf=new mPDF('utf-8', array(250,250)); // Define a custom size page (size in mm) ?> |
1 2 3 4 5 6 7 8 9 |
<?php $margin_left = 20; // sizes are defines in millimetres $margin_right = 20; $margin_top = 10; $margin_bottom = 10; $header = 5; $footer = 5; $mpdf=new mPDF('','', 0, '', $margin_left, $margin_right, $margin_top, $margin_bottom, $header, $footer); ?> |
1 2 3 4 5 6 7 8 9 10 |
<?php $margin_left = 20; // sizes are defines in millimetres $margin_right = 20; $margin_top = 10; $margin_bottom = 10; $header = 5; $footer = 5; $orientation = "L"; // can be P (Portrait) or L (Landscape) $mpdf=new mPDF('','', 0, '', $margin_left, $margin_right, $margin_top, $margin_bottom, $header, $footer, $orientation); ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $mode = "utf-8"; // sizes are defines in millimetres $format =" A10"; $default_font_size = "12"; // size in pt $default_font = "Courier"; $margin_left = 20; $margin_right = 20; $margin_top = 10; $margin_bottom = 10; $header = 5; $footer = 5; $orientation = "L"; // can be P (Portrait) or L (Landscape) $mpdf=new mPDF($mode,$format, $default_font_size, $default_font, $margin_left, $margin_right, $margin_top, $margin_bottom, $header, $footer, $orientation); ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $mpdf=new mPDF(); $mpdf->SetWatermarkText('thesoftwareguy'); $mpdf->showWatermarkText = true; // add a watermark image // first parameter is the source path of image // second is the transparency value // third is the size of the watermark (can be array) $mpdf->SetWatermarkImage('tiger.wmf', 0.15, 'F'); ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?php // set the header content left center and right alignment // values are separated by a pipe sign $mpdf->SetHeader('{DATE j-m-Y}|{PAGENO}/{nb}|My document'); // different layout of odd and even pages $mpdf->SetHeader(array( 'L' => array( 'content' => 'Text to go on the left', 'font-family' => 'sans-serif', 'font-style' => 'B', /* blank, B, I, or BI */ 'font-size' => '10', /* in pts */ ), 'C' => array( 'content' => '- {PAGENO} -', 'font-family' => 'serif', 'font-style' => 'BI', 'font-size' => '18', /* gives default */ ), 'R' => array( 'content' => 'Printed @ {DATE j-m-Y H:m}', 'font-family' => 'monospace', 'font-style' => '', 'font-size' => '10', ), 'line' => 1, /* 1 to include line below header/above footer */ ), 'E' /* defines header for Even Pages */ ); // Similarly you can set the footer by using SetFooter() method $mpdf->SetFooter('{PAGENO}'); ?> |
2 Comments:
particle October 19, 2017
Hey! I know this is sort of off-topic however I needed to
ask. Does running a well-established website like yours take a large amount of work?
I’m brand new to blogging but I do write in my diary everyday.
I’d like to start a blog so I will be able to share my
experience and views online. Please let me know if you
have any suggestions or tips for brand new aspiring bloggers.
Thankyou!
http://theelderscrolls5skyrimevolution225.ru
Webpreparations Team October 21, 2017
Hi, first of all, wanna say thanks a lot for appreciating our website’s contents. We love to share our knowledge with readers like you. In the same way you can also share your views with needy people. And now for your queries, want to say that, Off course for doing any thing bigger, needs work hard, but if we are passionate about our work, then we will reach our goal definitely. Just move on n on and you will get the result for sure.