I have a PHP loop that does the following:
- Log in to a web page via CURL
- Capture and internal page that requires a login
- Save the HTML of the page to a local file
- Using WKHTMLTOPDF, render the page as a PDF
The issue I’m having is that every once in a while (maybe ~30% of the time), the images won’t render in the PDF. If I open up one of the saved HTML files, I’ll find that I need to go in and manually refresh the page for the images to show up.
Any ideas on how to pragmatically ensure the images load? Things I’ve tried:
sleep(n)between each and every line- Adding
--javascript-delay 30000to my WKHTMLTOPDF call to ensure it has ample time to load any images.
#1 made it considerably worse, and #2 did nothing at all.
Thanks!
Between step 3 & 4 of your example you might want to consider parsing the HTML file for all image links and downloading them individually using curl, saving them locally as well and then updating the links in the saved HTML file to point to the new local image resources instead of the remote ones.
This should drastically improve the load time of images when rendering the HTML as a PDF.