-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
687 lines (595 loc) · 34.6 KB
/
index.html
File metadata and controls
687 lines (595 loc) · 34.6 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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#3b82f6">
<meta name="description" content="Convertissez vos fichiers Word, PDF et Excel en images JPG">
<title>FileToJPG - Convertisseur de fichiers en images</title>
<link rel="manifest" href="/convert/manifest.json">
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🖼️</text></svg>">
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mammoth/1.4.0/mammoth.browser.min.js"></script>
<style>
.dropzone {
border: 2px dashed #3b82f6;
transition: all 0.3s ease;
}
.dropzone.active {
border-color: #10b981;
background-color: #f0fdf4;
}
.dark .dropzone.active {
background-color: #052e16;
}
.file-item:hover {
background-color: #f8fafc;
}
.dark .file-item:hover {
background-color: #1e293b;
}
.progress-bar {
transition: width 0.3s ease;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
.animate-pulse {
animation: pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;
}
.dark {
color-scheme: dark;
}
.app-icon {
width: 48px;
height: 48px;
display: flex;
align-items: center;
justify-content: center;
background: #3b82f6;
color: white;
border-radius: 12px;
font-size: 24px;
margin-right: 12px;
}
.dark .app-icon {
background: #1e40af;
}
</style>
</head>
<body class="bg-gray-50 dark:bg-gray-900 min-h-screen">
<div class="container mx-auto px-4 py-8">
<!-- Header -->
<header class="text-center mb-12">
<div class="flex items-center justify-center mb-4">
<div class="app-icon">
🖼️
</div>
<div>
<h1 class="text-4xl font-bold text-blue-600 dark:text-blue-400 mb-2">FileToJPG</h1>
<p class="text-lg text-gray-600 dark:text-gray-300">Convertissez vos fichiers Word, PDF et Excel en images JPG</p>
</div>
</div>
<div class="flex justify-center">
<button id="themeToggle" class="p-2 rounded-full bg-gray-200 dark:bg-gray-700 text-gray-700 dark:text-gray-200">
<svg id="sunIcon" xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z" />
</svg>
<svg id="moonIcon" xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 hidden" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z" />
</svg>
</button>
</div>
</header>
<!-- Main Content -->
<main>
<!-- Upload Section -->
<div class="max-w-3xl mx-auto bg-white dark:bg-gray-800 rounded-xl shadow-md overflow-hidden mb-8">
<div class="p-8">
<div
id="dropzone"
class="dropzone rounded-lg p-12 text-center cursor-pointer dark:border-blue-400 dark:bg-gray-700/30"
ondragover="event.preventDefault(); this.classList.add('active');"
ondragleave="this.classList.remove('active');"
ondrop="event.preventDefault(); this.classList.remove('active'); handleFiles(event.dataTransfer.files);"
>
<div class="flex flex-col items-center justify-center space-y-4">
<svg xmlns="http://www.w3.org/2000/svg" class="h-16 w-16 text-blue-500 dark:text-blue-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12" />
</svg>
<h3 class="text-xl font-semibold text-gray-700 dark:text-gray-200">Glissez-déposez vos fichiers ici</h3>
<p class="text-gray-500 dark:text-gray-400">ou</p>
<label for="fileInput" class="px-6 py-2 bg-blue-600 dark:bg-blue-700 text-white rounded-lg hover:bg-blue-700 dark:hover:bg-blue-600 transition cursor-pointer">
Sélectionnez des fichiers
</label>
<input id="fileInput" type="file" class="hidden" multiple accept=".doc,.docx,.pdf,.xls,.xlsx" onchange="handleFiles(this.files)">
<p class="text-sm text-gray-500 dark:text-gray-400 mt-2">Formats supportés: DOC, DOCX, PDF, XLS, XLSX</p>
</div>
</div>
</div>
</div>
<!-- File List Section -->
<div id="fileListSection" class="max-w-3xl mx-auto bg-white dark:bg-gray-800 rounded-xl shadow-md overflow-hidden mb-8 hidden">
<div class="p-6">
<h2 class="text-xl font-semibold text-gray-800 dark:text-gray-200 mb-4">Fichiers à convertir</h2>
<div id="fileList" class="space-y-3">
<!-- Files will be listed here -->
</div>
<div class="mt-6 flex justify-end">
<button id="convertBtn" class="px-6 py-2 bg-green-600 dark:bg-green-700 text-white rounded-lg hover:bg-green-700 dark:hover:bg-green-600 transition flex items-center">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M3 17a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zm3.293-7.707a1 1 0 011.414 0L9 10.586V3a1 1 0 112 0v7.586l1.293-1.293a1 1 0 111.414 1.414l-3 3a1 1 0 01-1.414 0l-3-3a1 1 0 010-1.414z" clip-rule="evenodd" />
</svg>
Convertir tout
</button>
</div>
</div>
</div>
<!-- Progress Section -->
<div id="progressSection" class="max-w-3xl mx-auto bg-white dark:bg-gray-800 rounded-xl shadow-md overflow-hidden mb-8 hidden">
<div class="p-6">
<h2 class="text-xl font-semibold text-gray-800 dark:text-gray-200 mb-4">Conversion en cours</h2>
<div id="progressContainer" class="space-y-4">
<!-- Progress items will be added here -->
</div>
</div>
</div>
<!-- Results Section -->
<div id="resultsSection" class="max-w-3xl mx-auto bg-white dark:bg-gray-800 rounded-xl shadow-md overflow-hidden hidden">
<div class="p-6">
<div class="flex justify-between items-center mb-4">
<h2 class="text-xl font-semibold text-gray-800 dark:text-gray-200">Résultats de conversion</h2>
<button id="downloadAllBtn" class="px-4 py-2 bg-blue-600 dark:bg-blue-700 text-white rounded-lg hover:bg-blue-700 dark:hover:bg-blue-600 transition flex items-center text-sm">
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M3 17a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zm3.293-7.707a1 1 0 011.414 0L9 10.586V3a1 1 0 112 0v7.586l1.293-1.293a1 1 0 111.414 1.414l-3 3a1 1 0 01-1.414 0l-3-3a1 1 0 010-1.414z" clip-rule="evenodd" />
</svg>
Tout télécharger
</button>
</div>
<div id="resultsGrid" class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4">
<!-- Results will be displayed here -->
</div>
</div>
</div>
</main>
<!-- Footer -->
<footer class="text-center mt-12 text-gray-500 dark:text-gray-400 text-sm">
<p>FileToJPG - Convertisseur de fichiers en images JPG</p>
<p class="mt-1">Tous droits réservés © <span id="currentYear"></span></p>
</footer>
</div>
<!-- Install button container -->
<div id="installContainer" class="fixed bottom-4 right-4 hidden">
<button id="installBtn" class="px-4 py-2 bg-blue-600 dark:bg-blue-700 text-white rounded-lg shadow-lg hover:bg-blue-700 dark:hover:bg-blue-600 transition flex items-center">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M3 17a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zm3.293-7.707a1 1 0 011.414 0L9 10.586V3a1 1 0 112 0v7.586l1.293-1.293a1 1 0 111.414 1.414l-3 3a1 1 0 01-1.414 0l-3-3a1 1 0 010-1.414z" clip-rule="evenodd" />
</svg>
Installer
</button>
</div>
<script>
// Set current year in footer
document.getElementById('currentYear').textContent = new Date().getFullYear();
// Initialize PDF.js worker
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.worker.min.js';
// Store files and converted images
let filesToConvert = [];
let convertedImages = [];
let deferredPrompt;
// ========== PWA Functionality ==========
// Check if the browser supports service workers
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('sw.js').then(registration => {
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}).catch(err => {
console.log('ServiceWorker registration failed: ', err);
});
});
}
// Handle the beforeinstallprompt event
window.addEventListener('beforeinstallprompt', (e) => {
// Prevent the mini-infobar from appearing on mobile
e.preventDefault();
// Stash the event so it can be triggered later
deferredPrompt = e;
// Show the install button
const installContainer = document.getElementById('installContainer');
installContainer.classList.remove('hidden');
// Handle install button click
const installBtn = document.getElementById('installBtn');
installBtn.addEventListener('click', () => {
// Hide the install button
installContainer.classList.add('hidden');
// Show the install prompt
deferredPrompt.prompt();
// Wait for the user to respond to the prompt
deferredPrompt.userChoice.then((choiceResult) => {
if (choiceResult.outcome === 'accepted') {
console.log('User accepted the install prompt');
} else {
console.log('User dismissed the install prompt');
}
deferredPrompt = null;
});
});
});
// Track app installed event
window.addEventListener('appinstalled', () => {
console.log('App was installed');
const installContainer = document.getElementById('installContainer');
installContainer.classList.add('hidden');
deferredPrompt = null;
});
// ========== Dark/Light Mode Toggle ==========
const themeToggle = document.getElementById('themeToggle');
const sunIcon = document.getElementById('sunIcon');
const moonIcon = document.getElementById('moonIcon');
// Check for saved theme preference or use system preference
const savedTheme = localStorage.getItem('theme') ||
(window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
// Apply the saved theme
if (savedTheme === 'dark') {
document.documentElement.classList.add('dark');
sunIcon.classList.add('hidden');
moonIcon.classList.remove('hidden');
} else {
document.documentElement.classList.remove('dark');
sunIcon.classList.remove('hidden');
moonIcon.classList.add('hidden');
}
// Toggle theme on button click
themeToggle.addEventListener('click', () => {
if (document.documentElement.classList.contains('dark')) {
document.documentElement.classList.remove('dark');
localStorage.setItem('theme', 'light');
sunIcon.classList.remove('hidden');
moonIcon.classList.add('hidden');
} else {
document.documentElement.classList.add('dark');
localStorage.setItem('theme', 'dark');
sunIcon.classList.add('hidden');
moonIcon.classList.remove('hidden');
}
});
// ========== File Conversion Logic ==========
// Handle file selection
function handleFiles(selectedFiles) {
if (!selectedFiles || selectedFiles.length === 0) return;
// Filter only supported files
const supportedFiles = Array.from(selectedFiles).filter(file => {
const extension = file.name.split('.').pop().toLowerCase();
return ['doc', 'docx', 'pdf', 'xls', 'xlsx'].includes(extension);
});
if (supportedFiles.length === 0) {
alert('Aucun fichier supporté sélectionné. Formats acceptés: DOC, DOCX, PDF, XLS, XLSX');
return;
}
// Add files to the list
filesToConvert = [...filesToConvert, ...supportedFiles];
updateFileList();
document.getElementById('fileListSection').classList.remove('hidden');
}
// Update the file list UI
function updateFileList() {
const fileListElement = document.getElementById('fileList');
fileListElement.innerHTML = '';
filesToConvert.forEach((file, index) => {
const fileItem = document.createElement('div');
fileItem.className = 'file-item flex items-center justify-between p-3 border border-gray-200 dark:border-gray-700 rounded-lg';
const fileInfo = document.createElement('div');
fileInfo.className = 'flex items-center space-x-3';
// File icon based on type
const icon = document.createElement('div');
icon.className = 'text-blue-500 dark:text-blue-400';
const extension = file.name.split('.').pop().toLowerCase();
let iconPath;
switch(extension) {
case 'pdf':
iconPath = 'M4 4a2 2 0 012-2h4.586A2 2 0 0112 2.586L15.414 6A2 2 0 0116 7.414V16a2 2 0 01-2 2H6a2 2 0 01-2-2V4z';
break;
case 'doc':
case 'docx':
iconPath = 'M9 12h6m-6 4h6m2 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';
break;
case 'xls':
case 'xlsx':
iconPath = 'M9 17v-2m3 2v-4m3 4v-6m2 10H7a2 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';
break;
default:
iconPath = 'M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z';
}
icon.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="${iconPath}" /></svg>`;
const fileName = document.createElement('span');
fileName.className = 'font-medium text-gray-700 dark:text-gray-300 truncate max-w-xs';
fileName.textContent = file.name;
const fileSize = document.createElement('span');
fileSize.className = 'text-sm text-gray-500 dark:text-gray-400';
fileSize.textContent = formatFileSize(file.size);
fileInfo.appendChild(icon);
fileInfo.appendChild(document.createElement('div').appendChild(fileName));
fileInfo.appendChild(document.createElement('div').appendChild(fileSize));
const removeBtn = document.createElement('button');
removeBtn.className = 'text-red-500 hover:text-red-700 dark:hover:text-red-400';
removeBtn.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor"><path fill-rule="evenodd" d="M9 2a1 1 0 00-.894.553L7.382 4H4a1 1 0 000 2v10a2 2 0 002 2h8a2 2 0 002-2V6a1 1 0 100-2h-3.382l-.724-1.447A1 1 0 0011 2H9zM7 8a1 1 0 012 0v6a1 1 0 11-2 0V8zm5-1a1 1 0 00-1 1v6a1 1 0 102 0V8a1 1 0 00-1-1z" clip-rule="evenodd" /></svg>';
removeBtn.onclick = () => removeFile(index);
fileItem.appendChild(fileInfo);
fileItem.appendChild(removeBtn);
fileListElement.appendChild(fileItem);
});
}
// Remove file from the list
function removeFile(index) {
filesToConvert.splice(index, 1);
if (filesToConvert.length === 0) {
document.getElementById('fileListSection').classList.add('hidden');
} else {
updateFileList();
}
}
// Format file size
function formatFileSize(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
// Convert button click handler
document.getElementById('convertBtn').addEventListener('click', async () => {
if (filesToConvert.length === 0) return;
// Show progress section
document.getElementById('fileListSection').classList.add('hidden');
document.getElementById('progressSection').classList.remove('hidden');
// Create progress items
const progressContainer = document.getElementById('progressContainer');
progressContainer.innerHTML = '';
filesToConvert.forEach(file => {
const progressItem = document.createElement('div');
progressItem.className = 'space-y-2';
const fileName = document.createElement('div');
fileName.className = 'flex justify-between text-sm';
fileName.innerHTML = `<span class="font-medium text-gray-700 dark:text-gray-300 truncate">${file.name}</span> <span class="text-gray-500 dark:text-gray-400">0%</span>`;
const progressBar = document.createElement('div');
progressBar.className = 'w-full bg-gray-200 dark:bg-gray-700 rounded-full h-2.5';
progressBar.innerHTML = '<div class="progress-bar bg-blue-600 h-2.5 rounded-full" style="width: 0%"></div>';
progressItem.appendChild(fileName);
progressItem.appendChild(progressBar);
progressContainer.appendChild(progressItem);
});
// Convert each file
convertedImages = [];
for (let i = 0; i < filesToConvert.length; i++) {
const file = filesToConvert[i];
const progressItem = progressContainer.children[i];
const progressText = progressItem.querySelector('span:last-child');
const progressBar = progressItem.querySelector('.progress-bar');
try {
progressText.textContent = 'Conversion...';
progressBar.classList.add('animate-pulse');
const images = await convertFileToJPG(file, (progress) => {
const percent = Math.round(progress * 100);
progressText.textContent = `${percent}%`;
progressBar.style.width = `${percent}%`;
});
progressBar.classList.remove('animate-pulse');
progressBar.classList.remove('bg-blue-600');
progressBar.classList.add('bg-green-500');
progressText.textContent = 'Terminé ✓';
convertedImages.push({
fileName: file.name.replace(/\.[^/.]+$/, ''),
images: images
});
} catch (error) {
console.error('Conversion error:', error);
progressBar.classList.remove('animate-pulse');
progressBar.classList.remove('bg-blue-600');
progressBar.classList.add('bg-red-500');
progressText.textContent = 'Erreur ✗';
}
}
// Show results
document.getElementById('progressSection').classList.add('hidden');
showResults();
});
// Convert file to JPG images
async function convertFileToJPG(file, progressCallback) {
const extension = file.name.split('.').pop().toLowerCase();
progressCallback(0.1);
if (extension === 'pdf') {
return await convertPdfToJPG(file, progressCallback);
} else if (extension === 'doc' || extension === 'docx') {
return await convertWordToJPG(file, progressCallback);
} else if (extension === 'xls' || extension === 'xlsx') {
return await convertExcelToJPG(file, progressCallback);
}
throw new Error('Format non supporté');
}
// Convert PDF to JPG
async function convertPdfToJPG(file, progressCallback) {
return new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.onload = async function() {
try {
const typedArray = new Uint8Array(this.result);
const pdf = await pdfjsLib.getDocument(typedArray).promise;
const images = [];
for (let i = 1; i <= pdf.numPages; i++) {
const page = await pdf.getPage(i);
progressCallback(0.1 + (i / pdf.numPages) * 0.8);
const viewport = page.getViewport({ scale: 2.0 });
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
await page.render({
canvasContext: context,
viewport: viewport
}).promise;
const imageData = canvas.toDataURL('image/jpeg', 0.8);
images.push(imageData);
}
progressCallback(1);
resolve(images);
} catch (error) {
reject(error);
}
};
fileReader.onerror = reject;
fileReader.readAsArrayBuffer(file);
});
}
// Convert Word to JPG (simplified - would need server-side processing for better results)
async function convertWordToJPG(file, progressCallback) {
return new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.onload = async function() {
try {
const arrayBuffer = this.result;
const result = await mammoth.extractRawText({ arrayBuffer });
// Create a simple image representation of the text
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.width = 800;
canvas.height = Math.max(600, result.value.split('\n').length * 20);
context.fillStyle = document.documentElement.classList.contains('dark') ? '#1f2937' : 'white';
context.fillRect(0, 0, canvas.width, canvas.height);
context.fillStyle = document.documentElement.classList.contains('dark') ? 'white' : 'black';
context.font = '16px Arial';
const lines = result.value.split('\n');
lines.forEach((line, index) => {
context.fillText(line, 20, 30 + index * 20);
});
progressCallback(1);
resolve([canvas.toDataURL('image/jpeg', 0.8)]);
} catch (error) {
reject(error);
}
};
fileReader.onerror = reject;
fileReader.readAsArrayBuffer(file);
});
}
// Convert Excel to JPG (simplified - would need server-side processing for better results)
async function convertExcelToJPG(file, progressCallback) {
return new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.onload = function() {
try {
const data = new Uint8Array(this.result);
const workbook = XLSX.read(data, { type: 'array' });
// Create a simple image representation of the first sheet
const firstSheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[firstSheetName];
const html = XLSX.utils.sheet_to_html(worksheet);
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
// Create a temporary div to render the HTML table
const tempDiv = document.createElement('div');
tempDiv.style.position = 'absolute';
tempDiv.style.left = '-9999px';
tempDiv.style.width = '800px';
tempDiv.innerHTML = html;
document.body.appendChild(tempDiv);
// Style the table
const table = tempDiv.querySelector('table');
if (table) {
table.style.borderCollapse = 'collapse';
table.style.width = '100%';
Array.from(table.querySelectorAll('td, th')).forEach(cell => {
cell.style.border = '1px solid #ddd';
cell.style.padding = '8px';
});
}
// Calculate canvas size based on table content
const width = 800;
const height = tempDiv.offsetHeight;
canvas.width = width;
canvas.height = height;
context.fillStyle = document.documentElement.classList.contains('dark') ? '#1f2937' : 'white';
context.fillRect(0, 0, width, height);
context.fillStyle = document.documentElement.classList.contains('dark') ? 'white' : 'black';
context.font = '14px Arial';
if (table) {
const rows = table.rows;
for (let i = 0; i < rows.length; i++) {
const cells = rows[i].cells;
for (let j = 0; j < cells.length; j++) {
context.fillText(cells[j].textContent, j * 100 + 10, i * 20 + 20);
}
}
}
document.body.removeChild(tempDiv);
progressCallback(1);
resolve([canvas.toDataURL('image/jpeg', 0.8)]);
} catch (error) {
reject(error);
}
};
fileReader.onerror = reject;
fileReader.readAsArrayBuffer(file);
});
}
// Show conversion results
function showResults() {
const resultsGrid = document.getElementById('resultsGrid');
resultsGrid.innerHTML = '';
convertedImages.forEach((conversion, index) => {
conversion.images.forEach((imageData, imageIndex) => {
const resultItem = document.createElement('div');
resultItem.className = 'bg-white dark:bg-gray-700 rounded-lg border border-gray-200 dark:border-gray-600 overflow-hidden shadow-sm';
const imageContainer = document.createElement('div');
imageContainer.className = 'p-4 bg-gray-50 dark:bg-gray-600 flex justify-center';
const img = document.createElement('img');
img.src = imageData;
img.className = 'max-h-48 object-contain';
img.alt = `${conversion.fileName} - Page ${imageIndex + 1}`;
imageContainer.appendChild(img);
const infoContainer = document.createElement('div');
infoContainer.className = 'p-3 border-t border-gray-200 dark:border-gray-600';
const fileName = document.createElement('div');
fileName.className = 'font-medium text-gray-800 dark:text-gray-200 truncate';
fileName.textContent = `${conversion.fileName}${conversion.images.length > 1 ? ` - Page ${imageIndex + 1}` : ''}`;
const downloadBtn = document.createElement('button');
downloadBtn.className = 'mt-2 w-full px-3 py-1 bg-blue-600 dark:bg-blue-700 text-white rounded hover:bg-blue-700 dark:hover:bg-blue-600 transition text-sm flex items-center justify-center';
downloadBtn.innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M3 17a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zm3.293-7.707a1 1 0 011.414 0L9 10.586V3a1 1 0 112 0v7.586l1.293-1.293a1 1 0 111.414 1.414l-3 3a1 1 0 01-1.414 0l-3-3a1 1 0 010-1.414z" clip-rule="evenodd" />
</svg>
Télécharger
`;
downloadBtn.onclick = () => downloadImage(imageData, `${conversion.fileName}${conversion.images.length > 1 ? `-page-${imageIndex + 1}` : ''}.jpg`);
infoContainer.appendChild(fileName);
infoContainer.appendChild(downloadBtn);
resultItem.appendChild(imageContainer);
resultItem.appendChild(infoContainer);
resultsGrid.appendChild(resultItem);
});
});
document.getElementById('resultsSection').classList.remove('hidden');
}
// Download single image
function downloadImage(imageData, fileName) {
const link = document.createElement('a');
link.href = imageData;
link.download = fileName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
// Download all images
document.getElementById('downloadAllBtn').addEventListener('click', () => {
convertedImages.forEach((conversion, index) => {
conversion.images.forEach((imageData, imageIndex) => {
setTimeout(() => {
downloadImage(imageData, `${conversion.fileName}${conversion.images.length > 1 ? `-page-${imageIndex + 1}` : ''}.jpg`);
}, index * 300);
});
});
});
</script>
</body>
</html>