laravel-dompdf
laravel-dompdf copied to clipboard
CSS and Images no longer working
Hi there!
I'm using this package with laravel modules and the CSS and the external images are no longer working.
Everything works on my local environment as it should, but on the server, the style is no longer applied in the PDF.
This is how i am importing the CSS: {{ module_vite('build-eventpage', 'resources/assets/sass/app.scss') }}
and this is how i am using the images:
<img src="https://images.domain.com/domain-logo-orange.svg" alt="logo" class="logo__image">
It's weird because on my staging server the CSS is working but the images are not, and on the production nothing is working anymore.
Is there something that is blocking the images to be rendered and the CSS to be applied?
Thank you!
Production:
Staging:
Local:
I have the same problem. I am not using the alt tag so my image is rendered like this:
Found the solution after enabling the warnings with $pdf->setWarnings(true);
We need to set the following option 👍
$pdf->setOption(['isRemoteEnabled' => true]);
I'm embedding my images like this <img src="{{ public_path('resources/img/logo-big.jpg') }}" id="brand" alt="Logo" >
CSS is very limited.
Steps to check:
- $pdf->setOption(['isRemoteEnabled' => true]); if you are passing image paths.
- Pass image data as base64 strings to img elements will also work.
- Remove old config file and publish again. Config file has changed and this also afect image load on PDFs.
Here’s a quick, real-world example of how I fixed it simply by inlining the image as a Base64 data URI—no more temp-dir or “connection refused” drama:
// In your controller
$viewHtml = view('certificate-pdf', ['model' => $model]);
$pdf = PDF::loadHTML($viewHtml)
// you can leave remote enabled in case you still need it elsewhere
->setOption(['isRemoteEnabled' => true])
->setPaper('A4', 'landscape');
return $pdf->stream('certificate.pdf');
{{-- resources/views/certificate-pdf.blade.php --}}
@php
// grab just the filename from whatever path you stored
$fileName = basename($model->background_path);
$imageFile = public_path("storage/images/{$fileName}");
if (file_exists($imageFile)) {
$mime = mime_content_type($imageFile);
$base64 = base64_encode(file_get_contents($imageFile));
$dataUri = "data:{$mime};base64,{$base64}";
} else {
$dataUri = '';
}
@endphp
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style>
.background {
/* inline the PNG so Dompdf never has to fetch or write anything */
background-image: url("{{ $dataUri }}");
background-repeat: no-repeat;
background-size: cover;
width: 1122px;
height: 793px;
position: absolute;
top: 0;
left: 0;
}
/* ...your other styles... */
</style>
</head>
<body>
<div class="background"></div>
{{-- your certificate content here --}}
</body>
</html>
Why this works
- Base64 data URI: Dompdf just reads the CSS and inlines the bits—no HTTP call, no temp folder, no permission headaches.
- You still have
isRemoteEnabledon in case you need to pull in any other assets later.
That little switch got my background showing up every time without error.