Add templating feature #56

Closed
wants to merge 4 commits into
from
View
33 lib/document.coffee
@@ -10,7 +10,7 @@ PDFReference = require './reference'
PDFPage = require './page'
class PDFDocument
- constructor: (@options = {}) ->
+ constructor: (@options = {}, temp) ->
# PDF version
@version = 1.3
@@ -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()
@@ -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) ->
@@ -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) ->
@@ -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
View
11 lib/page.coffee
@@ -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
View
3 lib/reference.coffee
@@ -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"
View
4 lib/store.coffee
@@ -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