Repository navigation
Expand file tree
/
Copy pathdebug-pdf.js
More file actions
106 lines (90 loc) · 3.65 KB
/
Copy pathdebug-pdf.js
File metadata and controls
106 lines (90 loc) · 3.65 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
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
// Polyfill DOMMatrix
global.DOMMatrix = class DOMMatrix {
constructor() {
this.a = 1; this.b = 0; this.c = 0; this.d = 1; this.e = 0; this.f = 0;
}
toString() { return 'matrix(1, 0, 0, 1, 0, 0)'; }
};
const pdf = require('pdf-parse');
const fs = require('fs');
const output = [];
output.push('Keys: ' + JSON.stringify(Object.keys(pdf)));
(async () => {
if (pdf.default) {
output.push('Default keys: ' + JSON.stringify(Object.keys(pdf.default)));
}
if (pdf.PDFParse) {
output.push('--- Testing Instantiation ---');
let parser;
try {
parser = new pdf.PDFParse({}); // Successfully tested previously
output.push('Success: new PDFParse({})');
} catch (e) {
output.push('Failed: new PDFParse({}) -> ' + e.message);
}
const minimalPdf = Buffer.from('%PDF-1.0\n%EOF');
// Attempt: Data in constructor
try {
// @ts-ignore
const dataParser = new pdf.PDFParse({ data: minimalPdf });
await dataParser.load();
output.push('Success: new PDFParse({ data: buffer }).load()');
} catch (e) {
output.push('Failed: constructor({ data }) -> ' + e.message);
}
try {
// @ts-ignore
const urlParser = new pdf.PDFParse({ url: 'http://example.com/dummy.pdf', data: minimalPdf });
await urlParser.load();
output.push('Success: new PDFParse({ url, data }).load()');
} catch (e) {
output.push('Failed: constructor({ url, data }) -> ' + e.message);
}
if (parser) {
output.push('--- Parsing Source Code ---');
output.push(parser.load.toString());
output.push('--- Testing Load ---');
// Attempt 1: Direct Buffer
try {
// @ts-ignore
await parser.load(minimalPdf);
output.push('Success: parser.load(buffer)');
} catch (e) {
output.push('Failed: parser.load(buffer) -> ' + e.message);
}
// Attempt 2: Object with data
try {
// @ts-ignore
await parser.load({ data: minimalPdf });
output.push('Success: parser.load({ data: buffer })');
} catch (e) {
output.push('Failed: parser.load({ data: buffer }) -> ' + e.message);
}
// Attempt 2.5: Object with data AND url
try {
// @ts-ignore
await parser.load({ url: 'http://example.com/dummy.pdf', data: minimalPdf });
output.push('Success: parser.load({ url: dummy, data: buffer })');
} catch (e) {
output.push('Failed: parser.load({ url, data }) -> ' + e.message);
}
// Attempt 2.6: Object with data (Uint8Array) AND url
try {
// @ts-ignore
await parser.load({ url: 'http://example.com/dummy.pdf', data: new Uint8Array(minimalPdf) });
output.push('Success: parser.load({ url, data: uint8 })');
} catch (e) {
output.push('Failed: parser.load({ url, data: uint8 }) -> ' + e.message);
}
// Attempt 4: Object with source
try {
// @ts-ignore
await parser.load({ source: minimalPdf });
output.push('Success: parser.load({ source: buffer })');
} catch (e) {
output.push('Failed: parser.load({ source: buffer }) -> ' + e.message);
}
}
}
fs.writeFileSync('debug-output.txt', output.join('\n'));
})();