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

TimeSocks's avatar

laravel-dompdf not using CSS

I'm trying to use laravel-dompdf to render PDFs (obviously). I've got it working to the point that I can render data from my database as a PDF. However, I can't get any kind of CSS working - linked, in style tags, or even inline. It simply doesn't make a difference. I am just testing it at the moment with simple color and font-size changes, but no dice.

EDIT: Never mind, I was being a cretin and calling the wrong view. Why can't we delete our own questions?

0 likes
11 replies
pavj888's avatar

@TimeSocks Hey hi can you please paste in a code snippet I am facing the same problem. So I want the solution how you actually did it.

christopher's avatar

Just tried it and works without any problems. Did you set the Content Type to <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> as descriped?

Heres my example: ( Result: http://d.pr/i/1eQ3R )

Route:

Route::get('pdf', function() {
    $data = 'Just a test';
    $pdf = PDF::loadView('pdf.invoice', compact('data'));
    return $pdf->download('invoice.pdf');
});

HTML:

<!doctype html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Document</title>
    <style>
        body {
            background: black;
            color: #fff;;
        }
    </style>
</head>
<body>

<h1>{ { $data } }</h1>

</body>
</html>
Corez64's avatar

I have found that DOMPDF is not that great when dealing with CSS and formatting (although it may have improved since I last used it). I much prefer using wkhtmltopdf which is a CLI tool that uses the WebKit (HTML rendering engine used by Chrome, Safari and Opera) to generate the HTML save it to a PDF file which you can install using composer (AMD64, x86). KnpLabs have created a PHP library called snappy which adds a nice PHP wrapper to it and Barry vd. Heuvel has added a nice wrapper around that for Laravel: laravel-snappy.

pavj888's avatar

@ kayyyy as you said it works if we give styling within html style tag. but if i want to boostrap csshow should i do it?

@TimeSocks yup thatworks thank u

@Corez64 I need to try that whether thatsupports boostrap css file loading?

ChristopherSFSD's avatar

If you're using the elixir() helper the problem seems to be that the path returned begins with a / and dompdf doesn't like this.

This solved it for me ...

    <link rel="stylesheet" href="{{ ltrim(elixir('assets/css/app.css'), '/') }}" />
1 like
andrei.jurescu's avatar

The solution @ChristopherRaymond found worked for me as well

<link rel="stylesheet" href="{{ ltrim(elixir('css/pdf-invoice.css'), '/') }}" />

to download PDF file, and the usual

{!! HTML::style('css/pdf-invoice.css') !!}

used to view as html page.

develtip's avatar

For snappy this meta tag did the trick:

<base href="{{ url('/') }}">
1 like
takebrown's avatar

I have used laravel snappy. IT worked in my localhost but in server it return error.

The process has been signaled with signal "11"

RuskinF's avatar

Do not use DOMPDF to render the PDF version of an HTML page. The CSS style on the HTML was not used to be rendered very well by DOMPDF since the last time I confronted it.

As @corez64 suggested, use a simple CLI rendering application that displays the HTML in a PDF form.

Please or to participate in this conversation.