Skip to content

Instantly share code, notes, and snippets.

@vermilion1
Last active December 28, 2015 10:09
Show Gist options
  • Save vermilion1/7484817 to your computer and use it in GitHub Desktop.
Save vermilion1/7484817 to your computer and use it in GitHub Desktop.
Print image
var makePrint = function (win, src) {
if (!src) {
win = window.open();
src = win;
}
var add = function () {
var img = win.document.createElement('img');
var style = win.document.createElement('style');
style.setAttribute('media', 'all');
style.innerHTML = '@page{size:auto;margin:0;}body{border:0;background:#fff;margin:0;padding:0;}img{max-width:100%;max-height:100%;margin:0;display:block;}';
win.document.head.appendChild(style);
win.document.body.appendChild(img);
img.onload = function () {
win.print();
win.close();
};
img.src = src;
};
try {
add();
}
catch (e) {
win.onload = add;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment