From 1c48bad333cd87609affb587cb9e4b6ba1babbf8 Mon Sep 17 00:00:00 2001 From: Edward Faulkner Date: Tue, 9 Sep 2014 21:43:59 -0400 Subject: [PATCH] bufferedPages support --- lib/document.coffee | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/document.coffee b/lib/document.coffee index b86d5e2..cb30fab 100644 --- a/lib/document.coffee +++ b/lib/document.coffee @@ -19,6 +19,9 @@ class PDFDocument extends stream.Readable # Whether streams should be compressed @compress = @options.compress ? yes + @_pageBuffer = [] + @_pageBufferStart = 0 + # The PDF object store @_offsets = [] @_waiting = 0 @@ -76,11 +79,12 @@ class PDFDocument extends stream.Readable addPage: (options = @options) -> # end the current page if needed - @page?.end() - + @flushPages() unless @options.bufferPages + # create a page object @page = new PDFPage(this, options) - + @_pageBuffer.push(@page) + # add the page to the object store pages = @_root.data.Pages.data pages.Kids.push @page.dictionary @@ -96,7 +100,24 @@ class PDFDocument extends stream.Readable @transform 1, 0, 0, -1, 0, @page.height return this - + + bufferedPageRange: -> { start: @_pageBufferStart, count: @_pageBuffer.length } + + switchToPage: (n) -> + unless page = @_pageBuffer[n-@_pageBufferStart] + throw new Error("switchToPage(#{n}) out of bounds, current buffer covers pages #{@_pageBufferStart} to #{@_pageBufferStart + @_pageBuffer.length - 1}") + @page = page + + flushPages: -> + # this local variable exists so we're future-proof against + # reentrant calls to flushPages. + pages = @_pageBuffer + @_pageBuffer = [] + @_pageBufferStart += pages.length + for page in pages + page.end() + null + ref: (data) -> ref = new PDFReference(this, @_offsets.length + 1, data) @_offsets.push null # placeholder for this object's offset once it is finalized @@ -144,8 +165,7 @@ class PDFDocument extends stream.Readable ' end: -> - @page.end() - + @flushPages() @_info = @ref() for key, val of @info if typeof val is 'string'