2025-10-12 11:55:45 +05:30
|
|
|
import { showLoader, hideLoader, showAlert } from '../ui.js';
|
2025-10-24 13:42:09 +05:30
|
|
|
import {
|
|
|
|
|
downloadFile,
|
|
|
|
|
initializeQpdf,
|
|
|
|
|
readFileAsArrayBuffer,
|
|
|
|
|
} from '../utils/helpers.js';
|
2025-10-12 11:55:45 +05:30
|
|
|
import { state } from '../state.js';
|
|
|
|
|
|
|
|
|
|
export async function encrypt() {
|
2025-10-17 11:37:32 +05:30
|
|
|
const file = state.files[0];
|
2025-10-24 00:11:18 +05:30
|
|
|
const userPassword =
|
|
|
|
|
(document.getElementById('user-password-input') as HTMLInputElement)
|
|
|
|
|
?.value || '';
|
2025-10-24 11:37:15 +05:30
|
|
|
const ownerPasswordInput =
|
2025-10-24 00:11:18 +05:30
|
|
|
(document.getElementById('owner-password-input') as HTMLInputElement)
|
|
|
|
|
?.value || '';
|
|
|
|
|
|
|
|
|
|
if (!userPassword) {
|
|
|
|
|
showAlert('Input Required', 'Please enter a user password.');
|
2025-10-17 11:37:32 +05:30
|
|
|
return;
|
|
|
|
|
}
|
2025-10-12 11:55:45 +05:30
|
|
|
|
2025-10-24 11:37:15 +05:30
|
|
|
const ownerPassword = ownerPasswordInput || userPassword;
|
|
|
|
|
const hasDistinctOwnerPassword = ownerPasswordInput !== '';
|
|
|
|
|
|
2025-10-24 00:11:18 +05:30
|
|
|
const inputPath = '/input.pdf';
|
|
|
|
|
const outputPath = '/output.pdf';
|
|
|
|
|
let qpdf: any;
|
|
|
|
|
|
2025-10-17 11:37:32 +05:30
|
|
|
try {
|
2025-10-24 00:11:18 +05:30
|
|
|
showLoader('Initializing encryption...');
|
|
|
|
|
qpdf = await initializeQpdf();
|
|
|
|
|
|
|
|
|
|
showLoader('Reading PDF...');
|
|
|
|
|
const fileBuffer = await readFileAsArrayBuffer(file);
|
|
|
|
|
const uint8Array = new Uint8Array(fileBuffer as ArrayBuffer);
|
|
|
|
|
|
|
|
|
|
qpdf.FS.writeFile(inputPath, uint8Array);
|
|
|
|
|
|
|
|
|
|
showLoader('Encrypting PDF with 256-bit AES...');
|
|
|
|
|
|
2025-10-24 13:42:09 +05:30
|
|
|
const args = [inputPath, '--encrypt', userPassword, ownerPassword, '256'];
|
2025-10-24 00:11:18 +05:30
|
|
|
|
2025-10-24 11:37:15 +05:30
|
|
|
// Only add restrictions if a distinct owner password was provided
|
|
|
|
|
if (hasDistinctOwnerPassword) {
|
2025-10-24 00:11:18 +05:30
|
|
|
args.push(
|
|
|
|
|
'--modify=none',
|
2025-10-24 13:42:09 +05:30
|
|
|
'--extract=n',
|
|
|
|
|
'--print=none',
|
|
|
|
|
'--accessibility=n',
|
|
|
|
|
'--annotate=n',
|
|
|
|
|
'--assemble=n',
|
|
|
|
|
'--form=n',
|
|
|
|
|
'--modify-other=n'
|
2025-10-24 00:11:18 +05:30
|
|
|
);
|
2025-10-17 11:37:32 +05:30
|
|
|
}
|
2025-10-12 11:55:45 +05:30
|
|
|
|
2025-10-24 00:11:18 +05:30
|
|
|
args.push('--', outputPath);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
qpdf.callMain(args);
|
|
|
|
|
} catch (qpdfError: any) {
|
|
|
|
|
console.error('qpdf execution error:', qpdfError);
|
|
|
|
|
throw new Error(
|
|
|
|
|
'Encryption failed: ' + (qpdfError.message || 'Unknown error')
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showLoader('Preparing download...');
|
|
|
|
|
const outputFile = qpdf.FS.readFile(outputPath, { encoding: 'binary' });
|
|
|
|
|
|
|
|
|
|
if (!outputFile || outputFile.length === 0) {
|
|
|
|
|
throw new Error('Encryption resulted in an empty file.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const blob = new Blob([outputFile], { type: 'application/pdf' });
|
|
|
|
|
downloadFile(blob, `encrypted-${file.name}`);
|
|
|
|
|
|
|
|
|
|
hideLoader();
|
|
|
|
|
|
|
|
|
|
let successMessage = 'PDF encrypted successfully with 256-bit AES!';
|
2025-10-24 11:37:15 +05:30
|
|
|
if (!hasDistinctOwnerPassword) {
|
2025-10-24 00:11:18 +05:30
|
|
|
successMessage +=
|
2025-10-24 11:37:15 +05:30
|
|
|
' Note: Without a separate owner password, the PDF has no usage restrictions.';
|
2025-10-24 00:11:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showAlert('Success', successMessage);
|
|
|
|
|
} catch (error: any) {
|
2025-10-17 11:37:32 +05:30
|
|
|
console.error('Error during PDF encryption:', error);
|
|
|
|
|
hideLoader();
|
2025-10-24 00:11:18 +05:30
|
|
|
showAlert(
|
|
|
|
|
'Encryption Failed',
|
|
|
|
|
`An error occurred: ${error.message || 'The PDF might be corrupted.'}`
|
|
|
|
|
);
|
|
|
|
|
} finally {
|
|
|
|
|
try {
|
|
|
|
|
if (qpdf?.FS) {
|
|
|
|
|
try {
|
|
|
|
|
qpdf.FS.unlink(inputPath);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.warn('Failed to unlink input file:', e);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
qpdf.FS.unlink(outputPath);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.warn('Failed to unlink output file:', e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (cleanupError) {
|
|
|
|
|
console.warn('Failed to cleanup WASM FS:', cleanupError);
|
|
|
|
|
}
|
2025-10-17 11:37:32 +05:30
|
|
|
}
|
2025-10-24 13:42:09 +05:30
|
|
|
}
|