UNPKG

3.27 kBJavaScriptView Raw
1
2/*
3PDFReference - represents a reference to another object in the PDF object heirarchy
4By Devon Govett
5 */
6
7(function() {
8 var PDFObject, PDFReference, stream, zlib,
9 bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
10 extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
11 hasProp = {}.hasOwnProperty;
12
13 zlib = require('zlib');
14
15 stream = require('stream');
16
17 PDFReference = (function(superClass) {
18 extend(PDFReference, superClass);
19
20 function PDFReference(document, id, data) {
21 this.document = document;
22 this.id = id;
23 this.data = data != null ? data : {};
24 this.finalize = bind(this.finalize, this);
25 PDFReference.__super__.constructor.call(this, {
26 decodeStrings: false
27 });
28 this.gen = 0;
29 this.deflate = null;
30 this.compress = this.document.compress && !this.data.Filter;
31 this.uncompressedLength = 0;
32 this.chunks = [];
33 }
34
35 PDFReference.prototype.initDeflate = function() {
36 this.data.Filter = 'FlateDecode';
37 this.deflate = zlib.createDeflate();
38 this.deflate.on('data', (function(_this) {
39 return function(chunk) {
40 _this.chunks.push(chunk);
41 return _this.data.Length += chunk.length;
42 };
43 })(this));
44 return this.deflate.on('end', this.finalize);
45 };
46
47 PDFReference.prototype._write = function(chunk, encoding, callback) {
48 var base;
49 if (!Buffer.isBuffer(chunk)) {
50 chunk = new Buffer(chunk + '\n', 'binary');
51 }
52 this.uncompressedLength += chunk.length;
53 if ((base = this.data).Length == null) {
54 base.Length = 0;
55 }
56 if (this.compress) {
57 if (!this.deflate) {
58 this.initDeflate();
59 }
60 this.deflate.write(chunk);
61 } else {
62 this.chunks.push(chunk);
63 this.data.Length += chunk.length;
64 }
65 return callback();
66 };
67
68 PDFReference.prototype.end = function(chunk) {
69 PDFReference.__super__.end.apply(this, arguments);
70 if (this.deflate) {
71 return this.deflate.end();
72 } else {
73 return this.finalize();
74 }
75 };
76
77 PDFReference.prototype.finalize = function() {
78 var chunk, i, len, ref;
79 this.offset = this.document._offset;
80 this.document._write(this.id + " " + this.gen + " obj");
81 this.document._write(PDFObject.convert(this.data));
82 if (this.chunks.length) {
83 this.document._write('stream');
84 ref = this.chunks;
85 for (i = 0, len = ref.length; i < len; i++) {
86 chunk = ref[i];
87 this.document._write(chunk);
88 }
89 this.chunks.length = 0;
90 this.document._write('\nendstream');
91 }
92 this.document._write('endobj');
93 return this.document._refEnd(this);
94 };
95
96 PDFReference.prototype.toString = function() {
97 return this.id + " " + this.gen + " R";
98 };
99
100 return PDFReference;
101
102 })(stream.Writable);
103
104 module.exports = PDFReference;
105
106 PDFObject = require('./object');
107
108}).call(this);