forked from galkahana/HummusJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhummus.js
384 lines (323 loc) · 12.5 KB
/
hummus.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// start with binary objects
module.exports = require('./build/Release/hummus');
var fs = require('fs');
/*
PDFStreamForResponse is an implementation of a write stream that writes directly to an HTTP response.
Using this stream frees the user from having to create a PDF file on disk when generating on-demand PDFs
*/
function PDFStreamForResponse(inResponse)
{
this.response = inResponse;
this.position = 0;
}
PDFStreamForResponse.prototype.write = function(inBytesArray)
{
if(inBytesArray.length > 0)
{
this.response.write(new Buffer(inBytesArray));
this.position+=inBytesArray.length;
return inBytesArray.length;
}
else
return 0;
};
PDFStreamForResponse.prototype.getCurrentPosition = function()
{
return this.position;
};
module.exports.PDFStreamForResponse = PDFStreamForResponse;
/*
PDFWStreamForFile is an implementation of a write stream using the supplied file path.
*/
function PDFWStreamForFile(inPath)
{
this.ws = fs.createWriteStream(inPath);
this.position = 0;
this.path = inPath;
}
PDFWStreamForFile.prototype.write = function(inBytesArray)
{
if(inBytesArray.length > 0)
{
this.ws.write(new Buffer(inBytesArray));
this.position+=inBytesArray.length;
return inBytesArray.length;
}
else
return 0;
};
PDFWStreamForFile.prototype.getCurrentPosition = function()
{
return this.position;
};
PDFWStreamForFile.prototype.close = function(inCallback)
{
if(this.ws)
{
var self = this;
this.ws.end(function()
{
self.ws = null;
if(inCallback)
inCallback();
})
}
else
{
if(inCallback)
inCallback();
}
};
module.exports.PDFWStreamForFile = PDFWStreamForFile;
/*
PDFRStreamForFile is an implementation of a read stream using the supplied file path.
*/
function PDFRStreamForFile(inPath)
{
this.rs = fs.openSync(inPath,'r');
this.path = inPath;
this.rposition = 0;
this.fileSize = fs.statSync(inPath)["size"];
}
PDFRStreamForFile.prototype.read = function(inAmount)
{
var buffer = new Buffer(inAmount*2);
var bytesRead = fs.readSync(this.rs, buffer, 0, inAmount,this.rposition);
var arr = [];
for(var i=0;i<bytesRead;++i)
arr.push(buffer[i]);
this.rposition+=bytesRead;
return arr;
}
PDFRStreamForFile.prototype.notEnded = function()
{
return this.rposition < this.fileSize;
}
PDFRStreamForFile.prototype.setPosition = function(inPosition)
{
this.rposition = inPosition;
}
PDFRStreamForFile.prototype.setPositionFromEnd = function(inPosition)
{
this.rposition = this.fileSize-inPosition;
}
PDFRStreamForFile.prototype.skip = function(inAmount)
{
this.rposition += inAmount;
}
PDFRStreamForFile.prototype.getCurrentPosition = function(inAmount)
{
return this.rposition;
}
PDFRStreamForFile.prototype.close = function(inCallback)
{
fs.close(this.rs,inCallback);
};
module.exports.PDFRStreamForFile = PDFRStreamForFile;
/*
PDFPageModifier is a helper class providing a content context for existing pages, when in a file modification scenarios.
Using PDFPageModifier simplifies the process of adding content to existing pages, making it as simple to do as if it were
a new Page, with a regular content context.
*/
function PDFPageModifier(inModifiedFileWriter,inPageIndex)
{
this.writer = inModifiedFileWriter;
this.pageIndex = inPageIndex;
this.contexts = [];
}
PDFPageModifier.prototype.startContext = function()
{
if(!this.contextForm)
{
var pageMediaBox = this.writer.getModifiedFileParser().parsePage(this.pageIndex).getMediaBox();
this.contextForm = this.writer.createFormXObject(pageMediaBox[0],pageMediaBox[1],pageMediaBox[2],pageMediaBox[3]);
}
return this;
};
PDFPageModifier.prototype.getContext = function()
{
return this.contextForm ? this.contextForm.getContentContext() : null;
};
PDFPageModifier.prototype.endContext = function()
{
this.writer.endFormXObject(this.contextForm);
this.contexts.push(this.contextForm);
this.contextForm = null;
return this;
};
PDFPageModifier.prototype.writePage = function()
{
// allocate an object ID for the new contents stream (for placing the form)
// we first create the modified page object, so that we can define a name for the new form xobject
// that is unique
var objCxt = this.writer.getObjectsContext();
var newContentObjectID = objCxt.allocateNewObjectID();
// create a copying context, so we can copy the page dictionary, and modify its contents + resources dict
var cpyCxt = this.writer.createPDFCopyingContextForModifiedFile();
// get last page object, convert to JS object, so it's easier to traverse
var pageObjectID = cpyCxt.getSourceDocumentParser().getPageObjectID(this.pageIndex);
var pageDictionaryObject = cpyCxt.getSourceDocumentParser().parsePage(this.pageIndex).getDictionary().toJSObject();
// create modified page object
objCxt.startModifiedIndirectObject(pageObjectID);
var modifiedPageObject = objCxt.startDictionary();
// copy all elements of the page to the new page object, but the "Contents" and "Resources" elements
Object.getOwnPropertyNames(pageDictionaryObject).forEach(function(element,index,array)
{
if(element != 'Resources' && element != 'Contents')
{
modifiedPageObject.writeKey(element);
cpyCxt.copyDirectObjectAsIs(pageDictionaryObject[element]);
}
});
// Write new contents entry, joining the existing contents with the new one. take care of various scenarios of the existing Contents
modifiedPageObject.writeKey('Contents');
if(!pageDictionaryObject['Contents']) // no contents
{
objCxt.writeIndirectObjectReference(newContentObjectID);
}
else
{
objCxt.startArray();
if(pageDictionaryObject['Contents'].getType() == module.exports.ePDFObjectArray) // contents stream array
{
pageDictionaryObject['Contents'].toPDFArray().toJSArray().forEach(function(inElement)
{
objCxt.writeIndirectObjectReference(inElement.toPDFIndirectObjectReference().getObjectID());
});
}
else // single stream
{
objCxt.writeIndirectObjectReference(pageDictionaryObject['Contents'].toPDFIndirectObjectReference().getObjectID());
}
objCxt.writeIndirectObjectReference(newContentObjectID);
objCxt.endArray();
}
// Write a new resource entry. copy all but the "XObject" entry, which needs to be modified. Just for kicks i'm keeping the original
// form (either direct dictionary, or indirect object)
var resourcesIndirect = null;
var formResourcesNames = [];
modifiedPageObject.writeKey('Resources');
if(!pageDictionaryObject['Resources'])
{
// no existing resource dictionary, so write my own
var dict = objCxt.startDictionary();
dict.writeKey('XObject');
var xobjectDict = objCxt.startDictionary();
this.contexts.forEach(function(inElement,inIndex)
{
var formObjectName = "myForm_" + inIndex;
xobjectDict.writeKey(formObjectName);
xobjectDict.writeObjectReferenceValue(inElement.id);
formResourcesNames.push(formObjectName);
});
objCxt.endDictionary(xobjectDict).
endDictionary(dict);
}
else
{
// resources may be direct, or indirect. if direct, write as is, adding the new form xobject, otherwise wait till page object ends and write then
isResorucesIndirect = (pageDictionaryObject['Resources'].getType() == module.exports.ePDFObjectIndirectObjectReference);
if(isResorucesIndirect)
{
resourcesIndirect = pageDictionaryObject['Resources'].toPDFIndirectObjectReference().getObjectID();
modifiedPageObject.writeObjectReferenceValue(resourcesIndirect);
}
else
formResourcesNames = writeModifiedResourcesDict(pageDictionaryObject['Resources'],objCxt,cpyCxt,this.contexts);
}
// end page object and writing
objCxt.endDictionary(modifiedPageObject).
endIndirectObject();
// if necessary, create now the resource dictionary
if(resourcesIndirect)
{
objCxt.startModifiedIndirectObject(resourcesIndirect);
formResourcesNames = writeModifiedResourcesDict(cpyCxt.getSourceDocumentParser().parseNewObject(resourcesIndirect),objCxt,cpyCxt,this.contexts);
objCxt.endIndirectObject();
}
// last but not least, create the actual content stream object, placing the form
objCxt.startNewIndirectObject(newContentObjectID);
var streamCxt = objCxt.startUnfilteredPDFStream();
formResourcesNames.forEach(function(inElement)
{
objCxt.writeKeyword('q')
.writeNumber(1)
.writeNumber(0)
.writeNumber(0)
.writeNumber(1)
.writeNumber(0)
.writeNumber(0)
.writeKeyword('cm')
.writeName(inElement)
.writeKeyword('Do')
.writeKeyword('Q')
});
objCxt.endPDFStream(streamCxt)
.endIndirectObject();
return this;
};
function writeModifiedResourcesDict(inSourceDirect,inObjCxt,inCpyCxt,inNewXObjects)
{
var formResourcesNames = [];
var sourceObject = inSourceDirect.toPDFDictionary().toJSObject();
var dict = inObjCxt.startDictionary();
Object.getOwnPropertyNames(sourceObject).forEach(function(element,index,array)
{
if(element != 'XObject')
{
dict.writeKey(element);
inCpyCxt.copyDirectObjectAsIs(sourceObject[element]);
}
});
// now write a new xobject entry.
dict.writeKey('XObject');
var xobjectDict = inObjCxt.startDictionary();
var imageObjectName;
if(sourceObject['XObject']) // original exists, copy its keys
{
// i'm having a very sophisticated algo here to create a new unique name.
// i'm making sure it's different in one letter from any name, using a well known discrete math proof method
imageObjectName = '';
var jsDict = sourceObject['XObject'].toPDFDictionary().toJSObject();
Object.getOwnPropertyNames(jsDict).forEach(function(element,index,array)
{
xobjectDict.writeKey(element);
inCpyCxt.copyDirectObjectAsIs(jsDict[element]);
imageObjectName+=String.fromCharCode(
getDifferentChar(element.length >= index+1 ? element.charCodeAt(index) : 0x39));
});
inObjCxt.endLine();
}
else
imageObjectName = 'fm';
inNewXObjects.forEach(function(inElement,inIndex)
{
var formObjectName = imageObjectName + '_' + inIndex;
xobjectDict.writeKey(formObjectName);
xobjectDict.writeObjectReferenceValue(inElement.id);
formResourcesNames.push(formObjectName);
});
inObjCxt.endDictionary(xobjectDict)
.endDictionary(dict);
return formResourcesNames;
}
function getDifferentChar(inCharCode)
{
// numerals
if(inCharCode >= 0x30 && inCharCode <= 0x38)
return inCharCode+1;
if(inCharCode == 0x39)
return 0x30;
// lowercase
if(inCharCode >= 0x61 && inCharCode <= 0x79)
return inCharCode+1;
if(inCharCode == 0x7a)
return 0x61;
// uppercase
if(inCharCode >= 0x41 && inCharCode <= 0x59)
return inCharCode+1;
if(inCharCode == 0x5a)
return 0x41;
return 0x41;
}
module.exports.PDFPageModifier = PDFPageModifier;