2025-12-11 19:34:14 +05:30
|
|
|
import { showAlert } from '../ui.js';
|
|
|
|
|
import { downloadFile, formatBytes } from '../utils/helpers.js';
|
|
|
|
|
import { PDFDocument, PDFName } from 'pdf-lib';
|
|
|
|
|
import { icons, createIcons } from 'lucide';
|
2026-03-26 12:11:12 +05:30
|
|
|
import { loadPdfWithPasswordPrompt } from '../utils/password-prompt.js';
|
2025-12-11 19:34:14 +05:30
|
|
|
|
|
|
|
|
interface PageState {
|
2026-03-26 12:11:12 +05:30
|
|
|
file: File | null;
|
2025-12-11 19:34:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pageState: PageState = {
|
2026-03-26 12:11:12 +05:30
|
|
|
file: null,
|
2025-12-11 19:34:14 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function removeMetadataFromDoc(pdfDoc: PDFDocument) {
|
2026-03-26 12:11:12 +05:30
|
|
|
// @ts-expect-error getInfoDict is private but accessible at runtime
|
|
|
|
|
const infoDict = pdfDoc.getInfoDict();
|
|
|
|
|
const allKeys = infoDict.keys();
|
|
|
|
|
allKeys.forEach((key: { asString: () => string }) => {
|
|
|
|
|
infoDict.delete(key);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
pdfDoc.setTitle('');
|
|
|
|
|
pdfDoc.setAuthor('');
|
|
|
|
|
pdfDoc.setSubject('');
|
|
|
|
|
pdfDoc.setKeywords([]);
|
|
|
|
|
pdfDoc.setCreator('');
|
|
|
|
|
pdfDoc.setProducer('');
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// @ts-expect-error catalog.dict is private but accessible at runtime
|
|
|
|
|
const catalogDict = pdfDoc.catalog.dict;
|
|
|
|
|
if (catalogDict.has(PDFName.of('Metadata'))) {
|
|
|
|
|
catalogDict.delete(PDFName.of('Metadata'));
|
2025-12-11 19:34:14 +05:30
|
|
|
}
|
2026-03-26 12:11:12 +05:30
|
|
|
} catch (e: unknown) {
|
|
|
|
|
const msg = e instanceof Error ? e.message : String(e);
|
|
|
|
|
console.warn('Could not remove XMP metadata:', msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const context = pdfDoc.context;
|
|
|
|
|
if (context.trailerInfo) {
|
|
|
|
|
delete context.trailerInfo.ID;
|
2025-12-11 19:34:14 +05:30
|
|
|
}
|
2026-03-26 12:11:12 +05:30
|
|
|
} catch (e: unknown) {
|
|
|
|
|
const msg = e instanceof Error ? e.message : String(e);
|
|
|
|
|
console.warn('Could not remove document IDs:', msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// @ts-expect-error catalog.dict is private but accessible at runtime
|
|
|
|
|
const catalogDict = pdfDoc.catalog.dict;
|
|
|
|
|
if (catalogDict.has(PDFName.of('PieceInfo'))) {
|
|
|
|
|
catalogDict.delete(PDFName.of('PieceInfo'));
|
2025-12-11 19:34:14 +05:30
|
|
|
}
|
2026-03-26 12:11:12 +05:30
|
|
|
} catch (e: unknown) {
|
|
|
|
|
const msg = e instanceof Error ? e.message : String(e);
|
|
|
|
|
console.warn('Could not remove PieceInfo:', msg);
|
|
|
|
|
}
|
2025-12-11 19:34:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function resetState() {
|
2026-03-26 12:11:12 +05:30
|
|
|
pageState.file = null;
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
const fileDisplayArea = document.getElementById('file-display-area');
|
|
|
|
|
if (fileDisplayArea) fileDisplayArea.innerHTML = '';
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
const toolOptions = document.getElementById('tool-options');
|
|
|
|
|
if (toolOptions) toolOptions.classList.add('hidden');
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
const fileInput = document.getElementById('file-input') as HTMLInputElement;
|
|
|
|
|
if (fileInput) fileInput.value = '';
|
2025-12-11 19:34:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function updateUI() {
|
2026-03-26 12:11:12 +05:30
|
|
|
const fileDisplayArea = document.getElementById('file-display-area');
|
|
|
|
|
const toolOptions = document.getElementById('tool-options');
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
if (!fileDisplayArea) return;
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
fileDisplayArea.innerHTML = '';
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
if (pageState.file) {
|
|
|
|
|
const fileDiv = document.createElement('div');
|
|
|
|
|
fileDiv.className =
|
|
|
|
|
'flex items-center justify-between bg-gray-700 p-3 rounded-lg text-sm';
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
const infoContainer = document.createElement('div');
|
|
|
|
|
infoContainer.className = 'flex flex-col overflow-hidden';
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
const nameSpan = document.createElement('div');
|
|
|
|
|
nameSpan.className = 'truncate font-medium text-gray-200 text-sm mb-1';
|
|
|
|
|
nameSpan.textContent = pageState.file.name;
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
const metaSpan = document.createElement('div');
|
|
|
|
|
metaSpan.className = 'text-xs text-gray-400';
|
|
|
|
|
metaSpan.textContent = formatBytes(pageState.file.size);
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
infoContainer.append(nameSpan, metaSpan);
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
const removeBtn = document.createElement('button');
|
|
|
|
|
removeBtn.className = 'ml-4 text-red-400 hover:text-red-300 flex-shrink-0';
|
|
|
|
|
removeBtn.innerHTML = '<i data-lucide="trash-2" class="w-4 h-4"></i>';
|
|
|
|
|
removeBtn.onclick = function () {
|
|
|
|
|
resetState();
|
|
|
|
|
};
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
fileDiv.append(infoContainer, removeBtn);
|
|
|
|
|
fileDisplayArea.appendChild(fileDiv);
|
|
|
|
|
createIcons({ icons });
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
if (toolOptions) toolOptions.classList.remove('hidden');
|
|
|
|
|
} else {
|
|
|
|
|
if (toolOptions) toolOptions.classList.add('hidden');
|
|
|
|
|
}
|
2025-12-11 19:34:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleFileSelect(files: FileList | null) {
|
2026-03-26 12:11:12 +05:30
|
|
|
if (files && files.length > 0) {
|
|
|
|
|
const file = files[0];
|
|
|
|
|
if (
|
|
|
|
|
file.type === 'application/pdf' ||
|
|
|
|
|
file.name.toLowerCase().endsWith('.pdf')
|
|
|
|
|
) {
|
|
|
|
|
pageState.file = file;
|
|
|
|
|
updateUI();
|
2025-12-11 19:34:14 +05:30
|
|
|
}
|
2026-03-26 12:11:12 +05:30
|
|
|
}
|
2025-12-11 19:34:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function removeMetadata() {
|
2026-03-26 12:11:12 +05:30
|
|
|
if (!pageState.file) {
|
|
|
|
|
showAlert('No File', 'Please upload a PDF file first.');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const loaderModal = document.getElementById('loader-modal');
|
|
|
|
|
const loaderText = document.getElementById('loader-text');
|
|
|
|
|
if (loaderModal) loaderModal.classList.remove('hidden');
|
|
|
|
|
if (loaderText) loaderText.textContent = 'Removing all metadata...';
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (loaderModal) loaderModal.classList.add('hidden');
|
|
|
|
|
const result = await loadPdfWithPasswordPrompt(pageState.file);
|
|
|
|
|
if (!result) {
|
|
|
|
|
if (loaderModal) loaderModal.classList.add('hidden');
|
|
|
|
|
return;
|
2025-12-11 19:34:14 +05:30
|
|
|
}
|
|
|
|
|
if (loaderModal) loaderModal.classList.remove('hidden');
|
|
|
|
|
if (loaderText) loaderText.textContent = 'Removing all metadata...';
|
2026-03-26 12:11:12 +05:30
|
|
|
result.pdf.destroy();
|
|
|
|
|
const pdfDoc = await PDFDocument.load(result.bytes);
|
|
|
|
|
|
|
|
|
|
removeMetadataFromDoc(pdfDoc);
|
|
|
|
|
|
|
|
|
|
const newPdfBytes = await pdfDoc.save();
|
|
|
|
|
downloadFile(
|
|
|
|
|
new Blob([newPdfBytes as BlobPart], { type: 'application/pdf' }),
|
|
|
|
|
'metadata-removed.pdf'
|
|
|
|
|
);
|
|
|
|
|
showAlert('Success', 'Metadata removed successfully!', 'success', () => {
|
|
|
|
|
resetState();
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
showAlert('Error', 'An error occurred while trying to remove metadata.');
|
|
|
|
|
} finally {
|
|
|
|
|
if (loaderModal) loaderModal.classList.add('hidden');
|
|
|
|
|
}
|
2025-12-11 19:34:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', function () {
|
2026-03-26 12:11:12 +05:30
|
|
|
const fileInput = document.getElementById('file-input') as HTMLInputElement;
|
|
|
|
|
const dropZone = document.getElementById('drop-zone');
|
|
|
|
|
const processBtn = document.getElementById('process-btn');
|
|
|
|
|
const backBtn = document.getElementById('back-to-tools');
|
|
|
|
|
|
|
|
|
|
if (backBtn) {
|
|
|
|
|
backBtn.addEventListener('click', function () {
|
|
|
|
|
window.location.href = import.meta.env.BASE_URL;
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
if (fileInput && dropZone) {
|
|
|
|
|
fileInput.addEventListener('change', function (e) {
|
|
|
|
|
handleFileSelect((e.target as HTMLInputElement).files);
|
|
|
|
|
});
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
dropZone.addEventListener('dragover', function (e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
dropZone.classList.add('bg-gray-700');
|
|
|
|
|
});
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
dropZone.addEventListener('dragleave', function (e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
dropZone.classList.remove('bg-gray-700');
|
|
|
|
|
});
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
dropZone.addEventListener('drop', function (e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
dropZone.classList.remove('bg-gray-700');
|
|
|
|
|
const files = e.dataTransfer?.files;
|
|
|
|
|
if (files && files.length > 0) {
|
|
|
|
|
const pdfFiles = Array.from(files).filter(function (f) {
|
|
|
|
|
return (
|
|
|
|
|
f.type === 'application/pdf' ||
|
|
|
|
|
f.name.toLowerCase().endsWith('.pdf')
|
|
|
|
|
);
|
2025-12-11 19:34:14 +05:30
|
|
|
});
|
2026-03-26 12:11:12 +05:30
|
|
|
if (pdfFiles.length > 0) {
|
|
|
|
|
const dataTransfer = new DataTransfer();
|
|
|
|
|
dataTransfer.items.add(pdfFiles[0]);
|
|
|
|
|
handleFileSelect(dataTransfer.files);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
fileInput.addEventListener('click', function () {
|
|
|
|
|
fileInput.value = '';
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-12-11 19:34:14 +05:30
|
|
|
|
2026-03-26 12:11:12 +05:30
|
|
|
if (processBtn) {
|
|
|
|
|
processBtn.addEventListener('click', removeMetadata);
|
|
|
|
|
}
|
2025-12-11 19:34:14 +05:30
|
|
|
});
|