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

bionary's avatar

Browsershot PDF not Saving Images

I'm at a loss because my pdf creator (uses Spatie/Browsershot) on my server stopped showing images recently without any changes on my part.

The strange part is if I return the exact page to the browser without Browsershot...all images are intact as expected.

If I run the same exact page through Browsershot, the images are not saving to the resulting PDF.

I even put a timestamp on the bottom of the html page/pdf just to make sure I wasn't losing my mind.... very weird.

	//testing to screen (shows images)
	return view($viewName, compact('article', 'tempDir'));//TEST VIEW IN BROWSER
	//Saving as PDF using Browsershot (images are missing)
				Browsershot
		::html(view($viewName, compact('article', 'tempDir')))
        ->addChromiumArguments([
            'disable-application-cache',
            'incognito',
        ])
		->waitUntilNetworkIdle()
		->save(Storage::disk('public assets')->path($basename));

Any ideas?

0 likes
9 replies
TheoR's avatar

I had a similar issue but with a chart. tuned out, it was taking too long for the chart to render. I solved it by putting a two second delay(->setDelay(2000)) on the Browsershot like this:

        return response()->stream(function () use ($html) {
            echo Browsershot::html($html)
                ->emulateMedia('screen')
                ->showBackground()
                ->margins(10, 10, 10, 10)
                ->format('A4')
//                ->landscape()
                ->setDelay(2000)
                ->pdf();
//                ->bodyHtml();
        }, 200, ['Content-Type' => 'application/pdf',
            'Content-Disposition' => $content_type,
            'filename' => 'CADResponseReport.pdf'
        ]);

Hope that helps!

bionary's avatar

@TheoR Thanks I'll give that shot. I did think, however, that ->waitUntilNetworkIdle() would have done the trick, but I don't know for sure. The no-show image issue is inconsistent which makes it tricky to diagnose.... Several hours after I wrote this post it all worked fine again without me changing one thing.

I'm still baffled.

bionary's avatar

As I suspected, the ->setDelay(2000) does not fix the issue.

anuzpandey's avatar

Did you manage to make this work on a VPS? I am having the permission issue with this package in a Digital Ocean droplet with Ubuntu 22 installed.

bionary's avatar

@anuzpandey NO, I got so fed up with browsershot I considered it no longer a viable option for production. I don't care what people say, but in my experience browsershot or really puppeteer/headless chrome is a mess.

The problem is the inconsistent results with no error feedback. How can a pdf get generated 100 times in a row with all images intact and then the next 100 times there are missing images 20% of the time? This kind of problem is very hard to diagnose even if I had a better idea what was going on...but from somebody relying on a stack {headless-chrome | puppeteer | browsershot} ? Where in the heck is the problem? I don't know and I got tired of trying to figure it out.

My solution: I refactored all of my code to use image magick, or actually imagick extension for php. It was a lot more work but now I have an incredibly robust production system that never fails. I produce tens of thousands of images via cron jobs and imagic is awesome at this. Running for 1+ year without a single hiccup. You just have to watch your server ram...image magick hogs a massive amount of resources.

leemgu's avatar

@bionary Hey Bionary, we are just about to embark on something similar and I am struggling to get Browsershot to load images into the PDF. Do you know of any wrappers / libraries for imagick or can you provide any pointers on how you are achieving this?

bionary's avatar

@leemgu First off, I'm a part time coder and not a computer scientist. Command line is not my forte and while I "get it done" I rarely understand the full picture. Here is what I do know: Image Magick is a command line application that has nothing to do with php. You have to use the command line to install it. In my case I used brew on my mac and apt-get on the linux server

The wrapper to make php interact with Image Magick is called imagick and for some stupid reason it's only available via the pecl library. So instead of simply adding it to your composer file you now have to jump through a bunch of annoying hoops with pecl. Like I said stupid...and super annoying.

I'd give you a link but I can guarantee that something will change (like it always does) preventing any instructions I post here from working a week after I post this or as soon as macos updates.

The above, however should give you an overview of what needs to happen:

  • Install Image Magic
  • install iMagick via Pecl....

Since imagick will be installed via pecl you will be able to reference imagick easily like: $image = new \Imagick($imagePath);

leemgu's avatar

@bionary Thank you, appreciate the feedback. I managed to get Browsershot up and running in the end.

Please or to participate in this conversation.