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

Sturm's avatar
Level 5

DomPDF not working with Sail

I'm switching over our local dev environments from Lando to Laravel Sail. I've been successful so far, but for some reason, the PDFs generated with Laravel DOMPDF are coming out unstyled and with images missing.

Before, with Lando:

Lando

After, with Sail:

Sail

And here is a snippet of the Blade template that we're using:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ config('app.name', 'IEL Nexus') }}</title>

    <!-- Google fonts needed since Font Bunny doesn't work with dompdf -->
    <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=Homemade+Apple&family=Roboto:ital,wght@0,400;0,500;0,700;1,400;1,500;1,700&display=swap" rel="stylesheet">

    @vite('resources/css/app.css')
    <style>
        body {
            font-size: 10px;
        }

        .font-sans {
            font-family: 'Roboto', sans-serif;
        }

        .font-signature {
            font-family: 'Homemade Apple', handwriting, cursive;
        }
        dt {
            float: left;
            font-weight: bold;
            clear: both;
            padding-top: 5px;
        }

        dd {
            padding-top: 5px;
            text-align: right;
        }

        .w-third {
            width: 33%;
        }

        .w-fifth {
            width: 20%;
        }

        .w-two-fifths {
            width: 40%;
        }

        .w-quarter {
            width: 25%;
        }

        .w-half {
            width: 50%;
        }

        td {
            padding-top: 5px;
        }
    </style>
</head>

<body class="font-sans">
    <table class="w-full border-separate table-fixed border-spacing-2">
        <tr>
            <td class="w-two-fifths pl-2 align-top">
                <img src="{{ asset('images/logo-ratecon.png') }}" alt="RateCon Logo">
            </td>
            <td class="w-two-fifths px-2 align-top">
                <p><strong>Dispatch Sheet</strong></p>
                <p>
                    Integrity Express Logistics<br>
                    PO Box 42275 - Cincinnati, OH 45242<br>
                    Tel.: 937-502-1024 Ext: 1024 - Fax: 855-600-2467 - Email: {{ $customer_email }}
                </p>
            </td>
            <td class="w-fifth pl-2 align-top text-right">
                <strong>IEL PO#: {{ $po_number }}</strong>
            </td>
        </tr>
    </table>

<!-- and so on… -->

All I can figure out is that some prerequisites aren't installed with Sail by default but are with Lando. Looking at the prerequisites in the underlying DOMPDF package:

firefox_xr3yin5xYw.png

I cannot tell which ones are already installed in Sail by default, which I need to add, and how to add them.

I've already published Sail's vendor files so that I can edit docker/8.2/Dockerfile and docker/8.2/php.ini if need be; I just don't know what to put in them. Same with docker-compose.yml.

Can anyone please help me?

0 likes
24 replies
Sturm's avatar
Level 5

@vincent15000 We're using some of the classes from Tailwind CSS, but we've discovered that other classes, such as w-1/2, don't work because of the slash. So we avoided using them.

Still doesn't explain why it cannot find the image file, though.

1 like
vincent15000's avatar

@Sturm Some of the frameworks' classes aren't interpreted by Laravel DOMPDF, so it's better to not use a CSS framework if you need to generate a PDF file with Laravel DOMPDF.

The best way is to write your own CSS code.

Sturm's avatar
Level 5

@vincent15000 That's fine, I completely understand that, and I'd be willing to use standard non-framework styling, but it still leaves two questions:

  1. Why did it work fine with those classes when using Lando as my local dev environment?
  2. That doesn't explain why it cannot find the logo image file, which is a standard <img> element.
1 like
vincent15000's avatar

@Sturm For Lando, I don't know.

For the image, the possible reasons are multiple :

  • configuration of the base URL in the .env file

  • generation of the image URL inside the code

  • ...

Sturm's avatar
Level 5

@vincent15000 I did change APP_URL=http://nexus.lndo.site/ to APP_URL=http://localhost/ when moving from Lando to Sail, because that was the only URL that would take me to the site's homepage after running sail npm run dev.

The URL that asset('images/logo-ratecon.png') generates in that blade template is http://localhost/images/logo-ratecon.png, which exists, since that png is in the public/ folder.

1 like
vincent15000's avatar

@Sturm The APP_URL is used by Laravel to generate URLs from asset() and some other similar functions to generate URLs (like for example temporary URLs, ...).

The APP_URL has to contain the value of the website's base URL.

And sure in local development environment, it's not the same value than in production.

Sturm's avatar
Level 5

@vincent15000 Yes, and I have set it to http://localhost/ because that seems to be the only way can access the site locally after sail up -d and sail npm run dev. AFAIK, that is the site's base URL.

When asset() generates the image URL of http://localhost/images/logo-ratecon.png, I can paste that URL into a separate tab and it displays the PNG file, as expected. Which is why I am so confused by DOMPDF displaying an error message in place of the image on the PDF: "Image not found or type unknown".

1 like
GabrieleSbaiz's avatar

@Sturm I'm having a similar problem right now.

Try setting DOMPDF config 'show_warnings' => true to receive error messages from DOMPDF. In my case Connection refused on file_get_contents for css and img.

After changing asset to public_path on img and css in blade view, dompdf generate the right PDF even using sail.

I hope that this can help you.

1 like
Sturm's avatar
Level 5

@GabrieleSbaiz Thank you for the heads-up on show_warnings => true. Now, at least, I can see the exact problems DOMPDF is having.

Unfortunately, there are several issues that DOMPDF is having with this template under Sail and changing asset to public_path might only fix one of them. (It also means the image doesn't get shown in the preview.)

firefox_abdnLUu33h.png

1 like
Robstar's avatar

Try setting the body to a fixed pixel width (i.e. 595px if at 72dpi).

For the images have you enabled the "remote" setting (enable_remote within the published config file)?

However, can;t explain why switching dev. environments caused issues. I've personally always used LaraDock idf that helps.

1 like
Sturm's avatar
Level 5

@Robstar I don't see how setting the body to a fixed pixel width will help with the other styling options not being linked properly.

As @vincent15000 asked, what is this enable_remote setting, what does it do (more specifically, how would it help my situation), and how is it set? I cannot seem to find any documentation on it.

1 like
Robstar's avatar

@Sturm It's just part of the debug process. If you know a fixed width area allows percentage based columns to display correctly then you mostly likely have a media query issue when rendering PDFs.

1 like
Sturm's avatar
Level 5

@Robstar I've already determined that adding any styles directly to the template work fine. The problem seems to be pathing more than anything else. The DOMPDF renderer just cannot find the compiled .css file and logo image file.

Thank you for explaining enable_remote to me. That is a good idea, since the problem seems to be DOMPDF's inability to "find" external files (external to the blade template, that is), but I'm afraid that settings is already enabled (and is enabled by default, even).

1 like
krekas's avatar

With dompdf supports CSS 2.1 basically. Better use spaties new package for pdf generation.

1 like
Sturm's avatar
Level 5

@krekas While I'm not completely opposed to switching to a different package, I'd like to avoid it as much as possible. In addition, I've heard that the main requirement for Spatie's package is Puppeteer, which is a relatively weighty dependency.

Besides, from what I can tell, the problems I am experiencing are not specific to Laravel DOMPDF. Because the cause most likely has to do with Vite and pathing. Thus, I am likely to run into this same issue no matter what PDF creation package I use.

1 like
krekas's avatar

@Sturm because you are using sail (docker). Docker needs extra configuration

1 like
Sturm's avatar
Level 5

@krekas Lando also uses Docker. Regardless, what extra configuration would be needed?

1 like
Sturm's avatar
Sturm
OP
Best Answer
Level 5

Working with Tighten, a highly knowledgeable team of Laravel professionals, I finally have an answer.

I needed to set PHP_CLI_SERVER_WORKERS=3 in my .env file because Sail runs php artisan serve by default, which means that only has a single process. Therefore, it cannot run the web request to render the HTML + the asset fetching from the PDF renderer simultaneously. So, setting this environment variable to a number higher than 1 tells the PHP built-in web server to spin up more workers, which solves this issue.

1 like
Sturm's avatar
Level 5

@vincent15000 Yeah, that didn't come out how I meant it to. I'm sorry. I was only trying to give context to Tighten, not disparaging the help I received here.

Please or to participate in this conversation.