Created
April 25, 2022 06:44
-
-
Save jonz94/75d7f713d437fdcac5459a2042b58659 to your computer and use it in GitHub Desktop.
merge multiple base64 format PDFs into single PDF File Object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey did you find a solution