pdf-lib
pdf-lib copied to clipboard
Error in creating bookmarks while merging PDF documents using pdf-lib.
What were you trying to do?
Hello,
I am using the latest version of pdf-lib (version 1.17) and trying to create bookmarks while merging documents. It works fine sometimes, but other times I receive the error message "TypeError: object.sizeInBytes is not a function".
Here is the code snippet I am using:
async function mergePDFDocuments(documents, data, CoverPage) {
// Create a new PDF document to hold the merged PDF
const pdfDoc = await PDFDocument.create();
// Add the cover page to the merged PDF
const coverPageDoc = await PDFDocument.load(CoverPage);
const coverPage = await pdfDoc.copyPages(coverPageDoc, [0]);
coverPage.forEach(page => pdfDoc.addPage(page));
// Add the main PDF to the merged PDF
const mainDoc = await PDFDocument.load(data);
const mainDocPages = await pdfDoc.copyPages(mainDoc, mainDoc.getPageIndices());
mainDocPages.forEach(page => pdfDoc.addPage(page));
// Add additional PDF documents to the merged PDF
let totalPageCount = mainDoc.getPageCount();
for (const doc of documents) {
console.log("==================Working with file...=======>", doc.filename)
const docPages = await PDFDocument.load(doc.content);
const pagesToCopy = await pdfDoc.copyPages(docPages, docPages.getPageIndices());
pagesToCopy.forEach(page => pdfDoc.addPage(page));
// Create bookmarks for this file
const fileBookmarks = Array.from(Array(docPages.getPageCount()).keys()).map(pageNumber => {
return {
title: `Page ${totalPageCount + pageNumber + 1}`,
pageNumber: totalPageCount + pageNumber,
};
});
// Add bookmarks to merged document
let outlinesDict = pdfDoc.catalog.lookup(PDFName.of('Outlines'));
if (!outlinesDict) {
outlinesDict = pdfDoc.context.obj({
Type: 'Outlines',
First: null,
Last: null,
Count: 0,
});
pdfDoc.catalog.set(PDFName.of('Outlines'), outlinesDict);
}
const bookmarksOutline = pdfDoc.context.obj({
Type: 'Outline',
First: null,
Last: null,
Count: 0,
});
pdfDoc.context.assign(outlinesDict, 'First', bookmarksOutline);
for (const bookmark of fileBookmarks) {
const { title, pageNumber } = bookmark;
const page = pdfDoc.getPage(pageNumber);
const dest = pdfDoc.context.obj([
pdfDoc.context.obj(page.dictionary),
'Fit',
]);
const bookmarkItem = pdfDoc.context.obj({
Title: title,
Dest: dest,
Parent: bookmarksOutline,
});
pdfDoc.context.assign(bookmarksOutline, 'Count', bookmarksOutline.Count + 1);
if (bookmarksOutline.Last) {
pdfDoc.context.assign(bookmarksOutline.Last, 'Next', bookmarkItem);
pdfDoc.context.assign(bookmarkItem, 'Prev', bookmarksOutline.Last);
pdfDoc.context.assign(bookmarksOutline, 'Last', bookmarkItem);
} else {
pdfDoc.context.assign(bookmarksOutline, 'First', bookmarkItem);
pdfDoc.context.assign(bookmarksOutline, 'Last', bookmarkItem);
}
}
totalPageCount += docPages.getPageCount();
}
// Save the merged PDF as a buffer and return it
const pdfBytes = await pdfDoc.save();
return Buffer.from(new Uint8Array(pdfBytes));
}
Please let me know if there is anything else I can do to help.
How did you attempt to do it?
I feel like the issue is with the current version of this package.
What actually happened?
I feel like the issue is with the current version of this package.
What did you expect to happen?
I feel like the issue is with the current version of this package.
How can we reproduce the issue?
I feel like the issue is with the current version of this package.
Version
1.17.1
What environment are you running pdf-lib in?
Node
Checklist
- [X] My report includes a Short, Self Contained, Correct (Compilable) Example.
- [X] I have attached all PDFs, images, and other files needed to run my SSCCE.
Additional Notes
No response
I have encountered the same problem, and I would like to know if you already have a solution? Thank you!