Javascript print() method not printing Images ? then no problem i have a solution

If you are using window.Print() method of javascript to print the page but it prints only letters and does not print images of the page then use following solution for this problem

  • firstly try to use absolute url path in images src attribute like http://something.com/imger.jpg then it will be render correctly

  • secondly copy and paste below code in script tag 

function printContent(id){
var str=document.getElementById(id).innerHTML
newwin=window.open('','printwin','left=0,top=0,width=1,height=1,menubar=no')
newwin.document.write('<HTML>\n<HEAD>\n')
newwin.document.write('\n')
newwin.document.write('<script>\n')
newwin.document.write('function chkstate(){\n')
newwin.document.write('if(document.readyState=="complete"){\n')
newwin.document.write('window.close()\n')
newwin.document.write('}\n')
newwin.document.write('else{\n')
newwin.document.write('setTimeout("chkstate()",2000)\n')
newwin.document.write('}\n')
newwin.document.write('}\n')
newwin.document.write('function print_win(){\n')
newwin.document.write('window.print();\n')
newwin.document.write('chkstate();\n')
newwin.document.write('}\n')
newwin.document.write('<\/script>\n')
newwin.document.write('</HEAD>\n')
newwin.document.write('<BODY onload="print_win()">\n')
newwin.document.write(str)
newwin.document.write('</BODY>\n')
newwin.document.write('</HTML>\n')
newwin.document.close()
}

Call the above function in your buttons onclick event and send the div id to print

you will see in the print output text + images

Happy Coding!!!!!!!!!!!!!!:) 

Comments

Popular posts from this blog

Implement Logging in CSV file using Nlog in .net core MVC application- part 2

Implement Nlog in .Net core MVC application part 1

Angular User Session Timeout example step by step

Disable backspace key using Jquery

Devexpress Datebox date formatting in angular 6 with example