Set custom fonts for dompdf

For a recent project that deals with events, I wanted to give users an option to download event details as a PDF. I have considered several PHP pdf generation libraries and finally settled with Dompdf. Dompdf is an easy to user HTML to PDF converter PHP library. It supports CSS styling, inline style tags and most html tags.

I have installed the library using composer and added autoload file to include composer libraries. added required html and generated the pdf file.

<?php
require __DIR__ . '/vendor/autoload.php';

// reference the Dompdf namespace
use Dompdf\Dompdf;
use Dompdf\Options;

$content = <<<HTML
<div>
  <p>
  Bro ipsum dolor sit amet endo afterbang glades, road rash chowder first tracks japan air free ride couloir 
  derailleur hammerhead punter. Face shots bonk bowl spin hellflip. Poaching butter epic, crank betty taco glove 
  face plant slash shreddin ripper death cookies bonk brain bucket wack. Epic skinny ripping grunt OTB spread
  eagle couloir piste euro granny gear snake bite huck cruiser pillow popping bunny slope. 
  </p>

  <p>Ollie bunny slope bomb hole hammerhead brain bucket corn white room. Bonk bro 180 twister beater DH yard sale 
  hammerhead road rash corn roadie shred. Hardtail snake bite glades gnar skid lid pow pow dirt flow. Bear trap Ski 
  cornice groomer, poaching dope pow.</p>

  <p>Stoked giblets twister north shore, death cookies bowl pow tele gnar crunchy. Grind line daffy fatty bomb 
  hole grind wheelie drop clipless shred saddle chowder park. Rip apres smear free ride gorby freshies DH couloir 
  betty hurl carcass carve gnar wheels sketching spin. Snake bite table top berm, clean smear huck fatty over the bars 
  wheels huck liftie. Carbon couloir misty Whistler.</p>
</div>

HTML;

// instantiate and use the dompdf class
$options = new Options();
$options->set('isRemoteEnabled', true);
$dompdf = new Dompdf($options);
$dompdf->loadHtml($content);

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'portrait');

// Render the HTML as PDF
$dompdf->render();

And then after everything is working as expected,. customer didn’t like the default font. he wanted the branded front in the pdf. I have looking on stackoverflow and some other forums. couldn’t find any answer.

Photo by Road Trip with Raj on Unsplash

Dompdf initially support Helvetica, Times-Roman, Courier, Zapf-Dingbats, & Symbol. setting font family using css won’t work.

After few days of search, I have found the solution. Font style links need to be added dompdf html content when it renders. so the code should look as below. In this way you can use any custom font inside dompdf.

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Muli:400,600,700,800">
<style>
</head>
<?php
require __DIR__ . '/vendor/autoload.php';

// reference the Dompdf namespace
use Dompdf\Dompdf;
use Dompdf\Options;

$content = <<<HTML
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Muli:400,600,700,800">
<style>

</head>
body {
  font-family: "Muli", sans-serif;
  font-weight: 400;
}
</style>
<body>
<div>
  <p>
  Bro ipsum dolor sit amet endo afterbang glades, road rash chowder first tracks japan air free ride couloir 
  derailleur hammerhead punter. Face shots bonk bowl spin hellflip. Poaching butter epic, crank betty taco glove 
  face plant slash shreddin ripper death cookies bonk brain bucket wack. Epic skinny ripping grunt OTB spread
  eagle couloir piste euro granny gear snake bite huck cruiser pillow popping bunny slope. 
  </p>

  <p>Ollie bunny slope bomb hole hammerhead brain bucket corn white room. Bonk bro 180 twister beater DH yard sale 
  hammerhead road rash corn roadie shred. Hardtail snake bite glades gnar skid lid pow pow dirt flow. Bear trap Ski 
  cornice groomer, poaching dope pow.</p>

  <p>Stoked giblets twister north shore, death cookies bowl pow tele gnar crunchy. Grind line daffy fatty bomb 
  hole grind wheelie drop clipless shred saddle chowder park. Rip apres smear free ride gorby freshies DH couloir 
  betty hurl carcass carve gnar wheels sketching spin. Snake bite table top berm, clean smear huck fatty over the bars 
  wheels huck liftie. Carbon couloir misty Whistler.</p>
</div>
</body>
HTML;

// instantiate and use the dompdf class
$options = new Options();
$options->set('isRemoteEnabled', true);
$dompdf = new Dompdf($options);
$dompdf->loadHtml($content);

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'portrait');

// Render the HTML as PDF
$dompdf->render();