Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions lib/document.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,25 @@ class PDFDocument extends stream.Readable
@_waiting = 0
@_ended = false
@_offset = 0
Pages = @ref
Type: 'Pages'
Count: 0
Kids: []

Pages.finalize = () ->
@offset = @document._offset;
@document._write(@id + " " + @gen + " obj");
@document._write('<<');
@document._write('/Type /Pages');
@document._write('/Count ' + @data.Count);
@document._write('/Kids [' + Buffer.concat(@data.Kids).slice(0,-1).toString() + ']');
@document._write('>>');
@document._write('endobj');
return @document._refEnd(@);

@_root = @ref
Type: 'Catalog'
Pages: @ref
Type: 'Pages'
Count: 0
Kids: []
Pages: Pages

# The current page
@page = null
Expand Down Expand Up @@ -88,7 +100,7 @@ class PDFDocument extends stream.Readable

# add the page to the object store
pages = @_root.data.Pages.data
pages.Kids.push @page.dictionary
pages.Kids.push(new Buffer(@page.dictionary + ' '))
pages.Count++

# reset x and y coordinates
Expand Down
7 changes: 6 additions & 1 deletion lib/font/embedded.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ class EmbeddedFont extends PDFFont
if isCFF
fontFile.data.Subtype = 'CIDFontType0C'

@subset.encodeStream().pipe(fontFile)
@subset.encodeStream()
.on 'data', (data) ->
fontFile.write(data)

.on 'end', () ->
fontFile.end()

familyClass = (@font['OS/2']?.sFamilyClass or 0) >> 8
flags = 0
Expand Down
85 changes: 34 additions & 51 deletions lib/reference.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,53 @@ By Devon Govett
###

zlib = require 'zlib'
stream = require 'stream'

class PDFReference extends stream.Writable
class PDFReference
constructor: (@document, @id, @data = {}) ->
super decodeStrings: no
@gen = 0
@deflate = null
@compress = @document.compress and not @data.Filter
@uncompressedLength = 0
@chunks = []

initDeflate: ->
@data.Filter = 'FlateDecode'

@deflate = zlib.createDeflate()
@deflate.on 'data', (chunk) =>
@chunks.push chunk
@data.Length += chunk.length

@deflate.on 'end', @finalize

_write: (chunk, encoding, callback) ->
@buffer = []

write: (chunk) ->
unless Buffer.isBuffer(chunk)
chunk = new Buffer(chunk + '\n', 'binary')

@uncompressedLength += chunk.length
@data.Length ?= 0

@buffer.push(chunk)
@data.Length += chunk.length
if @compress
@initDeflate() if not @deflate
@deflate.write chunk
else
@chunks.push chunk
@data.Length += chunk.length

callback()

@data.Filter = 'FlateDecode'

end: (chunk) ->
super

if @deflate
@deflate.end()
else
@finalize()

if chunk
@write(chunk)
@finalize()

finalize: =>
@offset = @document._offset

@document._write "#{@id} #{@gen} obj"
@document._write PDFObject.convert(@data)

if @chunks.length
@document._write 'stream'
for chunk in @chunks
@document._write chunk

@chunks.length = 0 # free up memory
@document._write '\nendstream'

@document._write 'endobj'
@document._refEnd(this)

setTimeout () =>
@offset = @document._offset

@document._write "#{@id} #{@gen} obj"
@document._write PDFObject.convert(@data)

if @buffer.length
@buffer = Buffer.concat(@buffer)
if @compress
@buffer = zlib.deflateSync(@buffer)
@data.Length = @buffer.length
@document._write 'stream'
@document._write @buffer

@buffer.length = 0 # free up memory
@document._write '\nendstream'

@document._write 'endobj'
@document._refEnd(this)
, 0
toString: ->
return "#{@id} #{@gen} R"

module.exports = PDFReference
PDFObject = require './object'