Dompdf is an HTML to PDF converter. It is a (mostly) CSS 2.1 compliant HTML layout and rendering engine written in PHP. It can download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements. It also supports most presentational HTML attributes.
I have also used TCPDF but its much more difficult to use and it has lots of problems when it comes to rendering or using custom fonts. SO in the cases i need to convert php or html to pdf, i prefer dompdf.
HOW TO START
First off, download dompdf from here: https://github.com/dompdf/dompdf/releases
All you have to do at this point is to place it inside your project. If you have a WordPress installation where you would like to use it, you may place it in a folder called lib
:
wp-content/themes/your-theme/lib
HOW TO USE A WEBFONT WITH DOMPDF
The documentation here is almost not-existent and difficult for first-time users so i will place the steps here:
1. Download load_font.php
to the same lib
directory as dompdf:
curl -o load_font.php https://raw.githubusercontent.com/dompdf/utils/master/load_font.php
2. Open load_font.php
with a text editor. Change
require_once "autoload.inc.php";
to
require_once 'dompdf/autoload.inc.php';
3. Place your font under the same lib/fonts
folder. This way it is easier to use (these fonts will be copied inside the domdpf/lib/fonts) but you can also have the font in another folder, just make sure you update the paths mentioned below.
4. Make sure that php is in the PATH Environment Variable in your system.
5. Run the utility with the name of the font you are registering and the path to the TFF file.
So, open your command line, go to the lib directory and run
php load_font.php your-font-family ./path/to/font.ttf
for examle if i wanted to use the SourceSansPro font, i would download it (for example from fonts.google.com), place it in the lib/fonts folder:
php load_font.php SourceSansPro ./fonts/SourceSansPro-Regular.ttf ./fonts/SourceSansPro-Bold.ttt
The above command will convert the fonts in the needed format for dompdf.
CREATE YOUR LAYOUT
Now, all you have to do is create your desired output with html / php. Keep in mind that there are some problems with divs and float:left so its best to use the “good” old table layout for multi-columns layouts.
Example of use in order to create a pdf from php:
$html = '<html>
<head>
<meta http-equiv="Content-Type" content="charset=utf-8" />
<style type="text/css">
@page {
margin: 0;
}
* { padding: 0; margin: 0; }
@font-face {
font-family: "source_sans_proregular";
src: local("Source Sans Pro"), url("fonts/sourcesans/sourcesanspro-regular-webfont.ttf") format("truetype");
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: "source_sans_probold";
src: local("Source Sans Pro Bold"), url("fonts/sourcesans/sourcesanspro-bold-webfont.ttf") format("truetype");
font-weight: normal;
font-style: normal;
}
body{
font-family: "source_sans_proregular", Calibri,Candara,Segoe,Segoe UI,Optima,Arial,sans-serif;
color: #333;
background-color: #fff;
height:100%;
}
body b, table th{
font-weight: normal;
font-family: "source_sans_probold", Calibri,Candara,Segoe,Segoe UI,Optima,Arial,sans-serif;
}
table td, table th{
vertical-align: top;
}
</style>
</head>
<body>your html can be placed here</body>
</html>';
require_once 'lib/dompdf/autoload.inc.php';
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml($html, 'UTF-8');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4');
$dompdf->set_option('defaultMediaType', 'all');
$dompdf->set_option('isFontSubsettingEnabled', true);
// Render the HTML as PDF
$dompdf->render();
$dompdf->stream();