-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf_to_img.html
More file actions
460 lines (389 loc) · 19.1 KB
/
pdf_to_img.html
File metadata and controls
460 lines (389 loc) · 19.1 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>PDF to Image Converter</title>
<!-- PDF.js Libraries -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js"></script>
<style>
:root { --primary: #3b82f6; --bg: #f8fafc; --surface: #ffffff; --border: #e2e8f0; --text: #1e293b; --radius: 12px; }
body { font-family: 'Inter', system-ui, -apple-system, sans-serif; margin: 0; background: var(--bg); color: var(--text); -webkit-tap-highlight-color: transparent; }
/* Layout */
.container { max-width: 900px; margin: 0 auto; padding: 20px; box-sizing: border-box; }
.header { text-align: center; margin-bottom: 25px; }
h2 { margin: 0; color: var(--primary); font-size: 24px; }
p { color: #64748b; font-size: 14px; margin-top: 5px; }
/* 1. Upload Box */
.upload-area {
border: 2px dashed var(--primary);
background: #eff6ff;
border-radius: var(--radius);
padding: 30px 20px;
text-align: center;
cursor: pointer;
transition: 0.2s;
margin-bottom: 20px;
user-select: none;
}
.upload-area:active { transform: scale(0.98); }
.upload-area.has-file { background: #dbeafe; border-style: solid; }
.upload-icon { font-size: 36px; margin-bottom: 10px; display: block; }
.upload-text { font-weight: 600; color: var(--primary); font-size: 16px; }
.upload-sub { font-size: 12px; color: #94a3b8; margin-top:5px; }
input[type="file"] { display: none; }
/* Controls */
.controls { background: var(--surface); padding: 20px; border-radius: var(--radius); box-shadow: 0 2px 5px rgba(0,0,0,0.05); margin-bottom: 20px; }
.label { font-weight: 600; font-size: 14px; display: block; margin-bottom: 10px; }
select { width: 100%; padding: 12px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); font-size: 16px; }
/* Main Action Button */
.action-btn {
width: 100%; padding: 16px;
background: linear-gradient(135deg, var(--primary), #2563eb);
color: white; border: none; border-radius: 50px;
font-size: 16px; font-weight: 700; cursor: pointer;
box-shadow: 0 4px 10px rgba(59,130,246,0.3);
transition: transform 0.1s;
}
.action-btn:active { transform: scale(0.98); }
/* Results Grid */
.results-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
gap: 15px;
margin-top: 30px;
display: none;
}
.results-grid.active { display: grid; }
/* File Card Style (Hidden Image Layout) */
.file-card {
background: var(--surface);
border: 1px solid var(--border);
border-radius: 8px;
padding: 15px;
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
transition: 0.2s;
}
.file-icon { font-size: 36px; margin-bottom: 10px; color: #94a3b8; }
.file-name { font-size: 13px; font-weight: 600; color: var(--text); margin-bottom: 8px; }
/* View Button */
.view-btn {
background: transparent; border: 1px solid var(--primary);
color: var(--primary); border-radius: 4px;
padding: 6px 12px; font-size: 11px; font-weight: 600;
cursor: pointer; width: 100%; margin-bottom: 5px;
transition: background 0.2s;
}
.view-btn:hover { background: #eff6ff; }
/* Download Button */
.dl-mini-btn {
background: var(--primary); color: white; border: none;
padding: 8px 12px; border-radius: 6px; cursor: pointer;
font-size: 12px; font-weight: 600; width: 100%;
}
/* Info Section */
.info-section {
background: var(--surface); padding: 20px; border-radius: var(--radius);
margin-top: 30px; box-shadow: 0 2px 5px rgba(0,0,0,0.05); border: 1px solid #f1f5f9;
}
.info-section h3 { margin-top: 0; margin-bottom: 10px; font-size: 16px; color: var(--text); }
.info-section ol { padding-left: 20px; color: #64748b; font-size: 13px; line-height: 1.6; margin-bottom: 15px; }
.tutorial-img { width: 100%; height: auto; border-radius: 8px; border: 1px solid #e2e8f0; display: block; }
/* Modals */
.modal-overlay {
position: fixed; top: 0; left: 0; width: 100%; height: 100%;
background: rgba(0,0,0,0.85); backdrop-filter: blur(4px);
display: none; align-items: center; justify-content: center; z-index: 100;
}
.modal-overlay.active { display: flex; }
.modal-box {
background: white; width: 90%; max-width: 400px;
border-radius: 16px; padding: 25px; max-height: 85vh;
display: flex; flex-direction: column; text-align: center; position: relative;
}
/* Progress Bar */
.progress-container { width: 100%; margin: 15px 0; }
.progress-track { height: 8px; background: #e2e8f0; border-radius: 4px; overflow: hidden; }
.progress-fill { height: 100%; background: var(--primary); width: 0%; transition: width 0.2s; }
.progress-text { text-align: center; font-size: 12px; color: #64748b; margin-top: 5px; }
/* Custom Alert Styles */
.alert-icon { font-size: 40px; margin-bottom: 10px; display: block; }
.alert-btns { display: flex; gap: 10px; margin-top: 20px; justify-content: center; width: 100%; }
.btn-secondary { background: transparent; border: 1px solid #cbd5e1; color: #64748b; padding: 12px 20px; border-radius: 50px; font-weight: 600; cursor: pointer; flex: 1; }
/* Image Viewer */
.viewer-wrapper { text-align: center; position: relative; width: 100%; max-width: 900px; }
.viewer-img { max-width: 100%; max-height: 85vh; border-radius: 8px; box-shadow: 0 5px 25px rgba(0,0,0,0.5); }
.viewer-close {
position: absolute; top: -40px; right: 0; color: white;
font-size: 30px; cursor: pointer; font-weight: bold; background: none; border: none;
}
/* Password Input Style */
.password-input {
width: 100%; box-sizing: border-box; padding: 12px;
border: 2px solid #e2e8f0; border-radius: 8px;
font-size: 16px; text-align: center; margin: 10px 0;
outline: none; transition: 0.2s;
}
.password-input:focus { border-color: var(--primary); }
.error-text { color: #ef4444; font-size: 13px; margin-top: 5px; font-weight: 600; display: none; }
/* Password Hints Box */
.password-hint {
background: #f1f5f9; border: 1px solid #e2e8f0; border-radius: 8px;
padding: 12px; margin-bottom: 10px; text-align: left;
}
.hint-title { font-size: 12px; font-weight: 700; color: var(--text); margin-bottom: 4px; }
.hint-list { margin: 0; padding-left: 15px; color: #64748b; font-size: 11px; line-height: 1.5; }
.hint-list li { margin-bottom: 2px; }
</style>
</head>
<body>
<div class="container">
<div class="header">
<h2>PDF to Image Converter</h2>
<p>Extract pages from PDF as high-quality PNGs.</p>
</div>
<!-- Upload Area -->
<div class="upload-area" id="uploadBox" onclick="document.getElementById('fileInput').click()">
<span class="upload-icon">📄</span>
<div class="upload-text" id="uploadText">Tap to Select PDF</div>
<div class="upload-sub">All processing happens on your device</div>
</div>
<input type="file" id="fileInput" accept="application/pdf" onchange="handleFileSelect(this)">
<!-- Settings -->
<div class="controls">
<span class="label">Image Quality (Scale)</span>
<select id="scaleSelect">
<option value="1">Standard (72 DPI)</option>
<option value="1.5" selected>High (108 DPI)</option>
<option value="2">Very High (144 DPI)</option>
<option value="3">Ultra (216 DPI)</option>
</select>
</div>
<button class="action-btn" onclick="startConversion()">Convert PDF to Images</button>
<!-- Results Grid -->
<div id="resultsGrid" class="results-grid"></div>
<!-- How To Use Section -->
<div class="info-section">
<h3>How to Use</h3>
<ol>
<li><strong>Select PDF:</strong> Tap the box to upload your file (Password protected files supported).</li>
<li><strong>Choose Quality:</strong> Select Standard for speed or Ultra for high-resolution images.</li>
<li><strong>Convert:</strong> Click the button to process all pages.</li>
<li><strong>Download:</strong> Save individual pages or view them instantly.</li>
</ol>
<img src="https://raw.githubusercontent.com/mrbtool/File-maker/refs/heads/main/Picsart_26-01-28_20-41-43-965.png" alt="How to use tutorial" class="tutorial-img">
</div>
</div>
<!-- Progress Modal -->
<div id="loadingModal" class="modal-overlay">
<div class="modal-box">
<h3 style="margin:0 0 10px 0;">Converting...</h3>
<div class="progress-container">
<div class="progress-track"><div id="pFill" class="progress-fill"></div></div>
<div id="pText" class="progress-text">0%</div>
</div>
<div style="font-size:12px; color:#94a3b8;">Rendering pages...</div>
</div>
</div>
<!-- Custom Alert Modal -->
<div id="alertModal" class="modal-overlay">
<div class="modal-box">
<div class="alert-icon">⚠️</div>
<h3 id="alertTitle" style="margin:0 0 10px 0;">Notice</h3>
<p id="alertMsg" style="color:#64748b; margin:0; font-size:14px; line-height:1.5;"></p>
<div class="alert-btns">
<button class="action-btn" onclick="closeAlert()">OK</button>
</div>
</div>
</div>
<!-- Password Modal -->
<div id="passwordModal" class="modal-overlay">
<div class="modal-box">
<div class="alert-icon">🔒</div>
<h3 style="margin:0 0 5px 0;">Encrypted PDF</h3>
<p style="color:#64748b; margin:0 0 15px 0; font-size:14px;">This file is password protected.</p>
<!-- Password Hints -->
<div class="password-hint">
<div class="hint-title">Common Passwords:</div>
<ul class="hint-list">
<li><strong>Aadhar:</strong> First 4 letters of Name (CAPS) + Year (e.g. ANIS1999)</li>
<li><strong>PAN:</strong> Date of Birth (DDMMYYYY)</li>
</ul>
</div>
<input type="password" id="pdfPassword" class="password-input" placeholder="Enter Password">
<div id="passwordError" class="error-text">Incorrect password. Try again.</div>
<div class="alert-btns">
<button class="btn-secondary" onclick="cancelPassword()">Cancel</button>
<button class="action-btn" onclick="submitPassword()">Unlock</button>
</div>
</div>
</div>
<!-- Image Viewer Modal -->
<div id="imageViewerModal" class="modal-overlay" onclick="closeViewer()">
<div class="viewer-wrapper" onclick="event.stopPropagation()">
<button class="viewer-close" onclick="closeViewer()">×</button>
<img id="viewerImage" class="viewer-img" src="">
<div id="viewerName" style="color:white; margin-top:10px; font-size:14px;"></div>
</div>
</div>
<script>
// Initialize PDF.js worker
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.js';
let selectedFile = null;
let passwordCallback = null; // To store the function to call with password
// --- Custom Alert Logic ---
function showAlert(msg) {
document.getElementById('alertMsg').innerText = msg;
document.getElementById('alertModal').classList.add('active');
}
function closeAlert() {
document.getElementById('alertModal').classList.remove('active');
}
// --- File Handling ---
function handleFileSelect(input) {
if (input.files.length === 0) return;
selectedFile = input.files[0];
// Update UI to show selection
const box = document.getElementById('uploadBox');
const text = document.getElementById('uploadText');
box.classList.add('has-file');
text.innerText = selectedFile.name;
// Clear previous results
document.getElementById('resultsGrid').classList.remove('active');
document.getElementById('resultsGrid').innerHTML = '';
}
// --- Password Handling ---
function submitPassword() {
const pass = document.getElementById('pdfPassword').value;
if (passwordCallback) {
// Hide password modal and show loading again (briefly, until logic decides what to do)
document.getElementById('passwordModal').classList.remove('active');
document.getElementById('loadingModal').classList.add('active');
passwordCallback(pass);
}
}
function cancelPassword() {
document.getElementById('passwordModal').classList.remove('active');
document.getElementById('loadingModal').classList.remove('active');
document.getElementById('fileInput').value = ''; // Reset input
passwordCallback = null;
}
// Add Enter key support
document.getElementById('pdfPassword').addEventListener("keypress", function(event) {
if (event.key === "Enter") {
event.preventDefault();
submitPassword();
}
});
// --- Conversion Logic ---
async function startConversion() {
if (!selectedFile) {
showAlert("Please select a PDF file first.");
return;
}
const scale = parseFloat(document.getElementById('scaleSelect').value);
// Show Loading
document.getElementById('loadingModal').classList.add('active');
const pFill = document.getElementById('pFill');
const pText = document.getElementById('pText');
pFill.style.width = '0%';
pText.innerText = 'Initializing...';
try {
// Read PDF as ArrayBuffer
const arrayBuffer = await selectedFile.arrayBuffer();
// Create Loading Task
const loadingTask = pdfjsLib.getDocument(arrayBuffer);
// Hook into Password Request
loadingTask.onPassword = function(callback, reason) {
// If password needed
document.getElementById('loadingModal').classList.remove('active'); // Hide loading
document.getElementById('passwordModal').classList.add('active'); // Show password prompt
// Reset fields
document.getElementById('pdfPassword').value = '';
document.getElementById('passwordError').style.display = 'none';
document.getElementById('pdfPassword').focus();
// Check if this is a retry due to incorrect password
if (reason === pdfjsLib.PasswordResponses.INCORRECT_PASSWORD) {
document.getElementById('passwordError').style.display = 'block';
}
// Store callback
passwordCallback = callback;
};
const pdf = await loadingTask.promise;
const totalPages = pdf.numPages;
const grid = document.getElementById('resultsGrid');
grid.innerHTML = '';
grid.classList.add('active');
// Hide password modal just in case it was open
document.getElementById('passwordModal').classList.remove('active');
// Ensure loading modal is visible again for the rendering part
document.getElementById('loadingModal').classList.add('active');
for (let i = 1; i <= totalPages; i++) {
// Update Progress
const pct = Math.round(((i) / totalPages) * 100);
pFill.style.width = pct + '%';
pText.innerText = `Processing page ${i} of ${totalPages}`;
// Render Page
const page = await pdf.getPage(i);
const viewport = page.getViewport({ scale: scale });
const canvas = document.createElement('canvas');
canvas.width = viewport.width;
canvas.height = viewport.height;
const ctx = canvas.getContext('2d');
await page.render({ canvasContext: ctx, viewport: viewport }).promise;
// Create Data URL
const imgUrl = canvas.toDataURL('image/png');
const fileName = `page_${i}.png`;
// Create Result Card (No Image Preview for speed)
const card = document.createElement('div');
card.className = 'file-card';
card.innerHTML = `
<div class="file-icon">🖼️</div>
<div class="file-name">Page ${i}</div>
<button class="view-btn" onclick="openViewer('${imgUrl}', ${i})">View Image</button>
<button class="dl-mini-btn" onclick="downloadImage('${imgUrl}', '${fileName}')">Download</button>
`;
grid.appendChild(card);
// Small delay to allow UI to breathe
await new Promise(r => setTimeout(r, 50));
}
// Finish
pFill.style.width = '100%';
pText.innerText = 'Done!';
setTimeout(() => {
document.getElementById('loadingModal').classList.remove('active');
// Optional: Scroll to results
grid.scrollIntoView({ behavior: 'smooth' });
}, 500);
} catch (error) {
console.error(error);
document.getElementById('loadingModal').classList.remove('active');
// Only show alert if it's not a password error (handled by onPassword)
if (error.name !== 'PasswordException') {
showAlert("Error parsing PDF. It might be corrupted.");
}
}
}
// --- Helpers ---
function downloadImage(dataUrl, fileName) {
saveAs(dataUrl, fileName);
}
function openViewer(url, pageNum) {
document.getElementById('viewerImage').src = url;
document.getElementById('viewerName').innerText = `Page ${pageNum}`;
document.getElementById('imageViewerModal').classList.add('active');
}
function closeViewer() {
document.getElementById('imageViewerModal').classList.remove('active');
document.getElementById('viewerImage').src = '';
}
</script>
<script src="tool-ads.js"></script>
</body>
</html>