skip to Main Content

I cannot get my pdf generator to work. I’m using dompdf/dompdf library and I have a lot of trouble generating PDF I want. My issue is: every time I try to generate PDF with file that is using style with family-font: DejaVu Sans I get 500 ERROR and nothings happening.

When I delete line including "DejaVu Sans" everything works, but I need PDF file to handle extended Latin characters like "ążćłóńę". Is there any way to make this work, or any other way to use extended Latin in DomPdf?

PHP Generate:

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

use DompdfDompdf;
use DompdfOptions;

$options = new Options();
$options->set('isRemoteEnabled', true);

$dompdf = new Dompdf($options);

$php = file_get_contents("inputPdfData.php");

$dompdf->setPaper('A4', 'portrait');
$dompdf->loadHtml($php);

$dompdf->render();
$dompdf->stream();

"inputPdfData.php"

<html lang="pl">
<head>
    <title>PDF GENERATOR</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta charset="UTF-8" content="text/html">
<style>
    * {
        font-family: "DejaVu Sans", sans-serif;
    }
</style>
</head>
<body>
    ążćęłóńź
    ĄŻĘĆŹŁÓŃ
</body>
</html>

"composer.json"

{
    "require": {
        "dompdf/dompdf": "^2.0"
    }
}

Error I get:
Error

2

Answers


  1. Chosen as BEST ANSWER

    I end up not using dompdf because of multiple issues. For anyone struggling: try using other library mPDF worked like magic for me. It worked first time with no issues about font or charset. Maybe this will help someone avoid hours of searching for an answer (about dompdf)


  2. Normally after you have installed DOMPDF, the DejaVu TrueType fonts have been pre-installed to give dompdf decent Unicode character coverage by default.

    Aother way (instead of using DejaVu) to use extended Latin in DomPdf (or to render polish fonts, etc), you may choose using Poltawski Nowy font.

    The trick is to use google font, such as

    <link href="https://fonts.googleapis.com/css2?family=Poltawski+Nowy:ital,wght@0,400..700;1,400..700&display=swap" rel="stylesheet">
    

    So please find below a fully working code (modified based on your original code):

    PHP (e.g. font1.php)

    <?php
    require 'vendor/autoload.php';
    
    use DompdfDompdf;
    use DompdfOptions;
    
    $options = new Options();
    $options->set('isRemoteEnabled', true);
    
    $dompdf = new Dompdf($options);
    
    $php = file_get_contents("inputPdfData.php");
    
    $dompdf->setPaper('A4', 'portrait');
    $dompdf->loadHtml($php);
    
    $dompdf->render();
    $dompdf->stream();
    

    inputPdfData.php

    <html lang="pl">
    <link href="https://fonts.googleapis.com/css2?family=Poltawski+Nowy:ital,wght@0,400..700;1,400..700&display=swap" rel="stylesheet">
    
    <head>
        <title>PDF GENERATOR</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta charset="UTF-8" content="text/html">
    <style>
        * {
            font-family: "Poltawski Nowy", sans-serif;
        }
    </style>
    </head>
    <body>
    
    ążćęłóńź <br>
    ĄŻĘĆŹŁÓŃ <br>
    
    Stack Overflow is good 
    
    </body>
    </html>
    

    See below for the result

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search