Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add templating feature #56

Closed
wants to merge 4 commits into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 29 additions & 4 deletions lib/document.coffee
Expand Up @@ -10,7 +10,7 @@ PDFReference = require './reference'
PDFPage = require './page'

class PDFDocument
constructor: (@options = {}) ->
constructor: (@options = {}, temp) ->
# PDF version
@version = 1.3

Expand Down Expand Up @@ -43,6 +43,14 @@ class PDFDocument
@info[key] = val for key, val of @options.info
delete @options.info

@temp = @ref()

if (temp)
@store.objects[5].finalizedStream = temp.finalizedStream
@store.objects[5].data = temp.data
@haveTemp = true


# Add the first page
@addPage()

Expand Down Expand Up @@ -77,7 +85,11 @@ class PDFDocument
@store.ref(data)

addContent: (str) ->
@page.content.add str
if @isTemp is true
@temp.add str
else
@page.content.add str

return this # make chaining possible

write: (filename, callback) ->
Expand Down Expand Up @@ -115,14 +127,12 @@ class PDFDocument

generateBody: (out) ->
offset = out.join('\n').length

for id, ref of @store.objects
object = ref.object()
ref.offset = offset
out.push object

offset += object.length + 1

@xref_offset = offset

generateXRef: (out) ->
Expand All @@ -149,5 +159,20 @@ class PDFDocument

toString: ->
"[object PDFDocument]"

haveTemp: false
isTemp: false

startTemp: ->
@haveTemp = true
@isTemp = true

endTemp: ->
@temp.finalize true
@isTemp = false

outputTemp: ->
delete @temp.stream
@temp

module.exports = PDFDocument
11 changes: 8 additions & 3 deletions lib/page.coffee
Expand Up @@ -25,14 +25,19 @@ class PDFPage
@height = dimensions[if @layout is 'portrait' then 1 else 0]

# A reference to the content of this page
@content = @document.ref()

@content = @document.ref()

if @document.haveTemp
contents = [@content, @document.temp]
else
contents = @content

# The page dictionary
@dictionary = @document.ref
Type: 'Page'
Parent: @document.store.pages
MediaBox: [0, 0, @width, @height]
Contents: @content
Contents: contents

# The resource dictionary
@dictionary.data['Resources'] = @document.ref
Expand Down
3 changes: 1 addition & 2 deletions lib/reference.coffee
Expand Up @@ -10,13 +10,12 @@ class PDFReference
@gen = 0
@stream = null
@finalizedStream = null

object: ->
@finalize() if not @finalizedStream
out = ["#{@id} #{@gen} obj"]
out.push PDFObject.convert(@data)

if @stream
if @stream || @finalizedStream
out.push "stream"
out.push @finalizedStream
out.push "endstream"
Expand Down
4 changes: 2 additions & 2 deletions lib/store.coffee
Expand Up @@ -23,8 +23,8 @@ class PDFObjectStore
ref: (data) ->
@push ++@length, data

push: (id, data) ->
ref = new PDFReference(id, data)
push: (id, data, fStream) ->
ref = new PDFReference id, data
@objects[id] = ref
return ref

Expand Down