forked from foliojs/pdfkit
/
page.js
171 lines (150 loc) · 4.13 KB
/
page.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
PDFPage - represents a single page in the PDF document
By Devon Govett
*/
const DEFAULT_MARGINS = {
top: 72,
left: 72,
bottom: 72,
right: 72
};
const SIZES = {
'4A0': [4767.87, 6740.79],
'2A0': [3370.39, 4767.87],
A0: [2383.94, 3370.39],
A1: [1683.78, 2383.94],
A2: [1190.55, 1683.78],
A3: [841.89, 1190.55],
A4: [595.28, 841.89],
A5: [419.53, 595.28],
A6: [297.64, 419.53],
A7: [209.76, 297.64],
A8: [147.4, 209.76],
A9: [104.88, 147.4],
A10: [73.7, 104.88],
B0: [2834.65, 4008.19],
B1: [2004.09, 2834.65],
B2: [1417.32, 2004.09],
B3: [1000.63, 1417.32],
B4: [708.66, 1000.63],
B5: [498.9, 708.66],
B6: [354.33, 498.9],
B7: [249.45, 354.33],
B8: [175.75, 249.45],
B9: [124.72, 175.75],
B10: [87.87, 124.72],
C0: [2599.37, 3676.54],
C1: [1836.85, 2599.37],
C2: [1298.27, 1836.85],
C3: [918.43, 1298.27],
C4: [649.13, 918.43],
C5: [459.21, 649.13],
C6: [323.15, 459.21],
C7: [229.61, 323.15],
C8: [161.57, 229.61],
C9: [113.39, 161.57],
C10: [79.37, 113.39],
RA0: [2437.8, 3458.27],
RA1: [1729.13, 2437.8],
RA2: [1218.9, 1729.13],
RA3: [864.57, 1218.9],
RA4: [609.45, 864.57],
SRA0: [2551.18, 3628.35],
SRA1: [1814.17, 2551.18],
SRA2: [1275.59, 1814.17],
SRA3: [907.09, 1275.59],
SRA4: [637.8, 907.09],
EXECUTIVE: [521.86, 756.0],
FOLIO: [612.0, 936.0],
LEGAL: [612.0, 1008.0],
LETTER: [612.0, 792.0],
TABLOID: [792.0, 1224.0]
};
class PDFPage {
constructor(document, options = {}) {
this.document = document;
this.size = options.size || 'letter';
this.layout = options.layout || 'portrait';
// process margins
if (typeof options.margin === 'number') {
this.margins = {
top: options.margin,
left: options.margin,
bottom: options.margin,
right: options.margin
};
// default to 1 inch margins
} else {
this.margins = options.margins || DEFAULT_MARGINS;
}
// calculate page dimensions
const dimensions = Array.isArray(this.size)
? this.size
: SIZES[this.size.toUpperCase()];
this.width = dimensions[this.layout === 'portrait' ? 0 : 1];
this.height = dimensions[this.layout === 'portrait' ? 1 : 0];
this.content = this.document.ref();
// Initialize the Font, XObject, and ExtGState dictionaries
this.resources = this.document.ref({
ProcSet: ['PDF', 'Text', 'ImageB', 'ImageC', 'ImageI']
});
// The page dictionary
this.dictionary = this.document.ref({
Type: 'Page',
Parent: this.document._root.data.Pages,
MediaBox: [0, 0, this.width, this.height],
Contents: this.content,
Resources: this.resources
});
this.markings = [];
}
// Lazily create these objects
get fonts() {
const data = this.resources.data;
return data.Font != null ? data.Font : (data.Font = {});
}
get xobjects() {
const data = this.resources.data;
return data.XObject != null ? data.XObject : (data.XObject = {});
}
get ext_gstates() {
const data = this.resources.data;
return data.ExtGState != null ? data.ExtGState : (data.ExtGState = {});
}
get patterns() {
const data = this.resources.data;
return data.Pattern != null ? data.Pattern : (data.Pattern = {});
}
get colorSpaces() {
const data = this.resources.data;
return data.ColorSpace || (data.ColorSpace = {});
}
get annotations() {
const data = this.dictionary.data;
return data.Annots != null ? data.Annots : (data.Annots = []);
}
get structParentTreeKey() {
const data = this.dictionary.data;
return data.StructParents != null
? data.StructParents
: (data.StructParents = this.document.createStructParentTreeNextKey());
}
maxY() {
return this.height - this.margins.bottom;
}
write(chunk) {
return this.content.write(chunk);
}
end() {
this.dictionary.end();
this.resources.data.ColorSpace = {};
let i = 1;
for (let item in this.document.spotColors) {
item = this.document.spotColors[item];
this.resources.data.ColorSpace[`CS${i++}`] = `${item.id} 0 R`;
}
this.resources.end();
return this.content.end();
}
}
export default PDFPage;