Currently, I am working on developing an spfx web part that has the functionality to merge selected documents into one. By utilizing the pdf-lib and @pnp/sp libraries, I have been able to work on the following code:
const mergedFile = await PDFDocument.create();
// Iterating through each selected item
this.context.listView.selectedRows.forEach(async (row: RowAccessor) => {
// Accessing the file array buffer
const file = sp.web.getFileByServerRelativePath(row.getValueByName('FileRef'));
const fileBytes: ArrayBuffer = await file.getBuffer();
const fileToMerge = await PDFDocument.load(fileBytes);
const copyPages = await mergedFile.copyPages(fileToMerge, fileToMerge.getPageIndices());
copyPages.forEach((page) => {
mergedFile.addPage(page);
});
});
const savedMergedFile = await Promise.resolve(mergedFile.save());
const newFileAsBlob = new Blob([savedMergedFile]);
const newFile = await sp.web.getFolderByServerRelativePath(path).files.addChunked(encodeURI("newFile.pdf"), newFileAsBlob, undefined, true);
For creating this web part, I referred to this link and this link. However, upon testing the functionality, I noticed that the resulting document only contains a blank page. Can anyone help me understand what might be causing this issue?