Skip to content

Commit fd4392d

Browse files
committed
Use buffer instead of multiple writes
1 parent f19b455 commit fd4392d

2 files changed

Lines changed: 40 additions & 41 deletions

File tree

lib/document.coffee

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,25 @@ class PDFDocument extends stream.Readable
2727
@_waiting = 0
2828
@_ended = false
2929
@_offset = 0
30+
Pages = @ref
31+
Type: 'Pages'
32+
Count: 0
33+
Kids: new Buffer('')
34+
35+
Pages.finalize = () ->
36+
@offset = @document._offset;
37+
@document._write(@id + " " + @gen + " obj");
38+
@document._write('<<');
39+
@document._write('/Type /Pages');
40+
@document._write('/Count ' + @data.Count);
41+
@document._write('/Kids [' + @data.Kids.slice(0,-1).toString() + ']');
42+
@document._write('>>');
43+
@document._write('endobj');
44+
return @document._refEnd(@);
3045

3146
@_root = @ref
3247
Type: 'Catalog'
33-
Pages: @ref
34-
Type: 'Pages'
35-
Count: 0
36-
Kids: []
48+
Pages: Pages
3749

3850
# The current page
3951
@page = null
@@ -88,7 +100,7 @@ class PDFDocument extends stream.Readable
88100

89101
# add the page to the object store
90102
pages = @_root.data.Pages.data
91-
pages.Kids.push @page.dictionary
103+
pages.Kids = Buffer.concat([pages.Kids, new Buffer(@page.dictionary + ' ')]);
92104
pages.Count++
93105

94106
# reset x and y coordinates

lib/reference.coffee

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,50 @@ By Devon Govett
44
###
55

66
zlib = require 'zlib'
7-
stream = require 'stream'
87

98
class PDFReference
109
constructor: (@document, @id, @data = {}) ->
1110
@gen = 0
12-
@deflate = null
1311
@compress = @document.compress and not @data.Filter
1412
@uncompressedLength = 0
15-
@chunks = []
13+
@buffer = new Buffer('')
1614

17-
initDeflate: ->
18-
@data.Filter = 'FlateDecode'
19-
20-
@deflate = zlib.createDeflate()
21-
@deflate.on 'data', (chunk) =>
22-
@chunks.push chunk
23-
@data.Length += chunk.length
24-
25-
@deflate.on 'end', @finalize
26-
27-
write: (chunk, encoding, callback) ->
15+
write: (chunk) ->
2816
unless Buffer.isBuffer(chunk)
2917
chunk = new Buffer(chunk + '\n', 'binary')
3018

3119
@uncompressedLength += chunk.length
3220
@data.Length ?= 0
33-
21+
@buffer = Buffer.concat([@buffer, chunk])
22+
@data.Length += chunk.length
3423
if @compress
35-
@initDeflate() if not @deflate
36-
@deflate.write chunk
37-
else
38-
@chunks.push chunk
39-
@data.Length += chunk.length
24+
@data.Filter = 'FlateDecode'
4025

4126
end: (chunk) ->
42-
if @deflate
43-
@deflate.end()
44-
else
45-
@finalize()
27+
if chunk
28+
@write(chunk)
29+
@finalize()
4630

4731
finalize: =>
48-
@offset = @document._offset
49-
50-
@document._write "#{@id} #{@gen} obj"
51-
@document._write PDFObject.convert(@data)
32+
setTimeout () =>
33+
@offset = @document._offset
5234

53-
if @chunks.length
54-
@document._write 'stream'
55-
for chunk in @chunks
56-
@document._write chunk
35+
@document._write "#{@id} #{@gen} obj"
36+
@document._write PDFObject.convert(@data)
5737

58-
@chunks.length = 0 # free up memory
59-
@document._write '\nendstream'
38+
if @buffer.length
39+
if @compress
40+
@buffer = zlib.deflateSync(@buffer)
41+
@data.Length = @buffer.length
42+
@document._write 'stream'
43+
@document._write @buffer
6044

61-
@document._write 'endobj'
62-
@document._refEnd(this)
45+
@buffer.length = 0 # free up memory
46+
@document._write '\nendstream'
6347

48+
@document._write 'endobj'
49+
@document._refEnd(this)
50+
, 0
6451
toString: ->
6552
return "#{@id} #{@gen} R"
6653

0 commit comments

Comments
 (0)