Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add templating feature #56

Closed
wants to merge 4 commits into from

2 participants

@alFReD-NSH

This is a nice feature that we can build static parts of a PDF file before, save them, and then later on we put them on the dynamic content for everypage. Though the way I implemented it may not be the best(for example you can switch pages, which you should not, or maybe the fonts may not work), but at the moment it basically works:

I added these functions to public API:

  • startTemp() -> starts the templating mode

  • endTemp() -> ends the templating mode

  • outputTemp() -> which will outputs the template

I added an argument to the end of the constructor function:

  • constructor: (@options = {}, temp) in which temp is object that is returned by outputTemp()

Beside that 3 variables:

  • haveTemp: false

  • isTemp: false

  • temp -> which holds the template object

But this is just a work in progress. I'm thinking of adding features such as, building on a pre-built file, or having multiple templates.

@alFReD-NSH

This is the example:

var pdf = require('./index.js');

var doc = new pdf();
doc.startTemp();
doc.text('im a template!');
doc.endTemp();

var temp = JSON.parse(JSON.stringify( doc.outputTemp() )); // To ensure nothing happens when it is saved

var doc2 = new pdf({}, temp);
doc2.text('hi2');
doc2.addPage();
doc2.text('hi3');
doc2.write('tess.pdf');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
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
Something went wrong with that request. Please try again.