我需要创建带有动态字段的 PDF。有时文本可以是 2 行,有时可以是 3 行。如何在不硬编码静态 Y 坐标的情况下直接在第一个动态文本之后插入另一个元素?
这是我的代码:
const pdf = new window.jspdf.jsPDF();
pdf.text(`DYNAMIC TEXT THAT CAN HAVE 1 TO X LINES`, 5, 10);
//The next text which must be under the first, but without hardcode Y:20, because sometime, this is too much
pdf.text(`DYNAMIC TEXT THAT CAN HAVE 1 TO X LINES`, 5, 20);
我正在使用 CDN 链接中的 jsPDF 2.3.0
谢谢你!
我对 jsPDF 有同样的问题,但找不到任何答案。 如果您可以更改您正在使用的库,我发现this post建议尝试使用 pdfMake。也许它可以帮助你...
didDrawPage: function (data) {
doc.text(
"first title,
data.settings.margin.left,
22
);
doc.text("Second title", data.settings.margin.top, 50);
}
这个调整对我有用
我也遇到了同样的问题,找不到任何官方解决方案,所以使用了其他方法。这是我的解决方法。我将文本分割成我需要的大小,然后得到它的高度。希望有帮助
const start = 20;
let nextY = 60;
const textOne= `This might change: ${myTextOne}`;
nextY = addText(doc, textOne, nextY);
const textTwo = `This might change: ${myTextTwo}`;
nextY = addText(doc, textTwo, nextY);
function addText(doc, text, nextY) {
const locationsText = text;
doc.text(locationsText, start, nextY, { maxWidth: 180 });
nextY += roundUp(doc.getTextDimensions(doc.splitTextToSize(locationsText,
180)).h) + 5;
return nextY;
}