From 6e7b57a0d80b1e10754fa5fc5b84d65622a2fe77 Mon Sep 17 00:00:00 2001 From: Thomas Parslow Date: Thu, 24 Oct 2013 09:51:52 +0100 Subject: [PATCH] Have output return a binary buffer Right now output returns a string to its callback, this means you can't just write it to a file or send it to a response as the default encoding (utf8) is incorrect. You have to manually specify binary. If instead of a string you return a binary buffer then you don't have to worry about this. I also fixed the generation of the final string to not build the string with repeated concatenation which is O(n^2) and instead build up an array of strings then join them all at once which is O(n) --- lib/document.coffee | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/document.coffee b/lib/document.coffee index 6b97659..6137e54 100644 --- a/lib/document.coffee +++ b/lib/document.coffee @@ -98,11 +98,11 @@ class PDFDocument @generateXRef out @generateTrailer out - ret = '' + ret = [] for k in out - ret += k + '\n' + ret.push(k + '\n') - fn ret + fn new Buffer(ret.join(''),'binary') finalize: (fn) -> # convert strings in the info dictionary to literals @@ -168,4 +168,4 @@ class PDFDocument toString: -> "[object PDFDocument]" -module.exports = PDFDocument \ No newline at end of file +module.exports = PDFDocument