Shebas Khan Shebas Khan - 1 year ago 66
HTML Question

Print button to print a attached image

I would like to add a print button in my webpage. upon clicking the print icon, it should automatically start print the attached image

How can i achieve this?

Answer Source
  1. Open new window using
  2. Write to it the img tag.
  3. Once the image was loaded, print the document using window.print().
  4. Close the window using window.close()

You can do this with code like this:

function printImg(url) {
  var win ='');
  win.document.write('<img src="' + url + '" onload="window.print();window.close()" />');
<img src="" />
<button onclick="printImg('')">Print</button>,js