Dompdf is a library to generate PDF from HTML code. Examples can be found at

In the following example we use twig for the html template and a php-script that initiates the rendering of an pdf via dompdf. We also use a custom font.

Template example

        @font-face {
            font-family: yourfont;
            src: url(vendor/bower_components/yourFont-fonts/fonts/yourFont.ttf);
        @page {
            margin: 2cm;
        body {
            text-align: center;
            font-family: yourfont;
        hr {
            page-break-after: always;
            border: 0;
    {% if twiglogo %}
        <img src="{{ twiglogo }}" alt="Logo"/>
    {% endif %}
    <p>{{ sometext }}</p>

PHP Snippet

class Pdf_RetrainingConfirmation implements Kwf_Media_Output_Interface
       public static function getMediaOutput($id, $type, $className)
           define("DOMPDF_ENABLE_AUTOLOAD", false);

           $twig = new Kwf_View_Twig_Environment();
           $imagesource = 'http://sourceurl for some image';
           $html = $twig->render('app/Pdf/RetrainingConfirmation.twig', array(
               'twiglogo' => $imagesource,
               'sometext' => 'sometext'

           $dompdf = new DOMPDF();

           $output = $dompdf->output();
           $filename = 'yourFileName.pdf';
           file_put_contents($filename, $output);

           return array (
               'contents' =>  $output,
               'mimeType' => 'application/pdf',
               'lifetime' => false,
               'downloadFilename' => $filename

Custom Fonts

f you use a custom font, as in the example, reverence to that font with font-face in CSS as we did above.

How to add a font

To add a custom font, it is recommend to load it via composer. If there is no composer-package for the desired font but you have the fontfile, than you can make an repository for the font and load it with composer, see this article.

Not recommend but possible is to simple make an "font" folder at rootlevel and copy your fontfile into it and reference to that fontfile.

For other ways to add a font see dompdf documentation and/or following usefull links:

Important to know


Dompdf doesn't scale images, instead it embeds the origin image into the pdf. This can generate big pdf's, depanding on the imagesize. To get the pdf filesize as small as possible, we have to scale it with the scale() function from the Kwf_Media_Image Class. Then we get an imageblob that we have to encode to base64 to embed it in our template.


$imageBlob = Kwf_Media_Image::scale($honorLogo, array('width' => 200, 'height' => 200, 'cover' => false));

$imageBase64 = base64_encode($imageBlob);

$mimeType = 'image/png';

$src = 'data:'.$mimeType.';base64,'.$imageBase64;

For caching see this link.