Skip to content

Commit

Permalink
Update reference.js with upstream version
Browse files Browse the repository at this point in the history
  • Loading branch information
klimeryk committed Mar 29, 2024
1 parent 4376bcb commit 5d9a8e9
Showing 1 changed file with 29 additions and 49 deletions.
78 changes: 29 additions & 49 deletions packages/pdfkit/src/reference.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,37 @@
import zlib from 'zlib';
import stream from 'stream';
import PDFObject from './object';

class PDFReference extends stream.Writable {
constructor(document, id, data) {
super({ decodeStrings: false });

this.finalize = this.finalize.bind(this);
class PDFReference {
constructor(document, id, data = {}) {
this.document = document;
this.id = id;
if (data == null) {
data = {};
}
this.data = data;

this.gen = 0;
this.deflate = null;
this.compress = this.document.compress && !this.data.Filter;
this.uncompressedLength = 0;
this.chunks = [];
this.buffer = [];
}

initDeflate() {
this.data.Filter = 'FlateDecode';

this.deflate = zlib.createDeflate();
this.deflate.on('data', (chunk) => {
this.chunks.push(chunk);
return (this.data.Length += chunk.length);
});

return this.deflate.on('end', this.finalize);
}

_write(chunk, encoding, callback) {
if (!(chunk instanceof Uint8Array)) {
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) {
if (!this.deflate) {
this.initDeflate();
}
this.deflate.write(chunk);
} else {
this.chunks.push(chunk);
this.data.Length += chunk.length;
return (this.data.Filter = 'FlateDecode');
}

return callback();
}

end() {
super.end(...arguments);

if (this.deflate) {
return this.deflate.end();
end(chunk) {
if (chunk) {
this.write(chunk);
}

return this.finalize();
}

Expand All @@ -72,23 +41,34 @@ class PDFReference extends stream.Writable {
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.chunks.length) {
if (this.buffer.length) {
this.document._write('stream');
for (let chunk of Array.from(this.chunks)) {
this.document._write(chunk);
}
this.document._write(this.buffer);

this.chunks.length = 0; // free up memory
this.buffer = []; // free up memory
this.document._write('\nendstream');
}

this.document._write('endobj');
return this.document._refEnd(this);
this.document._refEnd(this);
}

toString() {
return `${this.id} ${this.gen} R`;
}
Expand Down

0 comments on commit 5d9a8e9

Please sign in to comment.