Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Convert HTML to PDF Using mPDF library

Tags: mpdf html avoid

Convert HTML to PDF Using mPDF library

mPDF is a PHP class which generates PDF files from UTF-8 encoded Html. It is based on FPDF and HTML2 FPDF with a number of enhancements. mPDF is used to convert into PDF file from HTML, PHP pages.

Some of the easy way to convert your web page into PDF file follows:

Step1: Initially download the mPDF PHP library packages (ZIP Files). Here download the library files in this link (with your preferred version)

http://www.mpdf1.com/mpdf/index.php

Step2: Open your downloaded mPDF file. Copy the mPDF file into your project folder. Check whether the file is downloaded correctly with pdf and CSS file in the mPDF folder.

Step3: Open your project file. In your current project file copy and paste this mPDF config code.

Step4: Configure your project. First call the mPDF file and get the style class. Then post value which information acquired from your web page for downloading.

In Order for the conversion of PDF to take place, both the CSS ie CSS in mPDF and CSS in HTML should have the same CSS file but in a different location.

Step5: To get the same style of your web page as PDF use the same style of CSS for both PDF and web page

$css = file_get_contents(‘filename.css’);

And assign this CSS file in your PHP project and also in which you are working on.

For the reference check below image to config.

Step6: Follow this step carefully for config the mPDF in your project as downloading PDF file.

            Config with Script file for Print and download the PDF.

Step7: Download as PDF for Invoice or web page. Here you can find the final output of converted PDF file

Step8: Incase if you are struggling with alignment issues or not generating the second page like a blank page of your webpage you can refer this step.

How to control page breaks in CSS while converting into PDF

Use this keyword at appropriate web page, this will result in more accurate alignment that fits into PDF

page-break-after: auto | always | avoid | left | right

page-break-before: auto | always | avoid | left | right

page-break-inside: auto | avoid

 Page break examples using in code:

 /* avoid splitting element across pages */

.no-break {

page-break-inside: Avoid;

}

/* force h2 title to display on new page */

h2 {

page-break-before: always;

}

Title

… content …

… content inside this element will not break from page to page …

New page title

           



This post first appeared on Kinds Of Web Design And Development Services, please read the originial post: here

Share the post

Convert HTML to PDF Using mPDF library

×

Subscribe to Kinds Of Web Design And Development Services

Get updates delivered right to your inbox!

Thank you for your subscription

×