fix: normalize pdf on the server

This commit is contained in:
Mythie
2024-12-18 08:14:14 +11:00
parent c161553d1d
commit 390a317bd3
5 changed files with 51 additions and 8 deletions

View File

@ -0,0 +1,18 @@
import { PDFDocument } from 'pdf-lib';
import { flattenAnnotations } from './flatten-annotations';
import { flattenForm, removeOptionalContentGroups } from './flatten-form';
export const normalizePdf = async (pdf: Buffer) => {
const pdfDoc = await PDFDocument.load(pdf).catch(() => null);
if (!pdfDoc) {
return pdf;
}
removeOptionalContentGroups(pdfDoc);
flattenForm(pdfDoc);
flattenAnnotations(pdfDoc);
return Buffer.from(await pdfDoc.save());
};