Table of Contents
How to convert HTML to PDF?
DOMPDF is the open-source PHP library, which is used to convert html to PDF files, it has many features along with few limitations.
Almost many web applications need to have this feature, we developers always been requested to add generate report feature, like wise I found DOMPDF good in certain ways basically it’s fulfilling my applications requirements and working well so far.
I decided to write a simple tutorial to guide on generating PDF report from HTML content.
Before starting to use DOMPDF please have a look at limitations along with the features it’s provides, you can just check with your needs and if sounds good then go ahead and use the plugin.
DOMPDF Limitations:
- Large file or HTML content with large table can take longer than usual execution time. (If you have big content coming on your way, I suggest you should find another alternative or may be other paid plugins)
DOMPDF Features:
- Can work with CSS 2.1 along with few basic CSS3 properties
- HTML 4.0
- External style sheet – we can embed external CSS file within HTML content
- Complex Tables – It works with Nested tables as long as we have limited data.
- Inline PHP support
- Basic SVG support
Let’s get started:
DOMPDF requires following configuration on your server, make sure you have those extension and libraries are ready to use with PHP
- PHP > 5.3
- DOM extension
- GD extension
- MB String extension
- php-font-lib
- php-svg-lib
Step 1: Installation:
Install DOMPDF from GitHub:
git clone https://github.com/dompdf/dompdf.git
Install php-font-lib: (to dompdf directory)
git clone https://github.com/PhenX/php-font-lib.git lib/php-font-lib
Install php-svg-lib (to dompdf directory)
git clone https://github.com/PhenX/php-svg-lib.git php-svg-lib
So now we are ready to use the plugin.
Step 2: Generate Sample PDF
require_once __DIR__ . '/dompdf/autoload.inc.php'; use Dompdf\Dompdf; $dompdf = new Dompdf(); $html = <<<'ENDHTML' <html> <body> <h1>Generated By DOMPDF</h1> <p style="color: green;">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus est labore nostrum quis? Accusamus alias asperiores assumenda, dicta dignissimos facere incidunt minus nihil, placeat provident quas qui, quidem vel voluptatum!</p> <p style="color: red;">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus est labore nostrum quis? Accusamus alias asperiores assumenda, dicta dignissimos facere incidunt minus nihil, placeat provident quas qui, quidem vel voluptatum!</p> <p style="color: blue;">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus est labore nostrum quis? Accusamus alias asperiores assumenda, dicta dignissimos facere incidunt minus nihil, placeat provident quas qui, quidem vel voluptatum!</p> </body> </html> ENDHTML; $dompdf->loadHtml($html); $dompdf->setPaper('A4', 'landscape'); $dompdf->render(); $dompdf->stream('mypdf.pdf');
A Quick description on execution:
In the above code first, we have to include the `autoload.inc.php` file, which is having dompdf settings, after including autoload.inc.php file we can simple reference DOMPDF namespace and then can initiate the object to use.
Loading HTML: `LoadHtml()` is used to load html content, we can also read content from external html file using `load_html_file()` function.
Saving Generated PDF file into the Specific Directory
If you need to store pdf file on server into a specific directory, you can easily use following code:
require_once __DIR__ . '/dompdf/autoload.inc.php'; use Dompdf\Dompdf; $dompdf = new Dompdf(); $html = <<<'ENDHTML' <html> <body> <h1>Generated By DOMPDF</h1> <p style="color: green;">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus est labore nostrum quis? Accusamus alias asperiores assumenda, dicta dignissimos facere incidunt minus nihil, placeat provident quas qui, quidem vel voluptatum!</p> <p style="color: red;">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus est labore nostrum quis? Accusamus alias asperiores assumenda, dicta dignissimos facere incidunt minus nihil, placeat provident quas qui, quidem vel voluptatum!</p> <p style="color: blue;">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus est labore nostrum quis? Accusamus alias asperiores assumenda, dicta dignissimos facere incidunt minus nihil, placeat provident quas qui, quidem vel voluptatum!</p> </body> </html> ENDHTML; $dompdf->loadHtml($html); $dompdf->setPaper('A4', 'landscape'); $dompdf->render(); $output = $dompdf->output(); file_put_contents("pdffiles/file.pdf", $output);
If you get any issues using tutorial, you can easily comment in below section.
And how do you load external css file?
Just like we do on web pages.
this pdf in supported tamil font?
It should but will check and get back to you!