index` must be at least 0 and at most 33 but my pdf have almost 41 pages
See original GitHub issueWhat 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:
- Created a year ago
- Comments:7

Top Related StackOverflow Question
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/
Yes I tried it. Its working fine Thanks.