user2565735
user2565735

Reputation: 119

PDFKIT - NODEJS: How to get current page number?

I am using pdfkit module (nodejs) for pdf generation. I need to switch the pages and write data to the file based on some conditions. After switching the page and writing data, it's jumping to the another page based on the content length. I need to find the current page number to build the logic. Eg: (There are 10 pages. If switch the writer/ process to the Page 4 and write some content, the process would be jump to Page 5 / 6 based on content type. I want to get which is the current process page) May I know how can I get the current page number? TIA!

Upvotes: 0

Views: 1174

Answers (1)

Vincent Maret
Vincent Maret

Reputation: 11

I had the same issue and found a workaround.

  getCurrentPageNumber(): number {
    const pageBuffer: PDFKit.PDFPage[] = (this.doc as any)._pageBuffer
    const currentPage = this.doc.page
    let currentPageNumber: number | null = null
    pageBuffer.forEach((page: PDFKit.PDFPage, i: number) => {
      if (page === currentPage) {
        currentPageNumber = i
      }
    })
    if (currentPageNumber === null) {
      throw new Error('Unable to get current page number')
    }
    return currentPageNumber
  }

I made a feature request on the repo https://github.com/foliojs/pdfkit/issues/1408

Upvotes: 1

Related Questions