mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 08:13:56 +10:00
30 lines
930 B
TypeScript
30 lines
930 B
TypeScript
import { PDFDocument } from '@cantoo/pdf-lib';
|
|
|
|
import { removePlaceholdersFromPDF } from './auto-place-fields';
|
|
import { AppError } from '../../errors/app-error';
|
|
import { flattenAnnotations } from './flatten-annotations';
|
|
import { flattenForm, removeOptionalContentGroups } from './flatten-form';
|
|
|
|
export const normalizePdf = async (pdf: Buffer) => {
|
|
const pdfDoc = await PDFDocument.load(pdf).catch((e) => {
|
|
console.error(`PDF normalization error: ${e.message}`);
|
|
|
|
throw new AppError('INVALID_DOCUMENT_FILE', {
|
|
message: 'The document is not a valid PDF',
|
|
});
|
|
});
|
|
|
|
if (pdfDoc.isEncrypted) {
|
|
throw new AppError('INVALID_DOCUMENT_FILE', {
|
|
message: 'The document is encrypted',
|
|
});
|
|
}
|
|
|
|
removeOptionalContentGroups(pdfDoc);
|
|
await flattenForm(pdfDoc);
|
|
flattenAnnotations(pdfDoc);
|
|
const pdfWithoutPlaceholders = await removePlaceholdersFromPDF(pdf);
|
|
|
|
return pdfWithoutPlaceholders;
|
|
};
|