github地址:https://github.com/parallax/jsPDF
直接引入到script似乎行不通
但是把这个链接直接打开却可以看到内容,然后把js内容复制到本地再引入。
有滚动条要先去掉,否则pdf中会显示一片黑色,但是呢去掉滚动条原本页面会变形,这时候给目标元素的父级一个opacity=0。不能display: none;不然导出的pdf会全黑
function exportHtml() {
var pdf =new jsPDF();
var name = 'name';
var target = document.getElementById('pdf');
html2canvas(document.querySelector('#pdf'), {
allowTaint: true,
}).then(function (canvas) {
var canvaWidth = canvas.width;
var canvaHeight = canvas.height;
var pageHeight = canvaWidth / 592.28 * 841.89;
var leftHeight = canvaHeight;
var position = 0;
var imgwidth = 595.28;
var imgheight = imgwidth / canvaWidth * canvaHeight;
var pageData = canvas.toDataURL('image/jpeg', 1.0);
var pdf = new jsPDF('', 'pt', 'a4'