-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.html
341 lines (310 loc) · 11 KB
/
index.html
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<!DOCTYPE html>
<html>
<head>
<title>pdf2text-ocr</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/pdf.min.js "></script>
<script src="https://cdn.jsdelivr.net/npm/tesseract.js@4/dist/tesseract.min.js"></script>
<script type="module">
pdfjsLib.GlobalWorkerOptions.workerSrc =
"https://cdn.jsdelivr.net/npm/[email protected]/build/pdf.worker.js";
const languages = [
{ name: "English", code: "eng" },
{ name: "Portuguese", code: "por" },
{ name: "Afrikaans", code: "afr" },
{ name: "Albanian", code: "sqi" },
{ name: "Amharic", code: "amh" },
{ name: "Arabic", code: "ara" },
{ name: "Assamese", code: "asm" },
{ name: "Azerbaijani", code: "aze" },
{ name: "Azerbaijani - Cyrillic", code: "aze_cyrl" },
{ name: "Basque", code: "eus" },
{ name: "Belarusian", code: "bel" },
{ name: "Bengali", code: "ben" },
{ name: "Bosnian", code: "bos" },
{ name: "Bulgarian", code: "bul" },
{ name: "Burmese", code: "mya" },
{ name: "Catalan; Valencian", code: "cat" },
{ name: "Cebuano", code: "ceb" },
{ name: "Central Khmer", code: "khm" },
{ name: "Cherokee", code: "chr" },
{ name: "Chinese - Simplified", code: "chi_sim" },
{ name: "Chinese - Traditional", code: "chi_tra" },
{ name: "Croatian", code: "hrv" },
{ name: "Czech", code: "ces" },
{ name: "Danish", code: "dan" },
{ name: "Dutch; Flemish", code: "nld" },
{ name: "Dzongkha", code: "dzo" },
{ name: "English, Middle (1100-1500)", code: "enm" },
{ name: "Esperanto", code: "epo" },
{ name: "Estonian", code: "est" },
{ name: "Finnish", code: "fin" },
{ name: "French", code: "fra" },
{ name: "French, Middle (ca. 1400-1600)", code: "frm" },
{ name: "Galician", code: "glg" },
{ name: "Georgian", code: "kat" },
{ name: "German", code: "deu" },
{ name: "German Fraktur", code: "frk" },
{ name: "Greek, Modern (1453-)", code: "ell" },
{ name: "Greek, Ancient (-1453)", code: "grc" },
{ name: "Gujarati", code: "guj" },
{ name: "Haitian; Haitian Creole", code: "hat" },
{ name: "Hebrew", code: "heb" },
{ name: "Hindi", code: "hin" },
{ name: "Hungarian", code: "hun" },
{ name: "Icelandic", code: "isl" },
{ name: "Indonesian", code: "ind" },
{ name: "Inuktitut", code: "iku" },
{ name: "Irish", code: "gle" },
{ name: "Italian", code: "ita" },
{ name: "Japanese", code: "jpn" },
{ name: "Javanese", code: "jav" },
{ name: "Kannada", code: "kan" },
{ name: "Kazakh", code: "kaz" },
{ name: "Kirghiz; Kyrgyz", code: "kir" },
{ name: "Korean", code: "kor" },
{ name: "Kurdish", code: "kur" },
{ name: "Lao", code: "lao" },
{ name: "Latin", code: "lat" },
{ name: "Latvian", code: "lav" },
{ name: "Lithuanian", code: "lit" },
{ name: "Macedonian", code: "mkd" },
{ name: "Malay", code: "msa" },
{ name: "Malayalam", code: "mal" },
{ name: "Maltese", code: "mlt" },
{ name: "Marathi", code: "mar" },
{ name: "Nepali", code: "nep" },
{ name: "Norwegian", code: "nor" },
{ name: "Oriya", code: "ori" },
{ name: "Panjabi; Punjabi", code: "pan" },
{ name: "Persian", code: "fas" },
{ name: "Polish", code: "pol" },
{ name: "Pushto; Pashto", code: "pus" },
{ name: "Romanian; Moldavian; Moldovan", code: "ron" },
{ name: "Russian", code: "rus" },
{ name: "Sanskrit", code: "san" },
{ name: "Serbian", code: "srp" },
{ name: "Serbian - Latin", code: "srp_latn" },
{ name: "Sinhala; Sinhalese", code: "sin" },
{ name: "Slovak", code: "slk" },
{ name: "Slovenian", code: "slv" },
{ name: "Spanish; Castilian", code: "spa" },
{ name: "Swahili", code: "swa" },
{ name: "Swedish", code: "swe" },
{ name: "Syriac", code: "syr" },
{ name: "Tagalog", code: "tgl" },
{ name: "Tajik", code: "tgk" },
{ name: "Tamil", code: "tam" },
{ name: "Telugu", code: "tel" },
{ name: "Thai", code: "tha" },
{ name: "Tibetan", code: "bod" },
{ name: "Tigrinya", code: "tir" },
{ name: "Turkish", code: "tur" },
{ name: "Uighur; Uyghur", code: "uig" },
{ name: "Ukrainian", code: "ukr" },
{ name: "Urdu", code: "urd" },
{ name: "Uzbek", code: "uzb" },
{ name: "Uzbek - Cyrillic", code: "uzb_cyrl" },
{ name: "Vietnamese", code: "vie" },
{ name: "Welsh", code: "cym" },
{ name: "Yiddish", code: "yid" },
];
let language = localStorage.getItem("language") ?? "eng";
const readFile = (file) => {
return new Promise((resolve) => {
const reader = new FileReader();
reader.addEventListener("loadend", (event) =>
resolve(new Uint8Array(event.target.result))
);
reader.readAsArrayBuffer(file);
});
};
const convertToImage = async (pdf) => {
const container = document.getElementById("container");
const images = [];
for (let pageNumber = 1; pageNumber <= pdf.numPages; pageNumber++) {
const page = await pdf.getPage(pageNumber);
const viewport = page.getViewport({ scale: 1.5 });
const canvas = document.createElement("canvas");
canvas.height = viewport.height;
canvas.width = viewport.width;
container.appendChild(canvas);
await page.render({
canvasContext: canvas.getContext("2d"),
viewport: viewport,
}).promise;
images.push(canvas.toDataURL("image/png"));
}
return images;
};
const convertToText = async (images) => {
const worker = await Tesseract.createWorker();
await worker.loadLanguage(language);
await worker.initialize(language);
const container = document.getElementById("container");
for (const image of images) {
const {
data: { text },
} = await worker.recognize(image);
const section = document.createElement("section");
const pre = document.createElement("pre");
pre.appendChild(document.createTextNode(text));
section.appendChild(pre);
container.appendChild(section);
}
await worker.terminate();
};
const loadFile = async (file) =>
pdfjsLib.getDocument({ data: file }).promise;
const convertFile = async (file) => {
showLoading();
const pdf = await loadFile(file);
const images = await convertToImage(pdf);
await convertToText(images);
hideLoading();
};
const showLoading = () =>
(document.getElementById("loading").style.display = "block");
const hideLoading = () =>
(document.getElementById("loading").style.display = "none");
const showError = (error) =>
document
.getElementById("error")
.appendChild(document.createTextNode(`Error: ${error.message}`));
const clearResults = () => {
["container", "loading", "error"].forEach((id) => {
let element = document.getElementById(id);
element.remove();
element = document.createElement("div");
element.id = id;
if (id === "loading") {
element.appendChild(document.createTextNode("Loading..."));
}
document.body.appendChild(element);
});
};
const languageSelect = document.getElementById("language-select");
languages.forEach((lang) => {
const option = document.createElement("option");
option.appendChild(document.createTextNode(lang.name));
option.value = lang.code;
languageSelect.appendChild(option);
});
languageSelect.value = language;
languageSelect.addEventListener("change", (event) => {
language = event.target.value;
localStorage.setItem("language", language);
});
const fileInput = document.getElementById("file-input");
fileInput.addEventListener("change", async () => {
clearResults();
try {
await convertFile(await readFile(fileInput.files[0]));
} catch (error) {
hideLoading();
showError(error);
}
});
</script>
</head>
<body>
<h1>pdf2text-ocr</h1>
<p>
<a href="https://racosa.github.io/pdf2text-ocr/">pdf2text-ocr</a> is a
simple tool for converting PDF to text using OCR. Files are converted
locally in the browser and are never uploaded to external servers.
</p>
<p>
It can be useful if you are getting gibberish when copying and pasting
text from PDF (<a
href="https://superuser.com/questions/137824/pdf-has-garbled-text-when-copy-pasting"
>example</a
>), specially if you don't want to or cannot use a cloud-based solution.
</p>
<p>
Free and open source:
<a href="https://github.com/racosa/pdf2text-ocr">Github</a>
</p>
<div id="input-container">
<p>Select the language and a file:</p>
<div>
<select name="languages" id="language-select"></select>
</div>
<div>
<label id="file-input-label" for="file-input">Select a file</label
><br />
<input type="file" id="file-input" name="file-input" multiple />
</div>
</div>
<div id="container"></div>
<div id="loading">Loading...</div>
<div id="error"></div>
</body>
<style>
body {
color: #212121;
font-family: Arial, sans-serif;
scroll-behavior: smooth;
font-size: 1.15rem;
line-height: 1.5;
max-width: 850px;
margin: auto;
padding-top: 1rem;
}
a {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
select {
font-size: 1.15rem;
font-family: inherit;
line-height: inherit;
padding-left: 1rem;
}
input[type="file"] {
display: none;
}
select,
#file-input-label {
padding: 0.5em;
border: thin solid grey;
border-radius: 3px;
cursor: pointer;
background-color: #f6f6fb;
}
#input-container {
margin-top: 2rem;
}
#input-container div {
display: inline-block;
margin-right: 1rem;
}
#loading,
#error {
margin-top: 2rem;
margin-bottom: 3rem;
}
#loading {
display: none;
}
section {
border-top: 1px solid #898ea4;
border-bottom: 1px solid #898ea4;
padding: 2rem 1rem;
margin: 3rem 0;
}
section + section,
section:first-child {
border-top: 0;
padding-top: 0;
}
pre {
font-family: Arial, sans-serif;
margin: 0;
}
canvas {
display: none;
}
</style>
</html>