-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmerge.ts
60 lines (54 loc) · 2.1 KB
/
merge.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { expandGlobSync } from "@std/fs";
import { join } from "@std/path";
import { PDFDocument, type PDFPage } from "pdf-lib";
/**
* Merges all PDF files in the specified directory or the current working directory.
*
* @param {Object} [options] - Options for the merge operation.
* @param {string} [options.dir] - Directory path to search for PDF files. Defaults to the current working directory.
* @param {string} [options.output] - Path for the output merged PDF file. Defaults to "merged.pdf" in the specified or current directory.
*
* @returns {Promise<void>} - Resolves when the merging is complete and the file is saved.
*/
export async function mergeAll(
options: { dir?: string; output?: string } = {},
) {
const directory = options.dir || Deno.cwd();
const outputFilePath = options.output || join(directory, "merged.pdf");
const globString = join(directory, "*.pdf");
const pdfsToMerge = Array.from(expandGlobSync(globString)).map(
(entry) => entry.path,
);
await mergePdfs(pdfsToMerge, { output: outputFilePath });
}
/**
* Merges an array of PDF files into a single PDF document.
*
* @param {string[]} pdfsToMerge - Array of file paths for the PDFs to merge.
* @param {Object} [options] - Options for the merge operation.
* @param {string} [options.output] - Path for the output merged PDF file. Defaults to "merged.pdf" in the current directory.
*
* @returns {Promise<void>} - Resolves when the merging is complete and the file is saved.
*/
export async function mergePdfs(
pdfsToMerge: string[],
options?: {
output?: string;
},
) {
const mergedPdf = await PDFDocument.create();
const outputFilePath = options?.output || join(Deno.cwd(), "merged.pdf");
for (const pdfPath of pdfsToMerge) {
const pdfBytes = Deno.readFileSync(pdfPath);
const pdfDoc = await PDFDocument.load(pdfBytes);
const copiedPages = await mergedPdf.copyPages(
pdfDoc,
pdfDoc.getPageIndices(),
);
copiedPages.forEach((page: PDFPage) => {
mergedPdf.addPage(page);
});
}
const mergedPdfFile = await mergedPdf.save();
Deno.writeFileSync(outputFilePath, mergedPdfFile);
}