-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.js
More file actions
340 lines (279 loc) · 11.4 KB
/
convert.js
File metadata and controls
340 lines (279 loc) · 11.4 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
// Required libraries (load from CDN or bundle)
// pdf-lib, pdf.js, jspdf, browser-image-compression, etc.
class DocumentConverter {
constructor() {
this.initUI();
this.initEventListeners();
}
initUI() {
// Initialize UI components
this.fileInput = document.getElementById('file-input');
this.convertBtn = document.getElementById('convert-btn');
this.clearBtn = document.getElementById('clear-btn');
this.previewArea = document.querySelector('.preview-area');
this.downloadArea = document.querySelector('.download-area');
// Tool selection
this.toolButtons = document.querySelectorAll('[data-tool]');
this.toolPanels = document.querySelectorAll('.tool-panel');
}
initEventListeners() {
// File input handling
this.fileInput.addEventListener('change', this.handleFileSelection.bind(this));
// Drag and drop
const dropZone = document.querySelector('.file-dropzone');
dropZone.addEventListener('dragover', this.handleDragOver.bind(this));
dropZone.addEventListener('drop', this.handleDrop.bind(this));
// Tool selection
this.toolButtons.forEach(btn => {
btn.addEventListener('click', this.switchTool.bind(this));
});
// Action buttons
this.convertBtn.addEventListener('click', this.convertFiles.bind(this));
this.clearBtn.addEventListener('click', this.clearSelection.bind(this));
}
handleFileSelection(e) {
const files = Array.from(e.target.files);
this.processSelectedFiles(files);
}
handleDragOver(e) {
e.preventDefault();
e.stopPropagation();
e.dataTransfer.dropEffect = 'copy';
}
handleDrop(e) {
e.preventDefault();
e.stopPropagation();
const files = Array.from(e.dataTransfer.files);
this.processSelectedFiles(files);
}
processSelectedFiles(files) {
// Validate files
if (!this.validateFiles(files)) return;
// Display preview
this.displayPreview(files);
}
validateFiles(files) {
// Check file types based on selected tool
const currentTool = document.querySelector('[data-tool].active').dataset.tool;
const allowedTypes = this.getAllowedTypes(currentTool);
for (const file of files) {
if (!allowedTypes.includes(file.type)) {
alert(`Invalid file type: ${file.name}. Allowed types: ${allowedTypes.join(', ')}`);
return false;
}
}
return true;
}
getAllowedTypes(tool) {
const typeMap = {
'image-to-pdf': ['image/jpeg', 'image/png', 'image/webp', 'image/bmp'],
'pdf-to-image': ['application/pdf'],
'image-converter': ['image/jpeg', 'image/png', 'image/webp', 'image/bmp', 'image/tiff'],
'pdf-tools': ['application/pdf']
};
return typeMap[tool] || [];
}
displayPreview(files) {
this.previewArea.innerHTML = '';
files.forEach(file => {
const previewItem = document.createElement('div');
previewItem.className = 'preview-item';
if (file.type.startsWith('image/')) {
const img = document.createElement('img');
img.src = URL.createObjectURL(file);
previewItem.appendChild(img);
} else if (file.type === 'application/pdf') {
// Use pdf.js to generate thumbnail
this.generatePdfThumbnail(file).then(thumbnail => {
previewItem.appendChild(thumbnail);
});
}
const fileName = document.createElement('p');
fileName.textContent = file.name;
previewItem.appendChild(fileName);
this.previewArea.appendChild(previewItem);
});
}
async generatePdfThumbnail(file) {
// Implement using pdf.js
// Return a canvas element with the first page preview
}
switchTool(e) {
const tool = e.currentTarget.dataset.tool;
// Update active button
this.toolButtons.forEach(btn => btn.classList.remove('active'));
e.currentTarget.classList.add('active');
// Show corresponding panel
this.toolPanels.forEach(panel => panel.classList.remove('active'));
document.getElementById(`${tool}-interface`).classList.add('active');
// Update file input accept attribute
const allowedTypes = this.getAllowedTypes(tool).join(',');
this.fileInput.setAttribute('accept', allowedTypes);
// Clear previous selections
this.clearSelection();
}
async convertFiles() {
const files = Array.from(this.fileInput.files);
if (files.length === 0) {
alert('Please select files first');
return;
}
const currentTool = document.querySelector('[data-tool].active').dataset.tool;
try {
this.showLoading(true);
let result;
switch(currentTool) {
case 'image-to-pdf':
result = await this.convertImagesToPdf(files);
break;
case 'pdf-to-image':
result = await this.convertPdfToImages(files[0]); // Assuming single PDF
break;
case 'image-converter':
result = await this.convertImageFormats(files);
break;
case 'pdf-tools':
// Handle PDF tools (merge, split, etc.)
break;
default:
throw new Error('Invalid tool selected');
}
this.displayDownloadLinks(result);
} catch (error) {
console.error('Conversion error:', error);
alert('An error occurred during conversion. Please try again.');
} finally {
this.showLoading(false);
}
}
async convertImagesToPdf(imageFiles) {
// Use pdf-lib or jspdf to create PDF from images
const { PDFDocument } = PDFLib;
const pdfDoc = await PDFDocument.create();
for (const imageFile of imageFiles) {
const imageBytes = await this.readFileAsArrayBuffer(imageFile);
let image;
if (imageFile.type === 'image/jpeg') {
image = await pdfDoc.embedJpg(imageBytes);
} else if (imageFile.type === 'image/png') {
image = await pdfDoc.embedPng(imageBytes);
}
const page = pdfDoc.addPage([image.width, image.height]);
page.drawImage(image, {
x: 0,
y: 0,
width: image.width,
height: image.height,
});
}
const pdfBytes = await pdfDoc.save();
return new Blob([pdfBytes], { type: 'application/pdf' });
}
async convertPdfToImages(pdfFile) {
// Use pdf.js to render each page as image
const pdf = await pdfjsLib.getDocument(URL.createObjectURL(pdfFile)).promise;
const images = [];
for (let i = 1; i <= pdf.numPages; i++) {
const page = await pdf.getPage(i);
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 imageBlob = await new Promise(resolve => {
canvas.toBlob(resolve, 'image/jpeg', 0.95);
});
images.push({
blob: imageBlob,
pageNumber: i
});
}
return images;
}
async convertImageFormats(imageFiles) {
// Convert between different image formats
const convertedImages = [];
for (const imageFile of imageFiles) {
const targetFormat = document.getElementById('target-format').value;
const quality = document.getElementById('image-quality').value;
const image = await createImageBitmap(imageFile);
const canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(image, 0, 0);
let blob;
if (targetFormat === 'jpg') {
blob = await new Promise(resolve => {
canvas.toBlob(resolve, 'image/jpeg', quality);
});
} else if (targetFormat === 'png') {
blob = await new Promise(resolve => {
canvas.toBlob(resolve, 'image/png');
});
}
convertedImages.push(blob);
}
return convertedImages;
}
displayDownloadLinks(results) {
this.downloadArea.innerHTML = '';
if (Array.isArray(results)) {
results.forEach((result, index) => {
const link = this.createDownloadLink(result, `converted-${index + 1}`);
this.downloadArea.appendChild(link);
});
} else {
const link = this.createDownloadLink(results, 'converted');
this.downloadArea.appendChild(link);
}
}
createDownloadLink(blob, fileName) {
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
// Determine file extension
let extension;
if (blob.type === 'application/pdf') {
extension = 'pdf';
} else if (blob.type === 'image/jpeg') {
extension = 'jpg';
} else if (blob.type === 'image/png') {
extension = 'png';
}
a.download = `${fileName}.${extension}`;
a.textContent = `Download ${a.download}`;
a.className = 'btn btn-primary me-2';
return a;
}
readFileAsArrayBuffer(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsArrayBuffer(file);
});
}
showLoading(show) {
if (show) {
this.convertBtn.disabled = true;
this.convertBtn.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Processing...';
} else {
this.convertBtn.disabled = false;
this.convertBtn.textContent = 'Convert';
}
}
clearSelection() {
this.fileInput.value = '';
this.previewArea.innerHTML = '';
this.downloadArea.innerHTML = '';
}
}
// Initialize the application when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
new DocumentConverter();
});