Hello Devs, 

In this tutorial, we will learn Laravel Dompdf Set Different font-family Example

When generating a PDF with Dompdf then it ignores fonts that are not available in its internally – Helvetica, Times-Roman, Courier, Zapf-Dingbats, Symbol.

Require to load the fonts for use in PDF creation which is not existing in Dompdf internal fonts.

Follow this step-by-step guide below. 


Step 1: Download Dompdf

Download Dompdf latest version from here


Step 2 :Table structure

CREATE TABLE `users` (
  `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `username` varchar(100) NOT NULL,
  `name` varchar(100) NOT NULL,
  `email` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;



Step 3: Configuration

Create a config.php

<?php

$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "tutorial"; /* Database name */

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
  die("Connection failed: " . mysqli_connect_error());
}


Step 4: Copy fonts

Create a new fonts folder at the project root.

Copy your fonts which you want to use in your pdf to the fonts folder


Step 5 :Create PDF and set font-family

include "config.php";

$html = "";
$html .= "<table border='1' width='100%' style='border-collapse: collapse;'>
        <thead>
            <tr>
                <td>S.no</td>
                <td>Username</td>
                <td>Name</td>
                <td>Email</td>
            </tr>
        </thead>
        <tbody>
";

$usersData = mysqli_query($con,"select * from users");
$sno = 1;
while($row = mysqli_fetch_assoc($usersData)){
    $html .= "<tr>
        <td>".$sno++."</td>
        <td>".$row['username']."</td>
        <td>".$row['name']."</td>
        <td>".$row['email']."</td>
    </tr>";
}
$html .= "</tbody></table>";

$filename = "newpdffile";

// include autoloader
require_once 'dompdf/autoload.inc.php';

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();

$dompdf->loadHtml($html);

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

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

// Output the generated PDF to Browser
$dompdf->stream($filename);


May this example help you.