-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
reference.js
84 lines (69 loc) · 1.94 KB
/
reference.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
/*
PDFReference - represents a reference to another object in the PDF object heirarchy
By Devon Govett
*/
import zlib from 'zlib';
import PDFAbstractReference from './abstract_reference';
import PDFObject from './object';
class PDFReference extends PDFAbstractReference {
constructor(document, id, data = {}) {
super();
this.document = document;
this.id = id;
this.data = data;
this.gen = 0;
this.compress = this.document.compress && !this.data.Filter;
this.uncompressedLength = 0;
this.buffer = [];
}
write(chunk) {
if (!Buffer.isBuffer(chunk)) {
chunk = Buffer.from(chunk + '\n', 'binary');
}
this.uncompressedLength += chunk.length;
if (this.data.Length == null) {
this.data.Length = 0;
}
this.buffer.push(chunk);
this.data.Length += chunk.length;
if (this.compress) {
return (this.data.Filter = 'FlateDecode');
}
}
end(chunk) {
if (chunk) {
this.write(chunk);
}
return this.finalize();
}
finalize() {
this.offset = this.document._offset;
const encryptFn = this.document._security
? this.document._security.getEncryptFn(this.id, this.gen)
: null;
if (this.buffer.length) {
this.buffer = Buffer.concat(this.buffer);
if (this.compress) {
this.buffer = zlib.deflateSync(this.buffer);
}
if (encryptFn) {
this.buffer = encryptFn(this.buffer);
}
this.data.Length = this.buffer.length;
}
this.document._write(`${this.id} ${this.gen} obj`);
this.document._write(PDFObject.convert(this.data, encryptFn));
if (this.buffer.length) {
this.document._write('stream');
this.document._write(this.buffer);
this.buffer = []; // free up memory
this.document._write('\nendstream');
}
this.document._write('endobj');
this.document._refEnd(this);
}
toString() {
return `${this.id} ${this.gen} R`;
}
}
export default PDFReference;