forked from hackclub/mail-team-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.js
63 lines (53 loc) · 2.06 KB
/
render.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var pdfjsLib = require('pdfjs-dist');
var Canvas = require('canvas');
var assert = require('assert');
var fs = require('fs');
function NodeCanvasFactory() {}
NodeCanvasFactory.prototype = {
create: function NodeCanvasFactory_create(width, height) {
assert(width > 0 && height > 0, 'Invalid canvas size');
var canvas = Canvas.createCanvas(width, height);
var context = canvas.getContext('2d');
return {
canvas: canvas,
context: context,
};
},
reset: function NodeCanvasFactory_reset(canvasAndContext, width, height) {
assert(canvasAndContext.canvas, 'Canvas is not specified');
assert(width > 0 && height > 0, 'Invalid canvas size');
canvasAndContext.canvas.width = width;
canvasAndContext.canvas.height = height;
},
destroy: function NodeCanvasFactory_destroy(canvasAndContext) {
assert(canvasAndContext.canvas, 'Canvas is not specified');
// Zeroing the width and height cause Firefox to release graphics
// resources immediately, which can greatly reduce memory consumption.
canvasAndContext.canvas.width = 0;
canvasAndContext.canvas.height = 0;
canvasAndContext.canvas = null;
canvasAndContext.context = null;
},
};
module.exports = async function(pdf, pageIndex) {
// Read the PDF file into a typed array so PDF.js can load it.
var rawData = new Uint8Array(pdf);
// Load the PDF file.
var loadingTask = pdfjsLib.getDocument({data: rawData, disableFontFace: false});
var pdfDocument = await loadingTask.promise
// Get the first page.
var page = await pdfDocument.getPage(pageIndex)
// Render the page on a Node canvas with 100% scale.
var viewport = page.getViewport({ scale: 4.0, });
var canvasFactory = new NodeCanvasFactory();
var canvasAndContext = canvasFactory.create(viewport.width, viewport.height);
var renderContext = {
canvasContext: canvasAndContext.context,
viewport: viewport,
canvasFactory: canvasFactory,
};
var renderTask = page.render(renderContext);
await renderTask.promise
var image = canvasAndContext.canvas.toBuffer();
return image
}