欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

html表格以pdf格式导出到本地

发布时间:2024/1/17 编程问答 61 豆豆
生活随笔 收集整理的这篇文章主要介绍了 html表格以pdf格式导出到本地 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

引入插件 jsPdf.js 以及 html2canvas.js,具体文件可上github找,实测可行
html2canvas(document.getElementById("downBox")).then(function (canvas) {

var imgData = canvas.toDataURL();
var arrDPI = js_getDPI();//获取显示器dpi
var dpiX = 96;
var dpiY = 96;
if (arrDPI.length > 0) {
dpiX = arrDPI[0];
dpiY = arrDPI[1];
}
var doc = new jsPDF('l', 'in', [(canvas.width + 10) / dpiX, (canvas.height + 10) / dpiY]);

doc.addImage(imgData, 'png', 7/dpiX,5/dpiY);

var pdfName = "12134";
doc.save(pdfName + ".pdf");
});

 

 

//获取显示器的dpi function js_getDPI() { var arrDPI = new Array(); if (window.screen.deviceXDPI != undefined) { arrDPI[0] = window.screen.deviceXDPI; arrDPI[1] = window.screen.deviceYDPI; } else { var tmpNode = document.createElement("DIV"); tmpNode.style.cssText = "width:1in;height:1in;position:absolute;left:0px;top:0px;z-index:99;visibility:hidden"; document.body.appendChild(tmpNode); arrDPI[0] = parseInt(tmpNode.offsetWidth); arrDPI[1] = parseInt(tmpNode.offsetHeight); tmpNode.parentNode.removeChild(tmpNode); } return arrDPI; }

转载于:https://www.cnblogs.com/lzdbj/p/10450047.html

总结

以上是生活随笔为你收集整理的html表格以pdf格式导出到本地的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。