Last active
December 28, 2015 10:09
-
-
Save vermilion1/7484817 to your computer and use it in GitHub Desktop.
Print image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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