Skip to content

Instantly share code, notes, and snippets.

@jonz94
Created April 25, 2022 06:44
Show Gist options
  • Save jonz94/75d7f713d437fdcac5459a2042b58659 to your computer and use it in GitHub Desktop.
Save jonz94/75d7f713d437fdcac5459a2042b58659 to your computer and use it in GitHub Desktop.
merge multiple base64 format PDFs into single PDF File Object
import { PDFDocument } from 'pdf-lib';
async mergeMultiplgePdf(base64Pdfs: string[]): Promise<File> {
const pdfDoc = await PDFDocument.create();
for (base64Pdf of base64Pdfs) {
const url = `data:application/pdf;base64,${base64Pdf}`;
const donorPdfBytes = await fetch(url).then((res) => res.arrayBuffer());
const donorPdfDoc = await PDFDocument.load(donorPdfBytes);
for (var k = 0; k < donorPdfDoc.getPageCount(); k++) {
const [donorPage] = await pdfDoc.copyPages(donorPdfDoc, [k]);
pdfDoc.addPage(donorPage);
}
}
const mergedPdf = await pdfDoc.save();
return new File([mergedPdf], 'merged.pdf');
}
@MalikHamza007
Copy link

Hey did you find a solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment