Skip to content

Added removePage() method #65

Closed
wants to merge 5 commits into from
View
68 lib/document.coffee
@@ -13,76 +13,82 @@ class PDFDocument
constructor: (@options = {}) ->
# PDF version
@version = 1.3
-
+
# Whether streams should be compressed
@compress = true
-
+
# The PDF object store
@store = new PDFObjectStore
-
+
# A list of pages in this document
@pages = []
-
+
# The current page
@page = null
-
+
# Initialize mixins
@initColor()
@initFonts()
@initText()
@initImages()
-
+
# Create the metadata
@_info = @ref
Producer: 'PDFKit'
Creator: 'PDFKit'
CreationDate: new Date()
-
+
@info = @_info.data
if @options.info
@info[key] = val for key, val of @options.info
delete @options.info
-
+
# Add the first page
@addPage()
-
+
mixin = (name) =>
methods = require './mixins/' + name
for name, method of methods
this::[name] = method
-
+
# Load mixins
mixin 'color'
mixin 'vector'
mixin 'fonts'
mixin 'text'
mixin 'images'
mixin 'annotations'
-
+
addPage: (options = @options) ->
# create a page object
@page = new PDFPage(this, options)
-
+
# add the page to the object store
@store.addPage @page
@pages.push @page
-
+
# reset x and y coordinates
@x = @page.margins.left
@y = @page.margins.top
-
+
return this
-
+
+ removePage: ->
+ # remove a page
+ @store.removePage()
+ @pages.pop()
+ return this
+
ref: (data) ->
@store.ref(data)
-
+
addContent: (str) ->
@page.content.add str
return this # make chaining possible
-
+
write: (filename, callback) ->
fs.writeFile filename, @output(), 'binary', callback
-
+
output: ->
out = []
@finalize()
@@ -91,40 +97,40 @@ class PDFDocument
@generateXRef out
@generateTrailer out
return out.join('\n')
-
+
finalize: ->
# convert strings in the info dictionary to literals
for key, val of @info when typeof val is 'string'
@info[key] = PDFObject.s val
-
+
# embed the subsetted fonts
for family, font of @_fontFamilies
font.embed()
-
+
# finalize each page
for page in @pages
page.finalize()
-
+
generateHeader: (out) ->
# PDF version
out.push "%PDF-#{@version}"
# 4 binary chars, as recommended by the spec
out.push "%\xFF\xFF\xFF\xFF\n"
return out
-
+
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) ->
len = @store.length + 1
out.push "xref"
@@ -134,7 +140,7 @@ class PDFDocument
for id, ref of @store.objects
offset = ('0000000000' + ref.offset).slice(-10)
out.push offset + ' 00000 n '
-
+
generateTrailer: (out) ->
trailer = PDFObject.convert
Size: @store.length
@@ -146,8 +152,8 @@ class PDFDocument
out.push 'startxref'
out.push @xref_offset
out.push '%%EOF'
-
+
toString: ->
"[object PDFDocument]"
-
-module.exports = PDFDocument
+
+module.exports = PDFDocument
View
20 lib/store.coffee
@@ -9,27 +9,31 @@ class PDFObjectStore
constructor: ->
@objects = {}
@length = 0
-
+
@root = @ref
Type: 'Catalog'
-
+
@root.data['Pages'] = @ref
Type: 'Pages'
Count: 0
Kids: []
-
+
@pages = @root.data['Pages']
-
+
ref: (data) ->
@push ++@length, data
-
+
push: (id, data) ->
ref = new PDFReference(id, data)
@objects[id] = ref
return ref
-
+
addPage: (page) ->
@pages.data['Kids'].push(page.dictionary)
@pages.data['Count']++
-
-module.exports = PDFObjectStore
+
+ removePage: ->
+ @pages.data['Kids'].pop()
+ @pages.data['Count']--
+
+module.exports = PDFObjectStore
Something went wrong with that request. Please try again.