2025-10-12 11:55:45 +05:30
|
|
|
import { showLoader, hideLoader, showAlert } from '../ui.js';
|
|
|
|
|
import { downloadFile } from '../utils/helpers.js';
|
|
|
|
|
import { state } from '../state.js';
|
|
|
|
|
|
|
|
|
|
export async function flatten() {
|
2025-10-17 11:37:32 +05:30
|
|
|
if (!state.pdfDoc) {
|
|
|
|
|
showAlert('Error', 'PDF not loaded.');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
showLoader('Flattening PDF...');
|
|
|
|
|
try {
|
|
|
|
|
const form = state.pdfDoc.getForm();
|
|
|
|
|
form.flatten();
|
2025-10-12 11:55:45 +05:30
|
|
|
|
2025-10-17 11:37:32 +05:30
|
|
|
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.');
|
2025-10-12 11:55:45 +05:30
|
|
|
}
|
2025-10-17 11:37:32 +05:30
|
|
|
} finally {
|
|
|
|
|
hideLoader();
|
|
|
|
|
}
|
|
|
|
|
}
|