问题
I have some code to create a pop-up write some html and print. Google Chrome is not showing images in the print preview, but other browsers are working fine. The file Logo_big.png
is missing in my case. How can I get it to work in Chrome also?
My code:
var newWindow = window.open('', '', 'width=100, height=100'),
document = newWindow.document.open(),
pageContent =
'<!DOCTYPE html>' +
'<html>' +
'<head>' +
'<meta charset="utf-8" />' +
'<title>Inventory</title>' +
'<style type="text/css">body {-webkit-print-color-adjust: exact; font-family: Arial; }</style>' +
'</head>' +
'<body><div><div style="width:33.33%; float:left;"><img src="img/Logo_big.png"/></body></html>';
document.write(pageContent);
document.close();
newWindow.moveTo(0, 0);
newWindow.resizeTo(screen.width, screen.height);
newWindow.print();
newWindow.close();
回答1:
The issue here is javascript not giving the DOM enough (any) time to load / render the image once it has the src
set.
You need to allow time for the browser to load / render (from cache or otherwise) the image, you can do so by setting a setTimeout
to delay the printing from the content being set.
An example for this specific question would be:
var newWindow = window.open('', '', 'width=100, height=100'),
document = newWindow.document.open(),
pageContent =
'<!DOCTYPE html>' +
'<html>' +
'<head>' +
'<meta charset="utf-8" />' +
'<title>Inventory</title>' +
'<style type="text/css">body {-webkit-print-color-adjust: exact; font-family: Arial; }</style>' +
'</head>' +
'<body><div><div style="width:33.33%; float:left;"><img src="img/Logo_big.png"/></body></html>';
document.write(pageContent);
document.close();
newWindow.moveTo(0, 0);
newWindow.resizeTo(screen.width, screen.height);
setTimeout(function() {
newWindow.print();
newWindow.close();
}, 250);
The lower you set the setTimeout
, the less time the browser will have to load the image so you will need to adjust this and account for slower internet connections / browsers where applicable.
回答2:
After a deep research I found a solution for showing the images and background in google chrome print preview:
function PopUp(data) {
var mywindow = window.open('','','left=0,top=0,width=950,height=600,toolbar=0,scrollbars=0,status=0,addressbar=0');
var is_chrome = Boolean(mywindow.chrome);
mywindow.document.write(data);
mywindow.document.close(); // necessary for IE >= 10 and necessary before onload for chrome
if (is_chrome) {
mywindow.onload = function() { // wait until all resources loaded
mywindow.focus(); // necessary for IE >= 10
mywindow.print(); // change window to mywindow
mywindow.close();// change window to mywindow
};
}
else {
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10
mywindow.print();
mywindow.close();
}
return true;
}
Thanks to Anand Deep Singh post that helped with a part of the code.
回答3:
You can add the following in your css for media print:
img {
-webkit-print-color-adjust: exact;
}
回答4:
You need to put delay before print. There is a native bug in chrome. Code would as under :-
function PrintDiv(data) {
var mywindow = window.open();
var is_chrome = Boolean(mywindow.chrome);
mywindow.document.write(data);
if (is_chrome) {
setTimeout(function () { // wait until all resources loaded
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10
mywindow.print(); // change window to winPrint
mywindow.close();// change window to winPrint
}, 250);
}
else {
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10
mywindow.print();
mywindow.close();
}
return true;
}
回答5:
If you are using jQuery you can use the "on load" event. This will wait until the entire page is ready (including Images). This should be useful if you have many images. Note You will need to include the jQuery source in your html.
Include the following in your html.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
Your javaScript will now be.
var newWindow = window.open('', '', 'width=100, height=100'),
document = newWindow.document.open(),
pageContent =
'<!DOCTYPE html>' +
'<html>' +
'<head>' +
'<meta charset="utf-8" />' +
'<title>Inventory</title>' +
'<style type="text/css">body {-webkit-print-color-adjust: exact; font-family: Arial; }</style>' +
'</head>' +
'<body><div><div style="width:33.33%; float:left;"><img src="img/Logo_big.png"/></body></html>';
document.write(pageContent);
document.close();
newWindow.moveTo(0, 0);
newWindow.resizeTo(screen.width, screen.height);
$(newWindow.window).on("load", function () {
newWindow.print();
newWindow.close();
});
Hope this helps.
回答6:
It looks like you have two opening 'div' tags that are never closed.
回答7:
var newWindow = window.open('', '', 'width=100, height=100'),
pageContent =
'<!DOCTYPE html>' +
'<html>' +
'<head>' +
'<meta charset="utf-8" />' +
'<title>Inventory</title>' +
'<style type="text/css">body {-webkit-print-color-adjust: exact; font-family: Arial; } img{display:visible}</style>' +
'</head>' +
'<body><img src="img/Logo_big.png"/></body></html>';
newWindow.document.write(pageContent);
document.close();
newWindow.moveTo(0, 0);
newWindow.resizeTo(screen.width, screen.height);
newWindow.print();
newWindow.close();
I hope that code works fine
Note that if image is not displayed so you need to specify absolute image url
Good luck :)
回答8:
I used the solution given by Jesus Flores
BUT it doesn't work when you cancel the print and then do it again.
So i used the setTimeout() function to tackle that situation,
function PopUp(data) {
var mywindow = window.open('','','left=0,top=0,width=950,height=600,toolbar=0,scrollbars=0,status=0,addressbar=0');
var is_chrome = Boolean(mywindow.chrome);
var isPrinting = false;
mywindow.document.write(data);
mywindow.document.close(); // necessary for IE >= 10 and necessary before onload for chrome
if (is_chrome) {
mywindow.onload = function() { // wait until all resources loaded
isPrinting = true;
mywindow.focus(); // necessary for IE >= 10
mywindow.print(); // change window to mywindow
mywindow.close();// change window to mywindow
isPrinting = false;
};
setTimeout(function () { if(!isPrinting){mywindow.print();mywindow.close();} }, 300);
}
else {
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10
mywindow.print();
mywindow.close();
}
return true;
}
This might be not the best way to do that so all kind of suggestions are welcome!
来源:https://stackoverflow.com/questions/31725373/google-chrome-not-showing-image-in-print-preview