-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
109 lines (98 loc) · 2.52 KB
/
index.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
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
103
104
105
106
107
108
109
import {Args, Flags} from '@oclif/core';
import {
addExtension,
removeExtension,
// Removing the extension will make the built cli crash
} from '../../utils.js';
import {BaseCommand} from '../../base-command.js';
export default class Compress2 extends BaseCommand {
static aliases = ['c2'];
static description = `Compress streams [mutool]`;
static examples = [
'<%= config.bin %> <%= command.id %> decompressed.pdf',
'<%= config.bin %> <%= command.id %> decompressed.pdf -o compressed.pdf',
];
static args = {
input: Args.string({
required: true,
description: `Decompressed PDF file`,
}),
};
static flags = {
output: Flags.string({
char: 'o',
description: 'Output file',
}),
linearize: Flags.boolean({
char: 'l',
aliases: ['optimize'],
charAliases: ['O'],
description:
'Linearize PDF (optimize for web browsers) (ALIASES: -O, --optimize)',
}),
pages: Flags.string({
char: 'p',
multiple: true,
description:
'Comma/space separated list of page numbers and ranges (1,3-5 12-9 N)',
}),
metadata: Flags.boolean({
char: 'm',
description: 'Preserve metadata',
}),
};
async run(): Promise<void> {
const {args, flags} = await this.parse(Compress2);
const {input} = args;
const {
output,
'dry-run': dryRun,
silent,
linearize,
metadata,
pages,
} = flags;
let finalOutput: string;
if (output) {
finalOutput = removeExtension(output);
} else {
finalOutput = removeExtension(input);
finalOutput = `${finalOutput}-compressed`;
}
finalOutput = addExtension(finalOutput);
const outputOptions = [
// -g garbage collect unused objects
// -gg in addition to -g compact xref table
// -ggg in addition to -gg merge duplicate objects
// -gggg in addition to -ggg check streams for duplication
'-gggg',
// -z deflate uncompressed streams
'-z',
// -f compress font streams
'-f',
// -i compress image streams
'-i',
// -c clean content streams
'-c',
// -s sanitize content streams
'-s',
];
if (linearize) {
outputOptions.push('-l');
}
if (metadata) {
outputOptions.push('-m');
}
await this.ensureDirExists(finalOutput);
this.logger(`Creating ${finalOutput}...`, silent);
const args2 = ['clean', ...outputOptions, input, finalOutput];
if (pages) {
let pagesOption = pages.join(',');
pagesOption = pagesOption.replaceAll(/,+/g, ',');
pagesOption = pagesOption.replace(/,$/, '');
args2.push(pagesOption);
}
await this.execute('mutool', args2, dryRun);
this.logger('Done.', silent);
}
}