-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (88 loc) · 2.91 KB
/
index.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env node
import { PDFDocument, StandardFonts, rgb } from "pdf-lib";
import fs from "fs/promises";
import { program } from "commander";
import path from "path";
import { mergePdfs } from "./commands/mergeCommand.js";
import { removePages } from "./commands/removePagesCommand.js";
import { extractPages } from "./commands/extractPagesCommand.js";
program
.name("pdfix")
.version("1.0.0")
.description("CLI tool for manipulating PDF files safely and efficiently");
/*
Create empty pdf files for testing
*/
program
.name("pdfix")
.command("create")
.arguments("<numberOfFiles>")
.description("Creates specified number of empty pdf files")
.action(async (numberOfFiles) => {
for (let i = 0; i < numberOfFiles; i++) {
const pdfDoc = await PDFDocument.create();
const page = pdfDoc.addPage();
const { width, height } = page.getSize();
const timesRomanFont = await pdfDoc.embedFont(StandardFonts.TimesRoman);
// Add text to the page before saving the document
const fontSize = 30;
page.drawText(`${i}`, {
x: 50,
y: height - 4 * fontSize,
size: fontSize,
font: timesRomanFont,
color: rgb(0, 0.53, 0.71),
});
// Save the document after the content is added
const pdfBytes = await pdfDoc.save();
const filePath = path.resolve(`testpdf${i}.pdf`);
// Write the PDF bytes to the file
await fs.writeFile(filePath, pdfBytes);
}
console.log(`${numberOfFiles} PDF files created successfully.`);
});
/*
Merge pdfs of two specified files
Usage: merge <filePath1> <filePath2> [otherFilePaths...]
*/
program
.name("pdfix")
.command("merge")
.option(
"-n, --name <fileName>",
'output file name. (Default: First file appended with "-merged")'
)
.arguments("<filePath1> <filePath2> [otherFilePaths...]")
.description("Merges two or more pdf files into a single pdf file")
.action(mergePdfs);
/*
Removes specified pages of specified file
Usage: removePages --seperator "[seperator]" <filePath> <pageNumbers>
*/
program
.name("pdfix")
.command("removePages")
.option("-s, --seperator <char>", "seperator for page numbers", ",")
.option(
"-n, --name <fileName>",
'output file name. (Default: File name appended with "-removed")'
)
.arguments("<filePath> <pageNumbers>")
.description("Removes specified pages of specified file")
.action(removePages);
/*
Extracts specified pages from specified file
Usage: removePages --seperator "[seperator]" <filePath> <pageNumbers>
*/
program
.name("pdfix")
.command("extractPages")
.option("-s, --seperator <char>", "seperator for page numbers", ",")
.option(
"-n, --name <fileName>",
'output file name. (Default: File name appended with "-extract")'
)
.arguments("<filePath> <pageNumbers>")
.description("Extracts specified pages from specified file")
.action(extractPages);
program.parseAsync(process.argv);