-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDFRepair.ts
More file actions
78 lines (67 loc) · 2.11 KB
/
PDFRepair.ts
File metadata and controls
78 lines (67 loc) · 2.11 KB
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
import { PDFDocument } from "@peculiarventures/pdf-doc";
import { PDFRepairRegistry, IRepairRule, globalRepairRegistry } from "./PDFRepairRegistry";
import { PDFRepairStatus } from "./PDFRepairStatus";
/**
* An object that records repair notes for a PDF file. The keys are rule IDs and the values are arrays of strings
* containing the repair notes.
*/
export interface RepairNotes {
[key: string]: string[];
}
export interface RepairCheckRule {
status: PDFRepairStatus;
description: string;
}
export interface RepairCheck {
status: PDFRepairStatus;
rules: Record<string, RepairCheckRule>;
}
/**
* A class for repairing PDF documents by applying a set of rules to them.
*/
export class PDFRepair {
private rules: IRepairRule[] = [];
constructor(registry: PDFRepairRegistry = globalRepairRegistry, ruleIds?: string[]) {
this.rules = registry.getRules(ruleIds);
}
/**
* Repairs a PDF document by applying a set of rules to it.
* @param doc - The PDF document to repair.
* @returns An array of strings representing the repair notes generated by the applied rules.
*/
async repairDocument(doc: PDFDocument): Promise<RepairNotes> {
const repairNotes: RepairNotes = {};
for (const rule of this.rules) {
const notes = await rule.apply(doc);
if (notes.length) {
if (repairNotes[rule.id]) {
throw new Error(`Rule '${rule.id}' returned multiple repair notes.`);
}
repairNotes[rule.id] = notes;
}
}
return repairNotes;
}
/**
* Checks if a PDF document needs to be repaired.
* @param doc - The PDF document to check.
* @returns
*/
async checkDocument(doc: PDFDocument): Promise<RepairCheck> {
const check: RepairCheck = {
status: PDFRepairStatus.notNeeded,
rules: {},
};
for (const rule of this.rules) {
const status = await rule.check(doc);
if (check.status !== PDFRepairStatus.requireClone && status !== PDFRepairStatus.notNeeded) {
check.status = status;
}
check.rules[rule.id] = {
status,
description: rule.description,
};
}
return check;
}
}