refactor: streamline HTML structure and enhance UI components
- Cleaned up the HTML structure in index.html and pdf-multi-tool.html for better readability and maintainability. - Improved the user interface of the PDF multi-tool with responsive button designs and better layout. - Added new CSS styles for button states and cursor behavior. - Updated README with corrected Docker deployment link. - Refactored JavaScript logic to utilize new helper functions for formatting star counts. - Commented out unused attachment functionalities in the logic files for future integration.
This commit is contained in:
@@ -1,86 +1,88 @@
|
||||
import { showLoader, hideLoader, showAlert } from '../ui.js';
|
||||
import { downloadFile, readFileAsArrayBuffer } from '../utils/helpers.js';
|
||||
import { state } from '../state.js';
|
||||
import { PDFDocument as PDFLibDocument } from 'pdf-lib';
|
||||
import JSZip from 'jszip';
|
||||
// TODO@ALAM - USE CPDF HERE
|
||||
|
||||
export async function extractAttachments() {
|
||||
if (state.files.length === 0) {
|
||||
showAlert('No Files', 'Please select at least one PDF file.');
|
||||
return;
|
||||
}
|
||||
// import { showLoader, hideLoader, showAlert } from '../ui.js';
|
||||
// import { downloadFile, readFileAsArrayBuffer } from '../utils/helpers.js';
|
||||
// import { state } from '../state.js';
|
||||
// import { PDFDocument as PDFLibDocument } from 'pdf-lib';
|
||||
// import JSZip from 'jszip';
|
||||
|
||||
showLoader('Extracting attachments...');
|
||||
try {
|
||||
const zip = new JSZip();
|
||||
let totalAttachments = 0;
|
||||
// export async function extractAttachments() {
|
||||
// if (state.files.length === 0) {
|
||||
// showAlert('No Files', 'Please select at least one PDF file.');
|
||||
// return;
|
||||
// }
|
||||
|
||||
for (const file of state.files) {
|
||||
const pdfBytes = await readFileAsArrayBuffer(file);
|
||||
const pdfDoc = await PDFLibDocument.load(pdfBytes as ArrayBuffer, {
|
||||
ignoreEncryption: true,
|
||||
});
|
||||
// showLoader('Extracting attachments...');
|
||||
// try {
|
||||
// const zip = new JSZip();
|
||||
// let totalAttachments = 0;
|
||||
|
||||
const embeddedFiles = pdfDoc.context.enumerateIndirectObjects()
|
||||
.filter(([ref, obj]: any) => {
|
||||
// obj must be a PDFDict
|
||||
if (obj && typeof obj.get === 'function') {
|
||||
const type = obj.get('Type');
|
||||
return type && type.toString() === '/Filespec';
|
||||
}
|
||||
return false;
|
||||
});
|
||||
// for (const file of state.files) {
|
||||
// const pdfBytes = await readFileAsArrayBuffer(file);
|
||||
// const pdfDoc = await PDFLibDocument.load(pdfBytes as ArrayBuffer, {
|
||||
// ignoreEncryption: true,
|
||||
// });
|
||||
|
||||
if (embeddedFiles.length === 0) {
|
||||
console.warn(`No attachments found in ${file.name}`);
|
||||
continue;
|
||||
}
|
||||
// const embeddedFiles = pdfDoc.context.enumerateIndirectObjects()
|
||||
// .filter(([ref, obj]: any) => {
|
||||
// // obj must be a PDFDict
|
||||
// if (obj && typeof obj.get === 'function') {
|
||||
// const type = obj.get('Type');
|
||||
// return type && type.toString() === '/Filespec';
|
||||
// }
|
||||
// return false;
|
||||
// });
|
||||
|
||||
// Extract attachments
|
||||
const baseName = file.name.replace(/\.pdf$/i, '');
|
||||
for (let i = 0; i < embeddedFiles.length; i++) {
|
||||
try {
|
||||
const [ref, fileSpec] = embeddedFiles[i];
|
||||
const fileSpecDict = fileSpec as any;
|
||||
// if (embeddedFiles.length === 0) {
|
||||
// console.warn(`No attachments found in ${file.name}`);
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// // Extract attachments
|
||||
// const baseName = file.name.replace(/\.pdf$/i, '');
|
||||
// for (let i = 0; i < embeddedFiles.length; i++) {
|
||||
// try {
|
||||
// const [ref, fileSpec] = embeddedFiles[i];
|
||||
// const fileSpecDict = fileSpec as any;
|
||||
|
||||
// Get attachment name
|
||||
const fileName = fileSpecDict.get('UF')?.decodeText() ||
|
||||
fileSpecDict.get('F')?.decodeText() ||
|
||||
`attachment-${i + 1}`;
|
||||
// // Get attachment name
|
||||
// const fileName = fileSpecDict.get('UF')?.decodeText() ||
|
||||
// fileSpecDict.get('F')?.decodeText() ||
|
||||
// `attachment-${i + 1}`;
|
||||
|
||||
// Get embedded file stream
|
||||
const ef = fileSpecDict.get('EF');
|
||||
if (ef) {
|
||||
const fRef = ef.get('F') || ef.get('UF');
|
||||
if (fRef) {
|
||||
const fileStream = pdfDoc.context.lookup(fRef);
|
||||
if (fileStream) {
|
||||
const fileData = (fileStream as any).getContents();
|
||||
zip.file(`${baseName}_${fileName}`, fileData);
|
||||
totalAttachments++;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn(`Failed to extract attachment ${i} from ${file.name}:`, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
// // Get embedded file stream
|
||||
// const ef = fileSpecDict.get('EF');
|
||||
// if (ef) {
|
||||
// const fRef = ef.get('F') || ef.get('UF');
|
||||
// if (fRef) {
|
||||
// const fileStream = pdfDoc.context.lookup(fRef);
|
||||
// if (fileStream) {
|
||||
// const fileData = (fileStream as any).getContents();
|
||||
// zip.file(`${baseName}_${fileName}`, fileData);
|
||||
// totalAttachments++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// } catch (e) {
|
||||
// console.warn(`Failed to extract attachment ${i} from ${file.name}:`, e);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
if (totalAttachments === 0) {
|
||||
showAlert('No Attachments', 'No attachments were found in the selected PDF(s).');
|
||||
hideLoader();
|
||||
return;
|
||||
}
|
||||
// if (totalAttachments === 0) {
|
||||
// showAlert('No Attachments', 'No attachments were found in the selected PDF(s).');
|
||||
// hideLoader();
|
||||
// return;
|
||||
// }
|
||||
|
||||
const zipBlob = await zip.generateAsync({ type: 'blob' });
|
||||
downloadFile(zipBlob, 'extracted-attachments.zip');
|
||||
showAlert('Success', `Extracted ${totalAttachments} attachment(s) successfully!`);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
showAlert('Error', 'Failed to extract attachments. The PDF may not contain attachments or may be corrupted.');
|
||||
} finally {
|
||||
hideLoader();
|
||||
}
|
||||
}
|
||||
// const zipBlob = await zip.generateAsync({ type: 'blob' });
|
||||
// downloadFile(zipBlob, 'extracted-attachments.zip');
|
||||
// showAlert('Success', `Extracted ${totalAttachments} attachment(s) successfully!`);
|
||||
// } catch (e) {
|
||||
// console.error(e);
|
||||
// showAlert('Error', 'Failed to extract attachments. The PDF may not contain attachments or may be corrupted.');
|
||||
// } finally {
|
||||
// hideLoader();
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user