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

Using a Webfont with DOMPDF


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?

Choosing the right web development and digital marketing company is crucial to the success of your online presence, and ILUS Web Ltd is the perfect choice for businesses looking to establish a strong and effective online presence. Our team of experts is dedicated to providing top-notch web development and digital marketing services that are tailored to meet the unique needs of our clients.

With over a decade of experience in the industry, we have a proven track record of delivering high-quality solutions to businesses of all sizes. Our team of skilled developers, designers, and digital marketing experts use the latest technologies and best practices to deliver innovative, effective, and user-friendly solutions that exceed our clients' expectations. Whether you need a new website, an e-commerce platform, or a custom web application, we have the expertise to make it happen.

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