js 打印
// 打印图片
function printThisWindow(url) {
let iframe = document.createElement('IFRAME')
let doc = null
iframe.setAttribute('class', 'print-iframe')
iframe.setAttribute('style', 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;')
document.body.appendChild(iframe)
doc = iframe.contentWindow.document
doc.___imageLoad___ = function () {
iframe.contentWindow.print()
if (navigator.userAgent.indexOf('MSIE') > 0) {
document.body.removeChild(iframe)
}
}
doc.write('<div style="height: 100%;width: 100%;">' + `<img src="${url}" style="max-height:100%;max-width: 100%;" onload="___imageLoad___()"/>` + '</div>')
doc.close()
iframe.contentWindow.focus()
}
// 打印当前页面
function doPrint() {
bdhtml = window.document.body.innerHTML;
sprnstr = "<!--startprint-->"; //开始打印标识字符串有17个字符
eprnstr = "<!--endprint-->"; //结束打印标识字符串
prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 17);
prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));
window.document.body.innerHTML = prnhtml;
// print.portrait = false ;//横向打印
window.print(); //调用浏览器的打印功能打印指定区域
window.document.body.innerHTML = bdhtml; // 最后还原页面
}
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
评论已关闭