phplaravelpdfdompdflaravel-vapor

Laravel Vapor + Docker, DomPDF not rendering custom fonts, works locally, not on staging


I am having some trouble getting custom fonts working with DomPDF and Laravel Vapor. (Localy the fonts work)

These are my CSS font definitions (fonts are stored inside public/fonts)

@font-face {
            font-family: caveat;
            font-style: normal;
            font-weight: normal;
            src: url('{{ public_path('fonts/caveat.ttf') }}') format('truetype');
        }

        @font-face {
            font-family: Arial;
            font-style: normal;
            font-weight: normal;
            src: url('{{ public_path('fonts/Arial.ttf') }}') format('truetype');
        }

This is my dockerfile

FROM laravelphp/vapor:php81

RUN apk add imagemagick imagemagick-dev php81-pecl-imagick \
&& pecl install imagick \
&& docker-php-ext-enable imagick

COPY . /var/task

Locally these fonts Arial and CaveAt get rendering fine in the PDF. However after deployment to staging, the font in the PDF falls back to Helvetica

I probably need to add some commands to the dockerfile in order to get it working. My question is: What commands?


Solution

  • First a big thanks to the answer of VonC for his long and useful answer. It did not solve my particular issue but it's good information. I highly recommend giving it a read even if my solution works for you.

    My answer is perhaps not the most recommended solution, but it works well for Laravel Vapor environments (and my specific case).

    How I solved it:

    fonts stored in /public/fonts

    public/fonts/Arial.ttf
    public/fonts/caveat.ttf
    

    adjustments in config/dompdf.php (Works with Laravel Homestead && Serverless Vapor has a useable /tmp folder that is alive per request and can be used to write temporary files to. Creating folders within /tmp in Dockerfile does not seem to work )

        "font_dir" => '/tmp/',
        "font_cache" => '/tmp/',
        "temp_dir" => '/tmp/',
    

    CSS font definitions

    @font-face {
        font-family: caveat;
        font-style: normal;
        font-weight: normal;
        src: url('{{ asset('fonts/caveat.ttf') }}') format('truetype');
    }
    
    @font-face {
        font-family: Arial;
        font-style: normal;
        font-weight: normal;
        src: url('{{ asset('fonts/Arial.ttf') }}') format('truetype');
    }
    

    Dockerfile

    FROM laravelphp/vapor:php81
    
    RUN apk add imagemagick imagemagick-dev php81-pecl-imagick \
    && pecl install imagick \
    && docker-php-ext-enable imagick
    
    COPY . /var/task
    

    I cannot express how happy I was to finally see my PDF render correctly after deployment.