jspdf在第一页上文本已满时添加新页面

问题描述 投票:1回答:1

我正在使用JSPDF在我们的网站中生成pdf文件。我有一些内容,每次都可以有所不同。每次内容在第一页上溢出时,如何自动调整JsPdf参数以添加新页面

jspdf
1个回答
0
投票

惠,

它有点晚了,答案并不完美,但它对我有用(在ES6中):

private createNewPdf() {
    this.positionY = MARGIN_TOP; // margin top
    this.pdf = new jsPDF({ orientation: 'portrait', unit: 'mm', lineHeight: 1 });
}

// convert mm to fontSize
private setTextHeight(this.textHeight) {
    let fontSize = this.textHeight * 4;
    this.pdf.setFontSize(fontSize);
 }

private addText(text) {
    this.setTextHeight(textHeight);
    if (this.positionY + textHeight > (this.pdf.internal.pages.height - MARGIN_BOTTOM)) {
       this.addPage();
    }
    this.pdf.text(MARGIN_LEFT, this.positionY + this.textHeight, text);
    this.positionY += textHeight;
}

private addPage() {
   this.pdf.addPage();
   this.positionY = MARGIN_TOP;
}

所以在你必须:

this.textHeight = 3; // mm
this.positionY = 0; // position of previous text
this.pdf = null;

this.createNewPdf();
this.addText('My first text');
this.addText('My second text');
...

如果文本多于地方,则页面将自动添加MARGIN_TOP(您必须定义此内容)

© www.soinside.com 2019 - 2024. All rights reserved.