-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
141 lines (125 loc) · 3.5 KB
/
main.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'use strict';
const {
createWorker
} = Tesseract;
const {
PDFDocument,
degrees,
} = PDFLib;
const form = document.getElementById('form');
const spinner = document.getElementById('spinner');
//pdfjsLib.getDocument('./pdf-rot.pdf').then(init);
//
//function init(pdf) {
// window.pdf = pdf;
//}
//
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://unpkg.com/pdfjs-dist@4.3.136/build/pdf.worker.min.mjs';
console.log('loading doc');
let pdfBuff, pdfDoc;
window.getPDFBuff = () => pdfBuff;
/*
fetch('./pdf-rot.pdf')
.then(a => a.arrayBuffer())
.then(async (a) => {
pdfBuff = new Uint8Array(a);
pdfDoc = await PDFDocument.load(pdfBuff);
return a;
})
.then(a => {
return pdfjsLib.getDocument(a).promise;
}).then(init)
.catch(console.error);
//pdfjsLib.getDocument('./pdf-rot.pdf').promise.then(init).catch(console.error);
*/
function createCanvas(width, height) {
return new OffscreenCanvas(width, height);
const canvas = document.createElement('canvas');
canvas.width = width >> 1;
canvas.height = height >> 1;
document.body.appendChild(canvas);
return canvas;
}
const scale = 4;
async function detectPage(doc, idx) {
const page = await doc.getPage(idx);
const viewport = page.getViewport({
scale
});
console.time(`render${idx}`);
const canvas = createCanvas(viewport.width, viewport.height);
console.log(`size ${canvas.width}x${canvas.height}`);
/** @type {CanvasRenderingContext2D} */
const context = canvas.getContext('2d');
page.render({
canvasContext: context,
viewport: viewport
});
console.timeEnd(`render${idx}`);
//await worker.setParameters('user_defined_dpi', '70', `job-${i}`);
console.time('Tesseract' + idx);
const res = await Tesseract.detect(canvas, `job-${idx}`);
console.log(res.data);
console.timeEnd('Tesseract' + idx);
return res.data;
}
async function init(doc) {
console.log('init');
window.doc = doc;
let canvas, context;
/*
const worker = await createWorker('eng', 1, {
legacyCore: true,
legacyLang: true
});
*/
for (let i = 1; i <= doc.numPages; i++) {
//debugger;
//break;
}
const result = await Promise.all(Array.from({length: doc.numPages}, (_, i) => detectPage(doc, i+1)));
for(let i=0; i<result.length; i++) {
const orientation = result[i];
const page = pdfDoc.getPage(i);
if(orientation.orientation_degrees != 0) {
page.setRotation(degrees(orientation.orientation_degrees));
}
}
const pdfData = await pdfDoc.save();
previewPdf(pdfData);
console.log(result);
console.log(pdfDoc);
window.pdfDoc = pdfDoc
}
function previewPdf(buff) {
const blob = new Blob([buff.buffer], {type: 'application/pdf'});
const url = URL.createObjectURL(blob);
console.log(url);
const obj = document.createElement('embed');
obj.width = window.innerWidth;
obj.height = window.innerHeight;
obj.src = url;
obj.type = 'application/pdf';
document.body.appendChild(obj);
}
function loadFile(f) {
return new Promise((res, rej) => {
const fr = new FileReader();
fr.onload = e => res(e.target.result);
fr.onerror = rej;
fr.readAsArrayBuffer(f);
});
}
form.addEventListener('submit', async(e) => {
e.preventDefault();
console.log('submit!!');
const fileElm = form.elements.file;
const file = fileElm.files[0];
pdfBuff = await loadFile(file);
pdfDoc = await PDFDocument.load(pdfBuff);
console.log(file);
const pdf = await pdfjsLib.getDocument(pdfBuff).promise;
spinner.classList.add('active');
await init(pdf);
spinner.classList.remove('active');
});