phpdockerpdffontsdompdf

DomPDF uses custom font on local server, but not in production


I'm using dompdf 3.1.0. The template html resides in public/templates/reports, while the custom fonts are at public/templates/reports/fonts. My CSS inside the template:

@font-face {
        font-family: "Ubuntu";
        src: url("./fonts/Ubuntu-Regular.ttf"); // I tried absolute path as well, no effect
      }

      body {
        font-family: "Ubuntu", serif;
        font-weight: 400;
        font-style: normal;
        font-size: 10pt;
      }

The DomPdf configuration:

        $options = new Options();
        $options->setIsRemoteEnabled(true);
        $options->setFontDir(APPPATH . '../public/templates/reports/fonts');
        $options->setFontCache(APPPATH . '../public/templates/reports/fonts');
        $options->setChroot(APPPATH . '../public/templates/reports/');
        $this->dompdf = new Dompdf($options);

At my local Apache server everything works as expected, however when I try it on the docker container, the font is the default one (even if I set the defaultFont on $options).

Docker image specification:

FROM php:8.3-fpm-alpine3.20

...

WORKDIR /var/www/html

COPY . .

RUN apk add --no-cache curl \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
    && composer install --no-dev \
    && chown -R www-data:www-data . \
    && chmod -R +r public

According to everything, DomPdf should find the font file. I would appreciate any help.


Solution

  • Finally I solved it by entering an internet URL instead of using local file.

    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Ubuntu:ital,wght@0,300;0,400;0,500;0,700;1,300;1,400;1,500;1,700&display=swap" rel="stylesheet">
    

    PHP:

    $options = new Options();
    $options->setIsRemoteEnabled(true);
    $this->dompdf = new Dompdf($options);