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

echosky's avatar

Cannot show image on header spatie/browsershot

Cannot show image on header browsershot library

use VerumConsilium\Browsershot\Facades\PDF;

Route::get('/test', function () {
    $bodyHtml = View::make('test.snap', compact('data'))->render();
    $headerHtml = View::make('test._header')->render();;

    $footerHtml = '<br><br>
        <h1 style="font-size: 10px;color: #999; margin: 15px 40px; clear:both; position: relative; top: 20px;">
      Page <span class="pageNumber"></span> of <span class="totalPages"></span>
    </h1>';

    /**
     * Browsershot
     */
    return PDF::loadHtml($bodyHtml)
        ->showBackground()
        ->showBrowserHeaderAndFooter()
        ->headerHtml($headerHtml)
        ->footerHtml($footerHtml)
        ->margins(30, 10, 30, 10)
        ->format('A4')
        ->inline();
});

_header.blade.php

<div style="font-size:14px;">Header </div>

<img src="https://onlinejpgtools.com/images/examples-onlinejpgtools/smile.jpg" alt="test" srcset="test">
<img src="{{ public_path('/images/phpt-logo.jpg') }}" alt="test">
1 like
9 replies
Sinnbeck's avatar

Did you try adding this?

->waitUntilNetworkIdle()
echosky's avatar

Now, I try use package spatie/browsershot and try add

->waitUntilNetworkIdle()

But, header not show anything

  return Browsershot::html($bodyHtml)
        ->waitUntilNetworkIdle()
        ->showBrowserHeaderAndFooter(true)
        ->headerHtml($headerHtml)
        ->footerHtml($footerHtml)
        ->format('A4')
        ->save(public_path('images/test/') . date('s') . '.pdf');

MalcolmKnott's avatar

Did you find a solution to this? I also can't generate a PDF that contains an image using the html() method, only works when generating from a url.

Edit:: It was because I wasn't using the full url in the image path.

.certificate { background-image: url({{ url('/very-blank-cert.jpg') }}); }
1 like
lumiform's avatar

I have the same problem. No images are loaded by URL. And

->waitUntilNetworkIdle()

Does not help.

Davids73's avatar

after a couple of days of beating my face, I realized that: headerHtml and footerHtml accepts only plain text, in chrome headless header and footer don't accepts link to externals resources, for that the src attribute of img tag don't work (I tried also encode img to base64). They need dedicated margin in style and they not load tailwind. hope this is useful to someone

2 likes
danielcoimbra's avatar

I found a solution (tested with spatie/browsershot).

We can use a .svg image file (copy the svg/xml code in your header/footer views)...

Or if you can't use .svg, just base64 encode a .png file (so, using the image inline, without the extra request to the images, it works). More details here:

https://github.com/puppeteer/puppeteer/issues/2604

4 likes

Please or to participate in this conversation.