html2canvas(document.getElementById("_container")).then(function (canvas) {
//打印
//window.print(canvas);
const contentWidth = canvas.width;
const contentHeight = canvas.height;
// 将canvas转为base64图片
const pageData = canvas.toDataURL('image/jpeg', 1.0);
// 设置pdf的尺寸,pdf要使用pt单位 已知 1pt/1px = 0.75 pt = (px/scale)* 0.75
// 2为上面的scale 缩放了2倍
const pdfX = (contentWidth + 10) / 2 * 0.75;
const pdfY = (contentHeight + 500) / 2 * 0.75 ;// 500为底部留白
// 设置内容图片的尺寸,img是pt单位
const imgX = pdfX;
const imgY = (contentHeight / 2 * 0.75); //内容图片这里不需要留白的距离
// 初始化jspdf 第一个参数方向:默认''时为纵向,
// 第二个参数设置pdf内容图片使用的长度单位为pt,
// 第三个参数为PDF的大小,单位是pt
const PDF = new jsPDF('', 'pt', [pdfX, pdfY]);
// 将内容图片添加到pdf中,因为内容宽高和pdf宽高一样,就只需要一页,位置就是 0,0
PDF.addImage(pageData, 'jpeg', 0, 0, imgX, imgY);
PDF.save('download.pdf');
});
html生成pdf
最新推荐文章于 2023-08-11 15:04:16 发布