Javascript 如何在使用JSPDF新html API从html生成pdf时提供宽度、高度、x和y坐标
我一直在使用JSPDF根据一些html生成pdf文档。在前面使用jspdf fromHTMLAPI时,我们可以给出这样的页边距Javascript 如何在使用JSPDF新html API从html生成pdf时提供宽度、高度、x和y坐标,javascript,pdf-generation,jspdf,jspdf-autotable,pdf-to-html,Javascript,Pdf Generation,Jspdf,Jspdf Autotable,Pdf To Html,我一直在使用JSPDF根据一些html生成pdf文档。在前面使用jspdf fromHTMLAPI时,我们可以给出这样的页边距 var margins2 = { top: 415, bottom: 10, left: 55, width: 300 }; doc.fromHTML(reactListContent, margins2.left, margins2.top, { 'width': margins2.widt
var margins2 = {
top: 415,
bottom: 10,
left: 55,
width: 300
};
doc.fromHTML(reactListContent, margins2.left, margins2.top, {
'width': margins2.width,
'elementHandlers': specialElementHandlers
}, margins2);
但是,在新的.html API中,如何提供边距、宽度和高度。
新的API就像
var pdf = new jsPDF('p', 'pt', 'letter');
pdf.html(document.getElementById('html'), {
callback: function (pdf) {
console.log("how to get margins");
}
});
如果查看,则具有x和y偏移的选项
opt: {
filename: 'file.pdf',
margin: [0, 0, 0, 0],
enableLinks: true,
x: 0,
y: 0,
html2canvas: {},
jsPDF: {}
}
因此,您可以如下设置x和y坐标:
pdf.html(document.getElementById('html'), {
x: 36,
y: 36,
callback: function () {
// pdf.save('test.pdf');
window.open(pdf.output('bloburl')); // to debug
}
});
不幸的是,我不能通过修改边距[0,0,0,0]
来做同样的事情。看起来是的。所以简单的答案是,还没有
一种解决方法是按边际计算html2canvas的规模:
let pdf = new jsPDF('p', 'pt', 'a4');
let margin = 36; // narrow margin - 12.7 mm
let srcwidth = document.getElementById('html').scrollWidth;
let scale = (595.28 - margin * 2) / srcwidth; // a4 pageSize 595.28
pdf.html(document.getElementById('html'), {
backgroundColor: 'lightyellow',
html2canvas: {
scale: scale // default is window.devicePixelRatio,
},
x: margin,
y: margin,
callback: function () {
window.open(pdf.output('bloburl'));
}
});