You are here: |Blog|Coding|Using A Webfont With Dompdf

Using a Webfont with DOMPDF


Coding

DOMPDF is a reliable class to convert HTML documents to PDF. It is compatible with most CSS 2.1 rules. The resulting pdf has remarkable quality, which is comparable to other paid (and pricey) solutions. By adding custom fonts, we will not only be able to display UTF-8 characters, such as Greek, Cyrillic etc, but also we will give our document this personalized character.

First you should install DOMPDF. This can be done by downloading and decompressing it to a directory of your choice of a php-enabled web server.
If you haven't already downloaded DOMPDF, you can get it from here:

https://github.com/dompdf/dompdf/releases

These fonts are embedded within the release:

  • Courier (Normal, Bold, Oblique, and BoldOblique variants)
  • Helvetica (Normal, Bold, Oblique, and BoldOblique variants)
  • Times (Normal, Bold, Oblique, and BoldOblique variants)
  • Symbol
  • ZapfDingbats

and as of release 0.6.0, DejaVu TrueType fonts are also included.

And now the fun begins!

So, lets say that we want to embed the "Roboto" webfont (regular and bold).

First, change to the lib directory of your dompdf installation:

    cd /path/to/dompdf/lib

Download load_font.php inside that (lib) directory.

    curl -o load_font.php https://raw.githubusercontent.com/dompdf/utils/master/load_font.php

Open load_font.php with your favorite text editor.

Replace

    require_once "autoload.inc.php";

To:

    require_once 'dompdf/autoload.inc.php'; 

Download Roboto font (regular and bold variations) from your favorite source, and place both .ttf under the same lib folder and in subfolder fonts.

    lib/fonts/

This is the generic way to run the load_font.php script

    php load_font.php new_font_family ./path/to/new_font.ttf

...or in our case:

    php load_font.php Roboto ./fonts/Roboto-Regular.ttf ./fonts/Roboto-Bold.ttt

The new webfont is now embedded in DOMPDF!

 

To use it, specify it in the head tag of your html code:

 <?php
/* make sure you escape the double quotes like this: \" */
$page="<html>
<head>
<style>
body{font-family: 'Roboto', sans-serif;}
... rest of css rules here ...
</style>
... your other 'head' tags here ...
</head>
<body>
... HTML CONTENT HERE ...
</body>
</html>";

/* then use DOMPDF to make the conversion: */

require_once "lib/dompdf/autoload.inc.php";

use Dompdf\Dompdf;

$dompdf=new Dompdf();

$dompdf->loadHtml($page, "UTF-8");

$dompdf->setPaper("A4", "portrait");

$dompdf->set_option('isFontSubsettingEnabled', true);
$dompdf->set_option('defaultMediaType', 'all');

$dompdf->render();

$dompdf->stream(); // stream the pdf in browser

The above commands for DOMPDF are pretty much self explainatory. 
For any further clarifications, refer to the author's page:

https://github.com/dompdf/dompdf

 


We will be delighted to hear from you

Why choose us?

Simply because we love creating beautiful websites, and we are damn good at it!

We at ILUS Web love programming!
All kinds of programming... Applications programming, Microcontrollers programming, Websites programming.
And we like to try to approach perfection everytime!

The best thing about a boolean is even if you are wrong, you are only off by one bit.

A beautiful and functional design is very important, as it interacts directly with the end User. And after all, it's the User's experience that we all work for!
Because a happy User, is of the most valuable assets to a business!

If you think good design is expensive, you should look at the cost of bad design.

A Website without S.E.O. is like a ship with broken steering, sailing in stormy weather!
In this day and age, the number of websites is reaching new records every day. Consequently, the competition of every new Site is huge, and the only way to "survive" is a methodical and successful S.E.O.

S.E.O. is what the Search Engines talk about you, when you are not present!
chart