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

phayes0289's avatar

Linked CSS Not Rendering When Using barryvdh/laravel-dompdf

I am having a problem with CSS Styles using barryvdh/laravel-dompdf. Any CSS stylesheets that are included, do not seem to be rendered or used. It is my understanding that I should be able to do this using Laravel-PDF. As quoted from someone else…. “Think of DOMPDF as a web browser that will print your HTML file”

So, I have created a CSS Layout for my reports. It includes the linked CSS files. It looks like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    {{-- Report Title--}}
    @section('title', $reportTitle)
    
    <link id="appbundle" rel="stylesheet" media="screen, print" href="{{ URL::asset('css/app.bundle.css') }}">

    <link id="mytheme" rel="stylesheet" media="screen, print" href="{{ URL::asset('css/themes/cust-theme-15.css') }}">

    <link id="siriustheme" rel="stylesheet" media="screen, print" href="{{ URL::asset('css/sirius.css') }}">

    <!-- Latest compiled and minified CSS -->

    @yield('headerStyle')
    {{-- End Styles--}}

</head>
<body>

{{--Report Header--}}
<table id="headerTable">
    <tr>
        <td><img src="{{ public_path($report_logo) }}" alt="Logo" height="40px;"></td>
        <td style="text-align: right" id="address">
            <div class="p-0 m-0">Stamford Fire Department</div>
            <div class="p-0 m-0 ">629 Main Street</div>
            <div class="p-0 m-0">Stamford, CT</div>
        </td>
    </tr>
</table>
{{-- End Report Header--}}

<!-- content -->
@yield('content')

<footer>
    <div class="page-number"></div>
</footer>
</body>
</html>

This mis my blade report that uses that layout:

@php
    $reportTitle = 'FireOps Event';
@endphp

@extends('layouts.report')

@section('title', $reportTitle)

@section('headerStyle')
    <style>
        .fill {
            background-image: url('{{ $featuredImagePath }}');
        }
    </style>
@stop
@section('content')
    <main>
        <section>
            <h5>{{ $event->event_start_date->format('D d M Y') }}
                - {{ $event->event_start_date->format('D d M Y') }}</h5>
            <h1 style="margin-bottom:10px">{{ $event->title }}</h1>

            <table id="contentBox">
                <tr>
                    <td style="vertical-align: top;padding-right: 4px" class="post-content">
                        <div class="fill border rounded mt-2" id="titleBox" style="height: 200px;">

                        </div>

                        {!! $event->content !!}
                    </td>
                    <td style="width:200px;vertical-align: top;padding-left: 4px" class="post-content">
                        <div class="card border" style="">

                            <div class="card-header">
                                Meta Data
                            </div>
                            <div class="card-body">
                                <h5 class="card-title">Event Posted:</h5>
                                <p class="card-text">
                                    <span class="d-block"><small>Effective
                                    3 weeks ago</small></span>
                                </p>
                                <h5 class="card-title">Posted By:</h5>
                                <p class="card-text"><a
                                            href="">{{$event->author->firstname}} {{$event->author->lastname}}</a></p>

                                <h5 class="card-title">Tags:</h5>
                            </div>

                        </div>
                        {{-- Host Informartion --}}
                        <div class="card border mb-0">

                            <div class="card-header">
                                Contact Information
                            </div>
                            <div class="card-body">
                                @if($event->contact_org)
                                    <div>{{ $event->contact_org }}</div>
                                @endif
                                @if($event->contact_url)
                                    <div>{{ $event->contact_url}}</div>
                                @endif
                                @if($event->contact_name)
                                    <div>{{ $event->contact_name }}</div>
                                @endif
                                @if($event->contact_title)
                                    <div>{{ $event->contact_title }}</div>
                                @endif
                                @if($event->contact_email)
                                    <div>{{ $event->contact_email }}</div>
                                @endif
                                @if($event->contact_phone)
                                    <div>{{ $event->contact_phone }}</div>
                                @endif
                                @if($event->contact_address)
                                    <div>{{ $event->contact_address }}</div>
                                @endif
                                <div>
                                    @if( $event->contact_city)
                                        {{ $event->contact_city }}
                                    @endif
                                    @if( $event->contact_state)
                                        , {{ $event->contact_state }}
                                    @endif
                                    @if( $event->contact_postalcode)
                                        {{ $event->contact_postalcode }}
                                    @endif
                                </div>

                            </div>

                        </div>
                        {{-- Venue Indormation --}}
                        <div class="card border mb-0">
                            <div class="card-header">
                                Venue
                            </div>
                            <div class="card-body">
                                @if($event->venue_name)
                                    <div>{{ $event->venue_name }}</div>
                                @endif
                                @if($event->venue_org)
                                    <div>{{ $event->venue_org}}</div>
                                @endif
                                @if($event->venue_url)
                                    <div>{{ $event->venue_url }}</div>
                                @endif
                                @if($event->geo_street)
                                    <div>{{ $event->geo_streetnumber }} {{ $event->geo_street }}
                                        @if($event->geo_unit)
                                            {{ $event->geo_unit }}
                                        @endif
                                    </div>
                                @endif

                                <div>
                                    @if($event->geo_city)
                                        {{ $event->geo_city }}
                                    @endif
                                    @if($event->geo_state)
                                        , {{ $event->geo_state }}
                                    @endif
                                    @if($event->geo_postalcode)
                                        {{ $event->geo_postalcode }}
                                    @endif</div>
                            </div>

                        </div>

                    </td>
                </tr>
            </table>
        </section>
    </main>
@endsection

I am displaying the PDF in the browser window through the controller, which looks like this:

 public function previewPDF(Event $event)
    {

        $featuredImage = $event->getMedia('Event Images')->first();
        $featuredImagePath = $featuredImage->getPath(); // This will give you the file path of the image

        $pdf = PDF::loadView('events.pdf', compact('event', 'featuredImagePath'));

        // Set paper size and margins
        $pdf->setPaper('A4', 'portrait'); // Set paper size to A4 and orientation to portrait

        // Return the PDF response with a 200 OK status code
        return new Response($pdf->output(), 200, [
            'Content-Type' => 'application/pdf',
            'Content-Disposition' => 'inline; filename="report.pdf"',
        ]);
    }

Am I doing something wrong, or did I misinterpret the documentation? Thanks in advance.

0 likes
2 replies
LaryAI's avatar
Level 58

It seems like the issue you're encountering with the barryvdh/laravel-dompdf package not rendering linked CSS files is a common one. DOMPDF, the underlying library used by this package, has some limitations and specific requirements when it comes to handling external resources like CSS files.

Here are a few steps and tips to ensure your CSS is applied correctly when generating PDFs:

  1. Use Absolute URLs for CSS Files: DOMPDF handles external stylesheets better when they are referenced with absolute URLs. You can use Laravel's asset() helper to generate absolute URLs.

  2. Embed CSS Directly: As an alternative to linking external stylesheets, consider embedding the CSS directly within the <style> tags in your HTML. This ensures that all styles are available to DOMPDF without relying on external file fetching.

  3. Check CSS Compatibility: Ensure that your CSS uses properties and values that are supported by DOMPDF. Some modern CSS features might not be supported.

  4. Permissions and Path Issues: Ensure that the paths to your CSS files are correct and that the server has the necessary permissions to access those files.

Here's how you might adjust your code:

Adjusting the Blade Template

Instead of linking to external CSS files, embed the CSS directly within the HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ $reportTitle }}</title>
    
    <style>
        @import url('{{ public_path('css/app.bundle.css') }}');
        @import url('{{ public_path('css/themes/cust-theme-15.css') }}');
        @import url('{{ public_path('css/sirius.css') }}');
        /* Additional embedded styles */
    </style>
    
    @yield('headerStyle')
</head>
<body>
    {{-- Content here --}}
</body>
</html>

Controller Method

Ensure that the paths and permissions are correctly set, and consider logging or debugging to confirm that paths resolve as expected.

public function previewPDF(Event $event)
{
    $featuredImage = $event->getMedia('Event Images')->first();
    $featuredImagePath = $featuredImage->getPath(); // Ensure this path is correct

    $pdf = PDF::loadView('events.pdf', compact('event', 'featuredImagePath'));
    $pdf->setPaper('A4', 'portrait');

    return new Response($pdf->output(), 200, [
        'Content-Type' => 'application/pdf',
        'Content-Disposition' => 'inline; filename="report.pdf"',
    ]);
}

By embedding the CSS directly within your HTML document, you can avoid issues related to external file loading and ensure greater compatibility with DOMPDF's rendering engine.

Please or to participate in this conversation.