-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
84 lines (67 loc) · 2.18 KB
/
app.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
function App()
{
var self = this;
this.complete = 0;
this.res = '';
this.pdfToText = function(data){
var div = document.getElementById('viewer');
// render the first pages
var pdf = new PDFJS.PDFDoc(data);
var total = pdf.numPages;
for (i = 1; i <= total; i++){
var page = pdf.getPage(i);
var canvas = document.createElement('canvas');
canvas.id = 'page' + i;
canvas.mozOpaque = true;
div.appendChild(canvas);
canvas.width = page.width;
canvas.height = page.height;
var context = canvas.getContext('2d');
context.save();
context.fillStyle = 'rgb(255, 255, 255)';
context.fillRect(0, 0, canvas.width, canvas.height);
context.restore();
self.setMessage("Rendering...");
var textLayer = document.createElement('div');
textLayer.className = 'textLayer';
document.body.appendChild(textLayer);
page.startRendering(context, function(){
if (++self.complete == total){
self.setMessage("Finished rendering. Extracting text...");
window.setTimeout(function()
{
var layers = '';
var nodes = document.querySelectorAll(".textLayer > div");
for (var j = 0; j < nodes.length; j++)
{
layers += nodes[j].textContent + "\n";
}
self.res = layers;
//self.sendOutput(layers);
self.setMessage("Done!");
}, 1000);
}
}, textLayer);
}
};
this.receiveInput = function(event){
if (event.source != parent) return;
if (!event.data.byteLength)
self.setMessage("Received data");
self.pdfToText(event.data);
}
this.sendOutput = function(text){
var recipient = parent.postMessage ? parent : (parent.document.postMessage ? parent.document : undefined);
recipient.postMessage(text, "*");
};
this.getRes = function()
{
return this.res;
}
this.setMessage = function(text){
document.getElementById("message").textContent = text;
}
window.addEventListener("message", self.receiveInput, true);
self.setMessage("Ready");
self.sendOutput("ready");
}