forked from nrhirani/node-qpdf
-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
decrypt.ts
40 lines (33 loc) · 1006 Bytes
/
decrypt.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
import execute from "./spawn.js";
import { fileExists } from "./utils.js";
export interface DecryptSettings {
/** The path for the encrypted pdf */
input: string;
/** The path for the decrypted pdf */
output?: string;
/** The password required for decrypting the pdf */
password?: string;
}
/**
* Decrypts a PDF
* @param payload The settings for decryption
* @returns The output of QPDF
*/
export const decrypt = async (payload: DecryptSettings): Promise<Buffer> => {
if (!payload.input) throw new Error("Please specify input file");
if (!fileExists(payload.input)) throw new Error("Input file doesn't exist");
const callArguments = ["--decrypt"];
// Password
if (payload.password) {
callArguments.push(`--password=${payload.password}`);
}
// Input file path
callArguments.push(payload.input);
// Print PDF on stdout
if (payload.output) {
callArguments.push(payload.output);
} else {
callArguments.push("-");
}
return execute(callArguments);
};