Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using javascript to print images

Tags:

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?

like image 520
andrew Avatar asked May 25 '10 22:05

andrew


People also ask

Can JavaScript display images?

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.

Can you print in JavaScript?

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.

How do I print an image in HTML?

You can print a web page by clicking the Print menu of browser or pressing the shortcut Ctrl+P (or command+P on macOS).


2 Answers

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.

like image 127
ricardo_escovar Avatar answered Sep 28 '22 07:09

ricardo_escovar


popup = window.open(); popup.document.write("imagehtml"); popup.focus(); //required for IE popup.print(); 
like image 31
Javier Parra Avatar answered Sep 28 '22 07:09

Javier Parra