Skip to content

Instantly share code, notes, and snippets.

@kodie
Last active June 6, 2016 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kodie/3f016e1a6f325b0d60f6 to your computer and use it in GitHub Desktop.
Save kodie/3f016e1a6f325b0d60f6 to your computer and use it in GitHub Desktop.
Print just an image from a page.
function printImage(image, title) {
var winContent = "<html><head><title>" + title + "</title><script>function step1(){\n" +
"setTimeout('step2()', 10);}\n" +
"function step2(){window.print();window.close()}\n" +
"</scri" + "pt></head><body onload='step1()'>\n" +
"<img src='" + image + "' /></body></html>";
var win = window.open('about:blank', '_new');
win.document.open();
win.document.write(winContent);
win.document.close();
}
printImage('path/to/image.png', 'Title for top of page');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment