2023-10-26 20:53:02 +02:00
|
|
|
|
2023-10-26 22:41:48 +02:00
|
|
|
import { PDFDocument } from 'pdf-lib';
|
2023-11-12 02:46:09 +01:00
|
|
|
import { PdfFile, convertAllToPdfLibFile, fromPdfLib } from '../wrappers/PdfFile';
|
2023-10-26 20:53:02 +02:00
|
|
|
|
2023-11-11 21:43:39 +01:00
|
|
|
export async function mergePDFs(files: PdfFile[]): Promise<PdfFile> {
|
|
|
|
|
2023-11-12 02:46:09 +01:00
|
|
|
const pdfLibFiles = await convertAllToPdfLibFile(files);
|
2023-10-17 02:14:06 +02:00
|
|
|
|
2023-10-26 22:41:48 +02:00
|
|
|
const mergedPdf = await PDFDocument.create();
|
2023-10-17 02:14:06 +02:00
|
|
|
|
2023-11-12 02:46:09 +01:00
|
|
|
for (let i = 0; i < pdfLibFiles.length; i++) {
|
|
|
|
const pdfToMerge = await pdfLibFiles[i].getAsPdfLib();
|
2023-10-17 02:14:06 +02:00
|
|
|
const copiedPages = await mergedPdf.copyPages(pdfToMerge, pdfToMerge.getPageIndices());
|
|
|
|
copiedPages.forEach((page) => mergedPdf.addPage(page));
|
|
|
|
}
|
|
|
|
|
2023-11-12 14:57:53 +01:00
|
|
|
return fromPdfLib(mergedPdf, files[0].filename);
|
2023-10-17 02:14:06 +02:00
|
|
|
};
|