// window.print()
// 获得要打印元素的内容
// 保存当前页面的整个html
// 把当前页面替换为打印内容
// 执行打印操作
// 还原当前页面
// 缺点:打印后页面绑定的事件失效
let screenFullHtml = document.body.innerHTML
let printHtml=document.getElementById('printCont').innerHTML;
document.body.innerHTML = printHtml;
window.print()
document.body.innerHTML = screenFullHtml;
// 获得要打印元素的内容
// 缺点: 会导致页面样式丢失
let printHtml=document.getElementById('printCont').innerHTML;
let newWindow= window.open("",'newwindow');
newWindow.document.body.innerHTML = printHtml;
newWindow.print();
// 借助插件实现
html2canvas(document.getElementById('printCont')).then(canvas=>{
var newstr = document.getElementById("printCont").innerHTML;
let printWindow = window.open("",'printWindow');
let oImg = new Image();
oImg.src = canvas.toDataURL(); // 导出图片
let contentWidth = canvas.width;
let contentHeight = canvas.height;
console.log(contentWidth,contentHeight)
// oImg.width = 595.28;
// oImg.height = 555.28/contentWidth * contentHeight;
oImg.width = contentWidth;
oImg.height = contentHeight;
printWindow.document.body.appendChild(oImg);
oImg.onload = function (){
printWindow.print();
printWindow.close()
}
})
生成pdf 下载测试(未完成)
html2canvas(document.getElementById('printCont')).then(canvas=>{
// canvas为转换后的Canvas对象
let img = new Image();
img.src = canvas.toDataURL(); // 导出图片
// document.body.appendChild(img); // 将生成的图片添加到body
$("img").attr("src",img.src);
var link = document.createElement("a");
link.setAttribute("download","下载");
link.href = oImg.src;
link.click();
})