Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shawdow's avatar

dompdf/dompdf package font family not reflecting!!

trying to generate pdf by adding font-family in css.it's reflecting as per documentation mentioned in dompdf/dompdf..please help what mistake i am doing here..to reflect font-family

it always shows Times-Roman when i download the certificate!!

controller code

public function generateSamplePdf(Request $request) {

        try {


            // Render the first view and capture its HTML content
            $html = view('sample-pdf', $request->all())->render();
            
            $fontDir = storage_path('fonts/cooper');

            // Create new Dompdf instance
            $dompdf = new Dompdf([
                'fontDir' => $fontDir,
            ]);

            // Load HTML to Dompdf
            $dompdf->loadHtml($html);

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

            // Render PDF (optional: you can set additional options here)
            $dompdf->render();

            //convert to base64
            $pdf_to_base64 = base64_encode($dompdf->output());

            // return response()->json([
            //     'status' => 'success',
            //     'statusCode' => 200,
            //     'data' => $pdf_to_base64,
            // ]);

            // Output PDF
            return $dompdf->stream("dompdf_out.pdf", array("Attachment" => false));

        } catch (\Exception $e) {
            // Handle the exception and return an error response
            return response()->json([
                'status' => 'error',
                'statusCode' => 500,
                'message' => 'An error occurred while generating the PDF. Please try again later.',
                'error' => $e->getMessage(), // Optionally include the error message
            ], 500);
        }
    }

sample pdf file

<!DOCTYPE html>
<html>
<head>
<title>PDF Document</title>
<?php
   $font_url = storage_path("fonts/cooper/Cooper Regular.ttf")
?>
<style>
      @font-face {
        font-family: 'Cooper';
        src: url({{ $font_url }}) format('truetype');
        font-weight: 400;
        font-style: normal;
      }
</style>
</head>
<body>
<div class="title"></div>
<p>This is a sample PDF document generated using Laravel and DOMPDF.</p>
<p>This is a sample PDF document generated using Laravel and DOMPDF.</p>
</body>
</html>
0 likes
1 reply

Please or to participate in this conversation.