Adding Footer to pdf using jsPDF

前端 未结 7 1481
南笙
南笙 2021-01-02 09:33

I am generating pdf from jsPDF api , I want to add footer to each page with page number .

How to achieve this . It is having option of adding footer from fromHTML p

7条回答
  •  隐瞒了意图╮
    2021-01-02 10:20

    Stephen Collins is the best answer! It works well with jspdf-autotable plugin.

    With this is made after all is added to the doc, so we can use easy the total page number!

    Add some style to the Stephen Collins answer: "page x of total"

    const addFooters = doc => {
      const pageCount = doc.internal.getNumberOfPages()
    
      doc.setFont('helvetica', 'italic')
      doc.setFontSize(8)
      for (var i = 1; i <= pageCount; i++) {
        doc.setPage(i)
        doc.text('Page ' + String(i) + ' of ' + String(pageCount), doc.internal.pageSize.width / 2, 287, {
          align: 'center'
        })
      }
    }
    
    
    let doc = new jsPDF()
    
    doc.text(...)
    doc.autoTable(...)
    addFooters(doc)
    doc.save()
    

提交回复
热议问题