在我的 React 应用程序(客户端)中,我有一个按钮可以使用当前的 html 内容与 jsPDF 创建 pdf。我使用 .html() 而不是 .fromHTML() 因为我需要在 pdf 转换中保留 html 样式。 真正的问题是当我使用html()时,我丢失了页面的行/列结构的大量内容。所以,我需要将我的 html 内容缩放到 pdf 中,但我不知道该怎么做。
我的代码:
let doc = new jsPDF('p', 'pt', 'a4')
let source = document.getElementById('pdfContent')
doc.html( source, {
callback: function (doc) {
doc.save();
}
});
最佳答案
“丢失大量内容”如果您的意思是您的内容无法放入 a4 页面,那么您可以使页面更大,例如使用 a3 而不是 a4,或者您可以使用 scale 缩小内容
在 html2canvas
中。可以根据您的页面格式和元素大小计算比例。
let doc = new jsPDF('p', 'pt', 'a4')
doc.html(document.getElementById('pdfContent'), {
html2canvas: {
scale: [yourScaleHere],
},
x: [margin],
y: [margin],
callback: function (doc) {
window.open(doc.output('bloburl'));
}
});
如果您不需要标准页面大小,您可以根据您的内容设置页面。例如let pdf = new jsPDF('p', 'pt', [612, 1200]);
关于javascript - 当我使用 jsPDF 将 html 转换为 pdf 时丢失内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60233354/