Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
540 lines (461 loc) · 28.5 KB
/
Copy pathindex.html
File metadata and controls
540 lines (461 loc) · 28.5 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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shadow PDF (Offline)</title>
<!-- STYLES: Tailwind CSS Local -->
<link rel="stylesheet" href="lib/css/tailwind.min.css">
<!-- LIBRARIES: React Local -->
<script src="lib/js/react.production.min.js"></script>
<script src="lib/js/react-dom.production.min.js"></script>
<!-- LIBRARIES: Babel Local -->
<script src="lib/js/babel.min.js"></script>
<!-- LIBRARIES: PDF.js Local -->
<script src="lib/js/pdf.min.js"></script>
<script>
pdfjsLib.GlobalWorkerOptions.workerSrc = 'lib/js/pdf.worker.min.js';
</script>
<!-- LIBRARIES: Turndown Local -->
<script src="lib/js/turndown.js"></script>
<style>
/* Colour Personalization */
/* Since we are using static CSS instead of a JS configurator,
we define here the colors of the theme used in the app */
.bg-primary { background-color: #10B981 !important; } /* Emerald 500 */
.hover\:bg-green-600:hover { background-color: #059669 !important; }
.text-primary { color: #10B981 !important; }
.border-primary { border-color: #10B981 !important; }
.bg-secondary { background-color: #F97316 !important; } /* Orange 500 */
.hover\:bg-orange-600:hover { background-color: #EA580C !important; }
.text-secondary { color: #F97316 !important; }
.bg-surfaceLight { background-color: #ffffff !important; }
.bg-surfaceDark { background-color: #1f2937 !important; } /* Gray 800 */
.bg-bgLight { background-color: #f3f4f6 !important; } /* Gray 100 */
.bg-bgDark { background-color: #111827 !important; } /* Gray 900 */
/* Scrollbar customizations for preview */
.preview-scroll::-webkit-scrollbar {
width: 8px;
}
.preview-scroll::-webkit-scrollbar-track {
background: rgba(0,0,0,0.1);
border-radius: 4px;
}
.preview-scroll::-webkit-scrollbar-thumb {
background: rgba(0,0,0,0.2);
border-radius: 4px;
}
.dark .preview-scroll::-webkit-scrollbar-track {
background: rgba(255,255,255,0.1);
}
.dark .preview-scroll::-webkit-scrollbar-thumb {
background: rgba(255,255,255,0.2);
}
/* Animazione drag & drop */
.drag-active {
border-color: #10B981 !important;
background-color: rgba(16, 185, 129, 0.1) !important;
transform: scale(1.02);
}
/* GLASSMORPHISM EFFECT */
.glassmorphism {
background: rgba(255, 255, 255, 0.1) !important;
backdrop-filter: blur(10px) !important;
-webkit-backdrop-filter: blur(10px) !important;
border: 1px solid rgba(255, 255, 255, 0.2) !important;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}
.dark .glassmorphism {
background: rgba(31, 41, 55, 0.3) !important;
border: 1px solid rgba(255, 255, 255, 0.1) !important;
}
/* Footer Styles */
.footer-link {
display: inline-flex;
align-items: center;
gap: 0.5rem;
color: #10B981;
text-decoration: none;
transition: all 0.3s ease;
font-size: 0.875rem;
}
.footer-link:hover {
color: #059669;
transform: translateY(-2px);
}
.dark .footer-link {
color: #10B981;
}
.dark .footer-link:hover {
color: #34D399;
}
.social-icon {
width: 20px;
height: 20px;
display: inline-block;
}
.footer-divider {
color: #D1D5DB;
}
.dark .footer-divider {
color: #4B5563;
}
</style>
</head>
<body class="transition-colors duration-300">
<div id="root"></div>
<script type="text/babel">
const { useState, useEffect, useRef } = React;
// --- DICTIONARY OF LANGUAGES ---
const translations = {
en: {
title: "- Shadow PDF -",
dropTitle: "Drag & Drop your PDFs here",
dropSubtitle: "or click the button below",
uploadBtn: "Upload PDF",
formatLabel: "Output Format:",
processing: "Processing...",
previewTitle: "Merged Document Preview",
resetBtn: "Load more PDFs",
downloadBtn: "Download Document",
fileCount: "files selected",
error: "Error reading file",
formats: {
txt: "Plain Text (.txt)",
md: "Markdown (.md)",
html: "HTML (.html)"
}
}
};
// --- MAIN COMPONENT ---
function App() {
// States of the application
const [lang, setLang] = useState('en');
const [theme, setTheme] = useState('light');
const [step, setStep] = useState('input'); // input, processing, result
const [files, setFiles] = useState([]);
const [outputFormat, setOutputFormat] = useState('txt');
const [progress, setProgress] = useState(0);
const [finalText, setFinalText] = useState('');
const [isDragging, setIsDragging] = useState(false);
const fileInputRef = useRef(null);
// 1. Detection of Language and Theme at Startup
useEffect(() => {
// Language
const userLang = navigator.language || navigator.userLanguage;
setLang(userLang.startsWith('it') ? 'it' : 'en');
// Theme
const darkModeQuery = window.matchMedia('(prefers-color-scheme: dark)');
const updateTheme = (e) => {
const isDark = e.matches;
setTheme(isDark ? 'dark' : 'light');
if (isDark) {
document.documentElement.classList.add('dark');
document.body.classList.add('bg-bgDark');
document.body.classList.remove('bg-bgLight');
} else {
document.documentElement.classList.remove('dark');
document.body.classList.add('bg-bgLight');
document.body.classList.remove('bg-bgDark');
}
};
// Set initial state and listener
updateTheme(darkModeQuery);
darkModeQuery.addEventListener('change', updateTheme);
return () => darkModeQuery.removeEventListener('change', updateTheme);
}, []);
const t = translations[lang];
// --- HANDLING DRAG & DROP ---
const handleDragOver = (e) => {
e.preventDefault();
setIsDragging(true);
};
const handleDragLeave = (e) => {
e.preventDefault();
setIsDragging(false);
};
const handleDrop = (e) => {
e.preventDefault();
setIsDragging(false);
if (e.dataTransfer.files && e.dataTransfer.files.length > 0) {
const pdfFiles = Array.from(e.dataTransfer.files).filter(f => f.type === "application/pdf" || f.name.endsWith('.pdf'));
if (pdfFiles.length > 0) {
startProcessing(pdfFiles);
}
}
};
const handleFileSelect = (e) => {
if (e.target.files && e.target.files.length > 0) {
const pdfFiles = Array.from(e.target.files);
startProcessing(pdfFiles);
}
};
// --- PDF EXTRACTION LOGIC (The heart of the system) ---
const startProcessing = async (selectedFiles) => {
setFiles(selectedFiles);
setStep('processing');
setProgress(0);
let extractedContent = "";
let totalPages = 0;
const renderPageImage = async (page) => {
const scale = 1.25;
const viewport = page.getViewport({ scale });
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.width = viewport.width;
canvas.height = viewport.height;
await page.render({ canvasContext: context, viewport }).promise;
return canvas.toDataURL('image/png');
};
try {
for (let i = 0; i < selectedFiles.length; i++) {
const file = selectedFiles[i];
const arrayBuffer = await file.arrayBuffer();
// Upload the PDF document
const pdf = await pdfjsLib.getDocument({ data: arrayBuffer }).promise;
// File header
if (outputFormat === 'html') {
extractedContent += `<h1>File: ${file.name}</h1>\n<hr>\n`;
} else if (outputFormat === 'md') {
extractedContent += `# File: ${file.name}\n\n---\n\n`;
} else {
extractedContent += `\n\n--- FILE: ${file.name} ---\n\n`;
}
// Iterate through the pages
for (let pageNum = 1; pageNum <= pdf.numPages; pageNum++) {
const page = await pdf.getPage(pageNum);
if (outputFormat === 'html' || outputFormat === 'md') {
const pageImage = await renderPageImage(page);
if (outputFormat === 'html') {
extractedContent += `<section class="pdf-page" style="margin-bottom:40px;"><div style="margin-bottom:18px;"><strong>Page ${pageNum}</strong></div><figure style="margin: 24px 0; text-align:center;"><img src="${pageImage}" alt="${file.name} - Page ${pageNum}" style="max-width:100%; height:auto; display:block; margin:0 auto;" /><figcaption style="color:#6b7280; font-size:0.95rem; margin-top:0.5rem;">Page ${pageNum}</figcaption></figure></section>`;
} else {
extractedContent += `### Page ${pageNum}\n\n\n\n`;
}
} else {
const cleanTextItem = (value) => {
return value
.replace(/[•‣◦▪▫◆◇●]/g, '')
.replace(/[\u2022\u2023\u25E6\u25AA\u25AB\u2219\u2218\u2024\u2027\u25CF\u25CB\u25CE\u25E7]/g, '')
.replace(/[\s\u00A0]+/g, ' ')
.trim();
};
const shouldKeepLine = (line) => {
const cleanLine = line.trim();
if (!cleanLine) return false;
if (/\b(Column|Columns|Row|Rows)\b/i.test(cleanLine)) return false;
if (/^(?:\d+[\s,;:\-]*){2,}\d+$/.test(cleanLine)) return false;
if (/^[\s\d\-.,]+$/.test(cleanLine) && /\d/.test(cleanLine)) return false;
return true;
};
const textContent = await page.getTextContent({ normalizeWhitespace: true, disableCombineTextItems: false });
let pageText = "";
let lastY = -1;
const items = (textContent.items || []).sort((a, b) => {
if (Math.abs(a.transform[5] - b.transform[5]) > 5) {
return b.transform[5] - a.transform[5];
}
return a.transform[4] - b.transform[4];
});
let isFirstItem = true;
items.forEach(item => {
const rawText = cleanTextItem(item.str);
if (!rawText) return;
if (!isFirstItem && lastY !== -1 && Math.abs(item.transform[5] - lastY) > 10) {
pageText += "\n";
}
pageText += rawText + " ";
lastY = item.transform[5];
isFirstItem = false;
});
pageText = pageText.replace(/[ \t]+/g, ' ').replace(/\s*\n\s*/g, '\n').trim();
pageText = pageText
.split('\n')
.map(line => line.trim())
.filter(line => shouldKeepLine(line))
.join('\n');
extractedContent += `--- PAGE ${pageNum} ---\n${pageText}\n\n`;
}
const fileProgress = (i / selectedFiles.length) * 100;
const pageContribution = (pageNum / pdf.numPages) * (100 / selectedFiles.length);
setProgress(Math.min(Math.round(fileProgress + pageContribution), 99));
}
}
let result = extractedContent;
setFinalText(result);
setProgress(100);
setTimeout(() => setStep('result'), 500);
} catch (err) {
console.error(err);
alert(t.error + ": " + err.message);
setStep('input');
}
};
const downloadFile = () => {
const mimeTypes = {
txt: 'text/plain',
md: 'text/markdown',
html: 'text/html'
};
let fileContent = finalText;
if (outputFormat === 'html') {
fileContent = `<!DOCTYPE html><html><head><meta charset='UTF-8'><title>Merged Content</title></head><body style='font-family: sans-serif; padding: 20px;'>${finalText}</body></html>`;
}
const blob = new Blob([fileContent], { type: mimeTypes[outputFormat] });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `merged_document.${outputFormat}`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
};
const resetApp = () => {
setFiles([]);
setFinalText('');
setProgress(0);
setStep('input');
};
return (
<div className={`min-h-screen flex items-center justify-center p-4 transition-colors duration-300 ${theme === 'dark' ? 'text-white' : 'text-gray-800'}`}>
<div className="w-full max-w-4xl transition-all duration-500">
<h1 className="text-3xl font-bold text-center mb-8 tracking-tight">{t.title}</h1>
<div className={`rounded-2xl shadow-xl overflow-hidden transition-all duration-300 glassmorphism ${theme === 'dark' ? 'border border-gray-700' : ''}`}>
{step === 'input' && (
<div className="p-8 md:p-12 animate-fade-in">
<div
className={`border-2 border-dashed rounded-xl p-10 text-center cursor-pointer transition-all duration-300 ${isDragging ? 'drag-active' : ''} ${theme === 'dark' ? 'border-gray-600 hover:border-gray-400 bg-gray-800' : 'border-gray-300 hover:border-gray-400 bg-gray-50'}`}
onDragOver={handleDragOver}
onDragLeave={handleDragLeave}
onDrop={handleDrop}
onClick={() => fileInputRef.current.click()}
>
<svg xmlns="http://www.w3.org/2000/svg" className={`h-16 w-16 mx-auto mb-4 ${theme === 'dark' ? 'text-gray-400' : 'text-gray-400'}`} fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M9 13h6m-3-3v6m5 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
</svg>
<h3 className="text-xl font-semibold mb-2">{t.dropTitle}</h3>
<p className={`text-sm ${theme === 'dark' ? 'text-gray-400' : 'text-gray-500'}`}>{t.dropSubtitle}</p>
<input
type="file"
multiple
accept=".pdf"
ref={fileInputRef}
className="hidden"
onChange={handleFileSelect}
/>
</div>
<div className="mt-8 flex flex-col items-center gap-6">
<button
onClick={() => fileInputRef.current.click()}
className="bg-primary hover:bg-green-600 text-white font-bold py-3 px-8 rounded-full shadow-lg transform transition hover:scale-105 active:scale-95 text-lg"
>
{t.uploadBtn}
</button>
<div className="flex flex-col sm:flex-row items-center gap-3 bg-opacity-10 p-2 rounded-lg">
<span className={`font-medium ${theme === 'dark' ? 'text-gray-300' : 'text-gray-600'}`}>{t.formatLabel}</span>
<div className="flex bg-gray-200 dark:bg-gray-700 rounded-lg p-1">
{['txt', 'md', 'html'].map((fmt) => (
<button
key={fmt}
onClick={() => setOutputFormat(fmt)}
className={`px-4 py-1.5 rounded-md text-sm font-medium transition-all ${outputFormat === fmt
? 'bg-white dark:bg-gray-600 text-primary shadow-sm'
: 'text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-200'}`}
>
{fmt.toUpperCase()}
</button>
))}
</div>
</div>
</div>
</div>
)}
{step === 'processing' && (
<div className="p-12 text-center animate-fade-in">
<h2 className="text-2xl font-bold mb-6">{t.processing}</h2>
<div className="w-full h-6 bg-gray-200 dark:bg-gray-700 rounded-full overflow-hidden relative">
<div
className="h-full bg-primary transition-all duration-300 ease-out flex items-center justify-center text-xs text-white font-bold"
style={{ width: `${progress}%` }}
>
{progress > 10 && `${progress}%`}
</div>
</div>
<p className="mt-4 text-gray-500 dark:text-gray-400">{files.length} {t.fileCount}</p>
</div>
)}
{step === 'result' && (
<div className="flex flex-col h-[80vh] md:h-[70vh] animate-fade-in">
<div className="p-4 border-b dark:border-gray-700 flex justify-between items-center bg-opacity-50">
<h3 className="font-semibold text-lg">{t.previewTitle}</h3>
<span className="text-xs uppercase px-2 py-1 rounded bg-gray-200 dark:bg-gray-700 text-gray-600 dark:text-gray-300 font-mono">
.{outputFormat}
</span>
</div>
<div className="flex-1 p-6 overflow-auto preview-scroll bg-gray-50 dark:bg-gray-900 font-mono text-sm leading-relaxed">
{outputFormat === 'html' ? (
<div dangerouslySetInnerHTML={{ __html: finalText }} className="text-gray-700 dark:text-gray-300 font-sans" />
) : (
<pre className="whitespace-pre-wrap font-inherit text-gray-700 dark:text-gray-300 font-sans">
{finalText}
</pre>
)}
</div>
<div className="p-6 border-t dark:border-gray-700 flex flex-col sm:flex-row justify-center gap-4 bg-surfaceLight dark:bg-surfaceDark z-10">
<button
onClick={resetApp}
className="bg-secondary hover:bg-orange-600 text-white font-bold py-3 px-6 rounded-full shadow-md transform transition hover:scale-105 active:scale-95 flex items-center justify-center gap-2"
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"></path></svg>
{t.resetBtn}
</button>
<button
onClick={downloadFile}
className="bg-primary hover:bg-green-600 text-white font-bold py-3 px-6 rounded-full shadow-md transform transition hover:scale-105 active:scale-95 flex items-center justify-center gap-2"
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"></path></svg>
{t.downloadBtn}
</button>
</div>
</div>
)}
</div>
<div className={`text-center mt-6 text-sm transition-all duration-300 ${theme === 'dark' ? 'text-gray-300' : 'text-gray-600'}`}>
<p className="mb-2">Created by <span className="font-semibold text-primary">AXXO</span></p>
<div className="flex justify-center items-center gap-4">
<a
href="https://github.com/axxodeveloper"
target="_blank"
rel="noopener noreferrer"
className="footer-link"
title="GitHub"
>
<svg className="social-icon" fill="currentColor" viewBox="0 0 24 24">
<path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v 3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z"/>
</svg>
GitHub
</a>
<span className="footer-divider">•</span>
<a
href="https://www.instagram.com/axxo.developer"
target="_blank"
rel="noopener noreferrer"
className="footer-link"
title="Instagram"
>
<svg className="social-icon" fill="currentColor" viewBox="0 0 24 24">
<path d="M12 2.163c3.204 0 3.584.012 4.85.07 3.252.148 4.771 1.691 4.919 4.919.058 1.265.069 1.645.069 4.849 0 3.205-.012 3.584-.069 4.849-.149 3.225-1.664 4.771-4.919 4.919-1.266.058-1.644.07-4.85.07-3.204 0-3.584-.012-4.849-.07-3.26-.149-4.771-1.699-4.919-4.92-.058-1.265-.07-1.644-.07-4.849 0-3.204.013-3.583.07-4.849.149-3.227 1.664-4.771 4.919-4.919 1.266-.057 1.645-.069 4.849-.069zm0-2.163c-3.259 0-3.667.014-4.947.072-4.358.2-6.78 2.618-6.98 6.98-.059 1.281-.073 1.689-.073 4.948 0 3.259.014 3.668.072 4.948.2 4.358 2.618 6.78 6.98 6.98 1.281.058 1.689.072 4.948.072 3.259 0 3.668-.014 4.948-.072 4.354-.2 6.782-2.618 6.979-6.98.059-1.28.073-1.689.073-4.948 0-3.259-.014-3.667-.072-4.947-.196-4.354-2.617-6.78-6.979-6.98-1.281-.059-1.69-.073-4.949-.073zM5.838 12a6.162 6.162 0 1 1 12.324 0 6.162 6.162 0 0 1-12.324 0zM12 16a4 4 0 1 1 0-8 4 4 0 0 1 0 8zm4.965-10.322a1.44 1.44 0 1 1 2.881.001 1.44 1.44 0 0 1-2.881-.001z"/>
</svg>
Instagram
</a>
</div>
</div>
</div>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);
</script>
</body>
</html>