|
|
@ -29,22 +29,22 @@ NodeCanvasFactory.prototype = { |
|
|
|
}; |
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
reset: function NodeCanvasFactory_reset(canvasAndContextPair, width, height) { |
|
|
|
reset: function NodeCanvasFactory_reset(canvasAndContext, width, height) { |
|
|
|
assert(canvasAndContextPair.canvas, 'Canvas is not specified'); |
|
|
|
assert(canvasAndContext.canvas, 'Canvas is not specified'); |
|
|
|
assert(width > 0 && height > 0, 'Invalid canvas size'); |
|
|
|
assert(width > 0 && height > 0, 'Invalid canvas size'); |
|
|
|
canvasAndContextPair.canvas.width = width; |
|
|
|
canvasAndContext.canvas.width = width; |
|
|
|
canvasAndContextPair.canvas.height = height; |
|
|
|
canvasAndContext.canvas.height = height; |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
destroy: function NodeCanvasFactory_destroy(canvasAndContextPair) { |
|
|
|
destroy: function NodeCanvasFactory_destroy(canvasAndContext) { |
|
|
|
assert(canvasAndContextPair.canvas, 'Canvas is not specified'); |
|
|
|
assert(canvasAndContext.canvas, 'Canvas is not specified'); |
|
|
|
|
|
|
|
|
|
|
|
// Zeroing the width and height cause Firefox to release graphics
|
|
|
|
// Zeroing the width and height cause Firefox to release graphics
|
|
|
|
// resources immediately, which can greatly reduce memory consumption.
|
|
|
|
// resources immediately, which can greatly reduce memory consumption.
|
|
|
|
canvasAndContextPair.canvas.width = 0; |
|
|
|
canvasAndContext.canvas.width = 0; |
|
|
|
canvasAndContextPair.canvas.height = 0; |
|
|
|
canvasAndContext.canvas.height = 0; |
|
|
|
canvasAndContextPair.canvas = null; |
|
|
|
canvasAndContext.canvas = null; |
|
|
|
canvasAndContextPair.context = null; |
|
|
|
canvasAndContext.context = null; |
|
|
|
}, |
|
|
|
}, |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
@ -65,16 +65,16 @@ pdfjsLib.getDocument(rawData).then(function (pdfDocument) { |
|
|
|
// Render the page on a Node canvas with 100% scale.
|
|
|
|
// Render the page on a Node canvas with 100% scale.
|
|
|
|
var viewport = page.getViewport(1.0); |
|
|
|
var viewport = page.getViewport(1.0); |
|
|
|
var canvasFactory = new NodeCanvasFactory(); |
|
|
|
var canvasFactory = new NodeCanvasFactory(); |
|
|
|
var canvasAndContextPair = canvasFactory.create(viewport.width, viewport.height); |
|
|
|
var canvasAndContext = canvasFactory.create(viewport.width, viewport.height); |
|
|
|
var renderContext = { |
|
|
|
var renderContext = { |
|
|
|
canvasContext: canvasAndContextPair.context, |
|
|
|
canvasContext: canvasAndContext.context, |
|
|
|
viewport: viewport, |
|
|
|
viewport: viewport, |
|
|
|
canvasFactory: canvasFactory |
|
|
|
canvasFactory: canvasFactory |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
page.render(renderContext).then(function () { |
|
|
|
page.render(renderContext).then(function () { |
|
|
|
// Convert the canvas to an image buffer.
|
|
|
|
// Convert the canvas to an image buffer.
|
|
|
|
image = canvasAndContextPair.canvas.toBuffer(); |
|
|
|
var image = canvasAndContext.canvas.toBuffer(); |
|
|
|
fs.writeFile('output.png', image, function (error) { |
|
|
|
fs.writeFile('output.png', image, function (error) { |
|
|
|
if (error) { |
|
|
|
if (error) { |
|
|
|
console.error('Error: ' + error); |
|
|
|
console.error('Error: ' + error); |
|
|
|