1.直接使用window.print() 打印
print(id){
let html = document.getElementById(id) // id要打印的html ID
let newHtml = html.innerHTML
let oldHtml = document.body.innerHTML
document.body.innerHTML = newHtml
if(!!window.ActiveXObject || 'ActiveXObject' in window) {
document.body.insertAdjacentHTML('beforeEnd',<object id="WebBrowser" classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height="0" width="0"></object>`)
document.all.WebBrowser.ExecWB(7,1) // 打印预览
// document.all.WebBrowser.ExecWB(8,1) // 页面设置
// document.all.WebBrowser.ExecWB(6,1) // 打印
const hkeyPath = 'HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\PageSetup\\'
try{
/* global ActiveXObject */
let RegWsh = new ActiveXObject('WScript.Shell')
RegWsh.RegWrite(hkeyPath+'header','')
RegWsh.RegWrite(hkeyPath+'footer','')
} catch (e){ }
}else{
document.getElementsByTagName('body')[0].style.zoom = 0.70
window.print()
}
window.location.reload()
document.body.innerHTML = oldContent
return false
}
2.设置分页
在需要分页的地方设置page-break-after: always
3.打印样式
@media print{
@page{
size: A4;// 定义为a4纸
margin:0 0 0 50px;//页面的边距
}
}
网友评论