Window.Print() Prints Blank Page

Chrome print blank page

It looks like it's attempting to print before the <img> has loaded, move the call to print inside an event handler for the load event of window by opening the link as a data URI or Blob, for example

var code = '\
<html>\
<head>\
<title></title>\
<script>\
function printFunction() {\
window.focus();\
window.print();\
window.close();\
}\
window.addEventListener(\'load\', printFunction);\
</script>\
</head>\
<body><img src="'+src+'" width="300"></body>\
</html>';

window.open('data:text/html,' + code, '_blank', 'width=600,height=600');

Don't forget you may need to HTML encode the tags in code


You could probably just listen for load on the <img> instead, but if you ever do anything more complicated than tring to print a single image you may find it breaks again in future

doc.write('<img onload="printFunction();" src="'+src+'" width="300">');

Where printFunction is the print function for all browsers

how to avoid extra blank page at end while printing?

You could maybe add

.print:last-child {
page-break-after: auto;
}

so the last print element will not get the extra page break.

Do note that the :last-child selector is not supported in IE8, if you're targetting that wretch of a browser.

element print preview is blank and prints blank pages in Google Chrome

Since the <script></script> tags are breaking the code, try using this line :

mywindow.document.write('<scr'+'ipt type="text/javascript">$(window).load(function() { window.print(); window.close(); });</scr'+'ipt>')

And without jQuery :

mywindow.document.write('<scr'+'ipt type="text/javascript">function PrintPage() { window.print(); window.close(); } window.onload = PrintPage;</scr'+'ipt>');

Windows.print() creating a blank page before pages

Although I didn't solve the problem, I fixed it by having the page open in a new tab so I can avoid a redirect altogether.

windows.print results in empty page

To anyone having the same problem: I couldn't figure out what was causing it, but I could get it done using the window.frame approach elaborated in this answer.



Related Topics



Leave a reply



Submit