Hi! In today's post we will see how to convert html to pdf in php using dompdf library. DomPDF is basically a php library that offers a simple way to convert html content to pdf so that you can generate pdf files on fly with php. Frameworks like Laravel offers separate packages to create pdf files, but there is not so much luck when you work on core php. Hence we need external tools for the task and DomPDF is a good fit for it.
The library creates downloadable pdf file from the html webpage. It supports CSS2.1 and CSS3 and renders the html layout including the styles. It also handles @import, @media and @screen queries and can load external stylesheets while generating the pdf.
1. Convert HTML to PDF - Basic Usage:
Download the dompdf archive from here and extract the contents to your root folder. After that, create a sample php file, 'index.php' and include the autoloader file to load the required dompdf libraries and helper functions into your PHP project.
The following php script describes the basic usage of the dompdf class. It converts simple html content into pdf and output to the browser.
<?php // include autoloader require_once 'dompdf/autoload.inc.php'; // import dompdf class into global namespace use Dompdf\Dompdf; // instantiate dompdf class $dompdf = new Dompdf(); // pdf content $html = '<h1 style="color:blue;">Hello World!</h1><p>A PDF generated by DomPDF library.</p>'; // load html $dompdf->loadHtml($html); // set paper size and orientation $dompdf->setPaper('A4', 'landscape'); // render html as pdf $dompdf->render(); // output the pdf to browser $dompdf->stream(); ?>
The function setPaper()
is optional. If not provided, the default page settings will be used for rendering the pdf.
2. Generate PDF and Show File Preview:
The dompdf library offers an option to preview the pdf file before downloading. After generating the pdf, display it on the browser to get a preview using the stream()
method.
This method takes two parameters of which the first is the filename and the second is the optional parameter called Attachment
. The default value for this param is '1' forcing the browser to open the download pop-up window. Instead, you must set it as '0' for the browser preview.
<?php require_once 'dompdf/autoload.inc.php'; use Dompdf\Dompdf; $dompdf = new Dompdf(); $html = '<h1 style="color:blue;">Hello World!</h1><p>A PDF generated by DomPDF library.</p>'; $dompdf->loadHtml($html); $dompdf->setPaper('A4', 'landscape'); $dompdf->render(); // preview pdf $dompdf->stream('newfile',array('Attachment'=>0)); ?>
3. Save PDF to File:
To save the created pdf as a file on your local disk, you must output the rendered pdf to a variable and write it to a file using the file_put_contents()
method.
<?php require_once 'dompdf/autoload.inc.php'; use Dompdf\Dompdf; $dompdf = new Dompdf(); $html = '<h1 style="color:blue;">Hello World!</h1><p>A PDF generated by DomPDF library.</p>'; $dompdf->loadHtml($html); $dompdf->setPaper('A4', 'landscape'); $dompdf->render(); // write pdf to a file $pdf = $dompdf->output(); file_put_contents("newfile.pdf", $pdf); ?>
4. Generate PDF from HTML File:
You can also generate pdf from an html file. First you have to read the contents of the file into a variable using file_get_contents()
method and then load it with the loadHtml()
function of the library.
<?php require_once 'dompdf/autoload.inc.php'; use Dompdf\Dompdf; $dompdf = new Dompdf(); $html = file_get_contents('data.html'); $dompdf->loadHtml($html); $dompdf->setPaper('A4', 'landscape'); $dompdf->render(); $dompdf->stream('newfile', array('Attachment'=>0)); ?>
5. DomPDF Options:
The library offers a range of options for customization. You can set the options at run time like this,
<?php use Dompdf\Dompdf; $dompdf = new Dompdf(); $dompdf->set_option('defaultFont', 'Courier'); ?>
For the complete list of available options, see the page Dompdf Options on github.
Read Also:- User Login and Registration Module using PHP & MySQL
- Paytm Payment Gateway Integration in PHP Example
To generate pdf files in your php project, you can use the DomPDF library. The support for CSS gives you good control over the looks of the generated pdf. But keep in mind rendering large tables and files will take time. I hope this post is useful to you. Please share it on social media if you like it.
No comments:
Post a Comment