-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.txt
123 lines (92 loc) · 3.22 KB
/
temp.txt
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const express = require('express');
const path=require('path');
const bodyParser=require('body-parser');
const app = express();
app.set('view engine','ejs');
app.set('views',path.join(__dirname,'views'))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
//static file path
app.use(express.static(path.join(__dirname,'public')));
var fs = require('fs');
var pdfjsLib = require('pdfjs-dist');
pdfjsLib.workerSrc ="build/pdf.worker.js";
app.get('/', function (req, res) {
var Canvas = require('canvas-prebuilt');
var assert = require('assert');
function NodeCanvasFactory() {}
NodeCanvasFactory.prototype = {
create: function NodeCanvasFactory_create(width, height) {
assert(width > 0 && height > 0, 'Invalid canvas size');
var canvas = new Canvas(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');
canvasAndContext.canvas.width = 0;
canvasAndContext.canvas.height = 0;
canvasAndContext.canvas = null;
canvasAndContext.context = null;
},
};
// Relative path of the PDF file.
var pdfURL = 'web/test.pdf';
// Read the PDF file into a typed array so PDF.js can load it.
var rawData = new Uint8Array(fs.readFileSync(pdfURL));
// Load the PDF file.
const source = {
data: rawData,
nativeImageDecoderSupport: 'none',
disableFontFace: true
}
pdfjsLib.getDocument(source).then(function (pdfDocument) {
console.log('# PDF document loaded.');
// Get the first page.
pdfDocument.getPage(1).then(function (page) {
// Render the page on a Node canvas with 100% scale.
var viewport = page.getViewport(1.0);
var canvasFactory = new NodeCanvasFactory();
var canvasAndContext = canvasFactory.create(viewport.width, viewport.height);
var renderContext = {
canvasContext: canvasAndContext.context,
viewport: viewport,
canvasFactory: canvasFactory
};
page.render(renderContext).then(function () {
// Convert the canvas to an image buffer.
var image = canvasAndContext.canvas.toBuffer();
fs.writeFile('output.png', image, function (error) {
if (error) {
console.error('Error: ' + error);
} else {
console.log('Finished converting first page of PDF file to a PNG image.');
}
});
});
});
}).catch(function(reason) {
console.log(reason);
});
res.render('index');
});
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
//
// Basic node example that prints document metadata and text content.
// Requires single file built version of PDF.js -- please run
// `gulp singlefile` before running the example.
//
// Loading file from file system into typed array
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});