question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

index` must be at least 0 and at most 33 but my pdf have almost 41 pages

See original GitHub issue

What were you trying to do?

I am trying to remove pages using thi pdf-lib library.

How did you attempt to do it?

async downloadPdfFile() { var url = "/uploads/" + this.pdfFile.upload.filename; var existingPdfBytes = await fetch(url).then((res) => res.arrayBuffer()); var pdfDoc = await PDFDocument.load(existingPdfBytes); let pages = this.selectedPages; pages.sort(function (a, b) { return a - b; }); pages.map((element) => { console.log(element); pdfDoc.removePage(element); }); var PdfBytes = await pdfDoc.save(); download(PdfBytes,Delete Pdf ${new Date()}.pdf, "application/pdf"); },

What actually happened?

Error: index must be at least 0 and at most 33, but was actually 38. But I have more than 38 pages in my pdf.

What did you expect to happen?

I should remove the selected pages on the base of index given.

How can we reproduce the issue?

async downloadPdfFile() { var url = "/uploads/" + this.pdfFile.upload.filename; var existingPdfBytes = await fetch(url).then((res) => res.arrayBuffer()); var pdfDoc = await PDFDocument.load(existingPdfBytes); let pages = this.selectedPages; pages.sort(function (a, b) { return a - b; }); pages.map((element) => { console.log(element); pdfDoc.removePage(element); }); var PdfBytes = await pdfDoc.save(); download(PdfBytes,Delete Pdf ${new Date()}.pdf, "application/pdf"); },

Version

1.17.1

What environment are you running pdf-lib in?

Browser, Node

Checklist

  • My report includes a Short, Self Contained, Correct (Compilable) Example.
  • I have attached all PDFs, images, and other files needed to run my SSCCE.

Additional Notes

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
hilmanskicommented, Oct 25, 2022

Thanks for the trick @joewestcott I document it for other, in case anyone want an example https://onebite.dev/removing-page-in-pdf-lib-show-error-index-must-be-at-least-x/

const pages [4 , 1 , 3, 2]
const sortedPages = pages.sort((a: number, b: number) => b - a)
sortedPages.forEach((page: any) => {
    const pageIndex = page - 1
    PDF_DOC.removePage(pageIndex)
})
1reaction
abubakar-dzinemediacommented, Jun 7, 2022

Yes I tried it. Its working fine Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating PDF indexes, Adobe Acrobat
Follow these steps to create, manage, and revise an index, prepare a PDF for indexing, and add metadata to document properties in Adobe ......
Read more >
The Book Thief
—Of course, an introduction. A beginning. Where are my manners? I could introduce myself properly, but it's not really necessary. You will know ......
Read more >
Fall Protection in Construction
Each worker constructing a leading edge 6 feet or more above a lower level must be protected by guardrail systems, safety net systems,...
Read more >
An Introduction to Combinatorics and Graph Theory
First we need to be clear on the rules: the board is covered if the ... Perhaps the most famous problem in graph...
Read more >
16117-Health workforce requirements for universal ...
Total numbers of health workers needed (total need) to reach the SDG threshold estimated for 2013 and forecasted for 2030 (by income group...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found