Implement a new PDF sanitization tool that allows users to remove various potentially sensitive elements from PDFs including metadata, annotations, JavaScript, embedded files, and more. The tool provides configurable options through checkboxes to selectively remove different types of content while preserving the core document structure. Extract reusable utility functions from existing tools (remove-metadata, remove-annotations, flatten) to support the new sanitization feature. The tool handles edge cases gracefully and provides feedback when no changes are made.
38 lines
918 B
TypeScript
38 lines
918 B
TypeScript
import { showLoader, hideLoader, showAlert } from '../ui.js';
|
|
import { downloadFile } from '../utils/helpers.js';
|
|
import { state } from '../state.js';
|
|
|
|
export function flattenFormsInDoc(pdfDoc) {
|
|
const form = pdfDoc.getForm();
|
|
form.flatten();
|
|
}
|
|
|
|
export async function flatten() {
|
|
if (!state.pdfDoc) {
|
|
showAlert('Error', 'PDF not loaded.');
|
|
return;
|
|
}
|
|
showLoader('Flattening PDF...');
|
|
try {
|
|
flattenFormsInDoc(state.pdfDoc);
|
|
|
|
const flattenedBytes = await state.pdfDoc.save();
|
|
downloadFile(
|
|
new Blob([flattenedBytes], { type: 'application/pdf' }),
|
|
'flattened.pdf'
|
|
);
|
|
} catch (e) {
|
|
console.error(e);
|
|
if (e.message.includes('getForm')) {
|
|
showAlert(
|
|
'No Form Found',
|
|
'This PDF does not contain any form fields to flatten.'
|
|
);
|
|
} else {
|
|
showAlert('Error', 'Could not flatten the PDF.');
|
|
}
|
|
} finally {
|
|
hideLoader();
|
|
}
|
|
}
|