-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
232 lines (194 loc) · 8.76 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="PDF2Text OCR Tool for converting scanned PDFs and images to text using optical character recognition. Free and easy-to-use tool for developers and users alike." />
<meta name="keywords" content="PDF to text, OCR tool, optical character recognition, image OCR, pdfjs, tesseract.js, text extraction, pdf text converter" />
<meta name="author" content="Azozz ALFiras" />
<meta name="email" content="dev@3zozz.com" />
<meta name="github" content="https://github.com/AzozzALFiras/Pdf2Text-OCR" />
<title>PDF2Text OCR Tool</title>
<script src="https://cdn.jsdelivr.net/npm/pdfjs-dist@3.6.172/build/pdf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/tesseract.js@4/dist/tesseract.min.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.4.0/jspdf.umd.min.js"></script>
<style>
.spinner {
border: 8px solid #f3f3f3;
border-top: 8px solid #3498db;
border-radius: 50%;
width: 50px;
height: 50px;
animation: spin 1s linear infinite;
margin: auto;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
</head>
<body class="bg-gray-50 text-gray-800 font-sans max-w-4xl mx-auto p-6">
<h1 class="text-2xl font-bold mb-4">PDF2Text OCR Tool</h1>
<p>A simple tool for converting PDFs and images to text using OCR. Files are processed locally in the browser.</p>
<div class="mt-4">
<label for="language-select" class="block text-lg font-medium mb-2">Select Language:</label>
<select id="language-select" class="block w-full p-2 border border-gray-300 rounded-md mb-4"></select>
<input type="file" id="file-input" class="hidden" />
<label for="file-input" class="block cursor-pointer py-2 px-4 bg-blue-500 text-white text-center rounded-md hover:bg-blue-600">Select PDF File</label>
<input type="file" id="image-input" accept="image/*" class="hidden" />
<label for="image-input" class="block cursor-pointer py-2 px-4 bg-blue-500 text-white text-center rounded-md hover:bg-blue-600 mt-4">Select Image File</label>
</div>
<div id="results-container" class="mt-4 p-4 bg-white rounded-md shadow-md" style="min-height: 200px; max-height: 400px; overflow-y: auto;"></div>
<div id="loading" class="hidden text-center mt-4">
<div class="spinner"></div>
<p class="mt-2 text-blue-500">Processing... Please wait.</p>
</div>
<div class="mt-4">
<button id="copy-button" class="hidden py-2 px-4 bg-green-500 text-white rounded-md hover:bg-green-600">Copy Text</button>
<button id="download-button" class="hidden py-2 px-4 bg-red-500 text-white rounded-md hover:bg-red-600">Download as PDF</button>
</div>
<footer class="mt-8 text-center text-gray-500">
© <span id="year"></span> PDF2Text OCR Tool. All rights reserved.
</footer>
<script>
document.getElementById("year").textContent = new Date().getFullYear();
pdfjsLib.GlobalWorkerOptions.workerSrc = "https://cdn.jsdelivr.net/npm/pdfjs-dist@3.6.172/build/pdf.worker.js";
const languages = [
{ name: "English", code: "eng" },
{ name: "Arabic", code: "ara" },
{ name: "Spanish", code: "spa" },
{ name: "French", code: "fra" },
{ name: "German", code: "deu" },
{ name: "Chinese", code: "chi_sim" },
{ name: "Japanese", code: "jpn" },
{ name: "Russian", code: "rus" },
{ name: "Italian", code: "ita" },
{ name: "Portuguese", code: "por" }
];
let selectedLanguage = localStorage.getItem("language") || "eng";
const readFile = (file) => new Promise((resolve) => {
const reader = new FileReader();
reader.onload = (e) => resolve(new Uint8Array(e.target.result));
reader.readAsArrayBuffer(file);
});
const convertPdfToImages = async (pdf) => {
const images = [];
for (let i = 1; i <= pdf.numPages; i++) {
const page = await pdf.getPage(i);
const viewport = page.getViewport({ scale: 1.5 });
const canvas = document.createElement("canvas");
canvas.width = viewport.width;
canvas.height = viewport.height;
await page.render({
canvasContext: canvas.getContext("2d"),
viewport,
}).promise;
images.push(canvas.toDataURL("image/png"));
}
return images;
};
const extractTextFromImages = async (images) => {
const worker = await Tesseract.createWorker();
await worker.loadLanguage(selectedLanguage);
await worker.initialize(selectedLanguage);
const resultsContainer = document.getElementById("results-container");
let fullText = "";
for (const image of images) {
const { data: { text } } = await worker.recognize(image);
fullText += text + "\n";
const paragraph = document.createElement("p");
paragraph.textContent = text;
paragraph.className = "mb-2";
resultsContainer.appendChild(paragraph);
}
await worker.terminate();
return fullText;
};
const processFile = async (file) => {
document.getElementById("loading").classList.remove("hidden");
const resultsContainer = document.getElementById("results-container");
resultsContainer.innerHTML = "";
try {
const arrayBuffer = await readFile(file);
const pdf = await pdfjsLib.getDocument({ data: arrayBuffer }).promise;
const images = await convertPdfToImages(pdf);
const extractedText = await extractTextFromImages(images);
document.getElementById("loading").classList.add("hidden");
document.getElementById("copy-button").classList.remove("hidden");
document.getElementById("download-button").classList.remove("hidden");
document.getElementById("copy-button").onclick = () => {
navigator.clipboard.writeText(extractedText).then(() => alert("Text copied to clipboard!"));
};
document.getElementById("download-button").onclick = () => {
const { jsPDF } = window.jspdf;
const pdf = new jsPDF();
pdf.text(extractedText, 10, 10);
pdf.save("extracted_text.pdf");
};
} catch (error) {
document.getElementById("loading").classList.add("hidden");
alert("Error: " + error.message);
}
};
const processImage = async (file) => {
document.getElementById("loading").classList.remove("hidden");
const resultsContainer = document.getElementById("results-container");
resultsContainer.innerHTML = "";
try {
const imageURL = URL.createObjectURL(file);
const worker = await Tesseract.createWorker();
await worker.loadLanguage(selectedLanguage);
await worker.initialize(selectedLanguage);
const { data: { text } } = await worker.recognize(imageURL);
resultsContainer.innerHTML = `<p>${text}</p>`;
document.getElementById("loading").classList.add("hidden");
document.getElementById("copy-button").classList.remove("hidden");
document.getElementById("download-button").classList.remove("hidden");
document.getElementById("copy-button").onclick = () => {
navigator.clipboard.writeText(text).then(() => alert("Text copied to clipboard!"));
};
document.getElementById("download-button").onclick = () => {
const { jsPDF } = window.jspdf;
const pdf = new jsPDF();
pdf.text(text, 10, 10);
pdf.save("extracted_text.pdf");
};
} catch (error) {
document.getElementById("loading").classList.add("hidden");
alert("Error: " + error.message);
}
};
document.getElementById("file-input").addEventListener("change", async (event) => {
const file = event.target.files[0];
if (file) {
await processFile(file);
}
});
document.getElementById("image-input").addEventListener("change", async (event) => {
const file = event.target.files[0];
if (file) {
await processImage(file);
}
});
const languageSelect = document.getElementById("language-select");
languages.forEach(({ name, code }) => {
const option = document.createElement("option");
option.value = code;
option.textContent = name;
languageSelect.appendChild(option);
});
languageSelect.value = selectedLanguage;
languageSelect.addEventListener("change", (event) => {
selectedLanguage = event.target.value;
localStorage.setItem("language", selectedLanguage);
});
</script>
</body>
</html>