Skip to content

Instantly share code, notes, and snippets.

@zeshanshani
Created March 12, 2019 09:24
Show Gist options
  • Select an option

  • Save zeshanshani/de899d2b02414e7f092761d3d9ae6e3e to your computer and use it in GitHub Desktop.

Select an option

Save zeshanshani/de899d2b02414e7f092761d3d9ae6e3e to your computer and use it in GitHub Desktop.
This code prints an image on the page.
/**
* Image Sourec to Print
*
* Usage:
*
* imgPrint( $('img').attr('src') );
*
* @param {string} source source of the image
*/
function imgSourceToPrint( source ) {
return "<html><head><script>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>";
}
/**
* Image Print
* @param {string} source source of the image
*/
function imgPrint( source ) {
Pagelink = "about:blank";
var pwa = window.open( Pagelink, "_new" );
pwa.document.open();
pwa.document.write( imgSourceToPrint( source ) );
pwa.document.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment