I would like to know if it's possible to use javascript to open a popup window containing an image, and at the same time have the print dialog show. Once someone clicks on print, the popup closes.
Is this easily attainable?
JavaScript is known as the web development language. By using JavaScript, we can make our web page attractive by inserting images into it. By default, we use the <img> tag in HTML to display images. In the <img> tag, we have a method known as src , which helps get the source of the image that we gave to display.
JavaScript PrintJavaScript does not have any print object or print methods. You cannot access output devices from JavaScript. The only exception is that you can call the window.print() method in the browser to print the content of the current window.
You can print a web page by clicking the Print menu of browser or pressing the shortcut Ctrl+P (or command+P on macOS).
Another great solution!! All credit goes to Codescratcher
<script> function ImagetoPrint(source) { return "<html><head><scri"+"pt>function step1(){\n" + "setTimeout('step2()', 10);}\n" + "function step2(){window.print();window.close()}\n" + "</scri" + "pt></head><body onload='step1()'>\n" + "<img src='" + source + "' /></body></html>"; } function PrintImage(source) { var Pagelink = "about:blank"; var pwa = window.open(Pagelink, "_new"); pwa.document.open(); pwa.document.write(ImagetoPrint(source)); pwa.document.close(); } </script> <a href="#" onclick="PrintImage('YOUR_IMAGE_PATH_HERE.JPG'); return false;">PRINT</a>
See the full example here.
popup = window.open(); popup.document.write("imagehtml"); popup.focus(); //required for IE popup.print();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With