I was cracking my head against the wall wondering why the image in footer is not shown. what i was doing was:
page.paperSize = {
format: 'A4',
orientation: 'landscape',
margin: {top: '0.5cm', right: '1cm', bottom: '0.5cm', left: '1cm'},
header: {
height: '2.5cm',
contents: phantom.callback(function(pageNum, numPages){
return '<div style="height:80px;border-bottom:1pt solid black">'+ header_html +'</div>';
})
},
footer: {
height: '3.5cm',
contents: phantom.callback(function(pageNum, numPages){
return '<div style="height:100px;">' + footer_html + '<div style="text-align:center;height:1cm;line-height:1cm;font-size:8pt;float:left;width:100%;">'+ pageNum +'/'+ numPages +'</div></div>';
})
}
};
Where footer_html was read from file and was:
<div style="float:left;"><img src="file:///documents/long/path/27f/ec5ba5091414e88aec5b9622a2d13/download.png" style="vertical-align:top;border:0px none;background-color:transparent;"></div>
And the image just did not display. It was not the problem of paths - path was correct. And nothing worked, until i threw same image into content too. Then it was rendered in both content and footer.
My fix was that i wrapped the image in content with div with display:none. Then i got rid of image in content and image in footer was rendered too.
No idea if its useful bit of information, but i was rendering page from local html, not over the web.
I had a similar issue to this.
I was using Handlebars to generate the HTML however the images only loaded if I also included it in the content directly. This didn't work at all for me for images with a dynamic location.
My solution was to wrap the render inside of a time-out so that the HTML images had a chance to render properly.
page.onLoadFinished = function () {
console.log('Doing something...');
// Handlebars stuff
page.evaluate(function (data) {
var template = Handlebars.templates['myTemplate'];
var result = template(data);
document.getElementById('outputDiv').innerHTML = result;
}, data);
// Timeout causes this to execute after the DOM is finished
setTimeout(function () {
page.render('../myPDF.pdf');
phantom.exit();
}, 0);
};
page.open('file:///path//to//index.html', function (status) {});
The issue, as we later found out is, that both header and footer are separate html documents when it comes to phantomjs. So they need to include their own css and js imports and so on. Same with images and all.
I am also having the exact same issue. And fixed it by putting the image in the body of my template and using css to not display it, thank you @OdifYltsaeb for the temporary solution!
I also ran into this issue. I can confirm it's still relevant in 2.1.
Another workaround might be to use a data URI.
I'm running into this issue now as well. For those who placed their image in the content with display: none, could you share how exactly you did this? I'm not getting it to work for me. Thanks.
Never mind, I just got it working. For me, I had to use the exact same path as that used in the content page. I assume this is because phantomjs doesn't load resources referenced in the header/footer documents, but it does for the content?
This might well be a bug in PhantomJS (or, more likely, in Webkit, which would need to get reported directly to the Webkit developers) but it is unclear exactly what the problem is and what are the conditions that trigger it. The bug tracker is not for trying to work out whether something even is a bug in the first place; it is only for developing fixes to well-characterized bugs.
Everyone suffering this problem, please do the following:
You are welcome to discuss this problem further on any of
phantomjs tag)but I am going to close this bug report now, since we (the developers) cannot take any action on it yet. Please feel free to reopen it if you determine that this really is a bug in PhantomJS 2.1 proper and you can provide a self-contained test case.
Just wanted to leave some notes to those dealing with this issue:
<img src="data:image/png;base64,..."/> is a valid solution, and works. However, you must still load the image into the main content to have it "cached", otherwise it still won't work.https and your SSL certificates are not configured, the image will also not load in the footer or header.
Most helpful comment
Just wanted to leave some notes to those dealing with this issue:
<img src="data:image/png;base64,..."/>is a valid solution, and works. However, you must still load the image into the main content to have it "cached", otherwise it still won't work.httpsand your SSL certificates are not configured, the image will also not load in the footer or header.