chrome-php / chrome

Instrument headless chrome/chromium instances from PHP
MIT License
2.21k stars 270 forks source link

PDF images (assets) are not loading #562

Open Chrynn opened 8 months ago

Chrynn commented 8 months ago

Hello,

I used this package and after creating PDF the PDF is without images (could not find image icon) although in browser it's loading fine. Im using http://localhost:90/assets/image/test.png link to image <img> in setted html template to PDF. Probably will be something bad with path to files in headless mode or something with protocols. Im using MAC device.

When i set it to headless:false its showing error with (might be helpful)

Chrome process stopped before startup completed. Additional info: [4897:4897:1121/151359.976985:ERROR:ozone_platform_x11.cc(234)] Missing X server or $DISPLAY [4897:4897:1121/151359.976996:ERROR:env.cc(225)] The platform failed to initialize. Exiting.

enricodias commented 8 months ago

Are the images in the header or footer of the pdf? Check if #347 applies to your case.

Chrynn commented 8 months ago

Are the images in the header or footer of the pdf? Check if #347 applies to your case.

Images are not loading even in the div's in body

divinity76 commented 8 months ago

if you load the image with JavaScript like

d(
$page->evaluate('(function(){
    let xhr = new XMLHttpRequest();
    xhr.open("GET", "http://localhost:90/assets/image/test.png", false);
    xhr.send();
    return xhr.responseText;
})();')->getReturnValue()
);

do you get the image binary?

hallowacher commented 8 months ago

hello, don't know if related but i had the same issue and stumbled on this thread. unfortunately i had issues with headless and delay, so i don't know the error message. but i have to convert the image to base64 to get the image shown in the pdf. $image = file_get_contents($imagePath); $imageb64 = base64_encode($image);

<img src="data:image/png;base64, {{ imageb64 }}" class="d-block mx-lg-auto img-fluid" alt="No Pics" width="100" height="100"> replace "{{ imageb64 }}" with the string "base64_encode()" returns. Hope this helps.

Chrynn commented 7 months ago

hello, don't know if related but i had the same issue and stumbled on this thread. unfortunately i had issues with headless and delay, so i don't know the error message. but i have to convert the image to base64 to get the image shown in the pdf. $image = file_get_contents($imagePath); $imageb64 = base64_encode($image);

<img src="data:image/png;base64, {{ imageb64 }}" class="d-block mx-lg-auto img-fluid" alt="No Pics" width="100" height="100"> replace "{{ imageb64 }}" with the string "base64_encode()" returns. Hope this helps.

Thank you, it's sad it doesn't work in the native way (for example)

<img src="http://localhost:90/assets/image/random-image.png">

but this actually works.

<img src="data:image/png;base64,{{ base64_encode(file_get_contents('assets/image/random-image.png')) }}">
stale[bot] commented 3 months ago

This issue has been automatically marked as stale because there has been no recent activity. It will be closed after 30 days if no further activity occurs. Thank you for your contributions.