jspdf在第一页上文字填满时添加新页面

时间:2017-03-21 10:02:24

标签: jspdf

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

1 个答案:

答案 0 :(得分: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(你必须定义)

相关问题