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

frapto's avatar

Laravel Snappy not rendering headers and footers and ignoring my css?

Hi, after my last question regarding the timeouts, i switched to linux (ubuntu 20.04 LTS to be specific) so the pcntl libraries work. However this brought more problems. I installed snappy and fixed the wkhtmltopdf paths but suddenly headers and footers are not getting rendered. This is my controller code

public function createPDF(){
        $pdf = App::make('snappy.pdf.wrapper');
        $footer = \view('supporting.footer')->render();
        $header = \view('supporting.header')->render();
        $userData = \collect([$this->userData[1]]); //just for faster rendering and testing
        $pdf->loadView('orders', ['users' => $userData])
        ->setOption('margin-top', '20mm')
        ->setOption('margin-bottom', '20mm')
        ->setOption('minimum-font-size', 25)
        ->setOption('header-html', $header)
        ->setOption('footer-html', $footer);
        $pdf->save($this->path);
}

my header view:

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

</head>
<body>
    <div style="background: blanchedAlmond;color:green;">My Header is amazing<div>
</body>
</html>

This is my footer view:

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    <div style="background: blanchedalmond; color:green;">Copyright &copy; 2020</div>
</body>
</html>

My orders view: (just some basic tables)

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Order Layout</title>
    <style>
        *{
            font-family: cursive;
        }

        .wrapper {
            display: block;
        }

        .invoice {
            display: block;

            width: 80%;
            margin: 0 auto;
            margin-top: 10px;
            padding-top: 10px;
            padding-bottom: 10px;
            background: #d3d3d3;
            page-break-inside: avoid !important;
            padding-left: 20px;
        }

        .order-items {
            padding-left: 100px;
        }

        .table {
            width: 90%;
            align-self: center;
            border: 1px solid black;
            page-break-inside: avoid !important;
            orphans: 15;
        }
        .table>* {
            text-align: center;
        }
    </style>
</head>

<body>

    <main class="wrapper">
        @foreach ($users as $user)

        @php
        $orders = $user->orders //just for renaming
        @endphp
        @foreach ($orders as $order)
        
        <div class="invoice">
            @php
            $sum=0;
            @endphp
            <h2>{{$user->name}}: {{$order->id}}</h2>
            <div class="order-items">
                <table class="table" border="1">
                    <thead>
                        <tr>
                            <th>Product Name</th>
                            <th>Unit Price</th>
                            <th>Qty</th>
                            <th>subtotal</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($order->products as $product)
                        <tr>
                            <th>
                                {{$product->name}}<br>
                            </th>
                            <td>{{$product->unit_price}}</td>
                            <td>{{$product->pivot->quantity}}</td>
                            @php
                            $sum+= $product->pivot->quantity*$product->unit_price
                            @endphp
                            <td>{{$product->pivot->quantity*$product->unit_price}}</td>
                        </tr>
                        @endforeach
                    </tbody>
                    <tfoot>
                        <tr>
                            <th colspan="3">Total:</th>
                            <td>{{$sum}}</td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
        @endforeach
        @endforeach
    </main>
</body>

</html>

This is the pdf layout comparison is on: https://imgur.com/a/ySpSjJN

No headers and footers getting generated, and table headers are not repeated when table is broken across pages

I have not changed anything in the code, I have no idea why it suddenly broke and how can i fix it? I tried multiple css properties, routes, and other stuff and had no luck with any.

I'm open to switching libraries. Though my pdf may be large (currently 3000 pages roughly but may grow or shrink), dompdf simply doesnt work, tcpdf has unclear documentation, and mpdf is too slow.

Thank you

0 likes
1 reply
frapto's avatar
frapto
OP
Best Answer
Level 2

It turned out I needed to use the qt patched version rather than the normal one.

Please or to participate in this conversation.