Skip to content

Commit 36eb8fc

Browse files
committed
fix(api): handle encrypted PDFs in NDA PDF processing
1 parent 26f86ee commit 36eb8fc

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

apps/api/src/trust-portal/nda-pdf.service.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { Injectable, Logger } from '@nestjs/common';
2-
import { PDFDocument, rgb, StandardFonts, degrees } from 'pdf-lib';
2+
import {
3+
PDFDocument,
4+
rgb,
5+
StandardFonts,
6+
degrees,
7+
EncryptedPDFError,
8+
} from 'pdf-lib';
39
import { AttachmentsService } from '../attachments/attachments.service';
410

511
@Injectable()
@@ -242,7 +248,23 @@ By signing below, the Receiving Party agrees to be bound by the terms of this Ag
242248
},
243249
): Promise<Buffer> {
244250
const { name, email, docId, watermarkText } = params;
245-
const pdfDoc = await PDFDocument.load(pdfBuffer);
251+
252+
let pdfDoc: PDFDocument;
253+
try {
254+
pdfDoc = await PDFDocument.load(pdfBuffer);
255+
} catch (error) {
256+
if (error instanceof EncryptedPDFError) {
257+
// Encrypted PDF - return as-is without watermark
258+
// User already signed NDA for accountability, encrypted PDFs require password anyway
259+
this.logger.debug(
260+
`Skipping watermark for PDF ${docId} - file is encrypted`,
261+
);
262+
return pdfBuffer;
263+
}
264+
// Re-throw other parsing errors
265+
throw error;
266+
}
267+
246268
await this.addWatermark(pdfDoc, name, email, docId, watermarkText);
247269
const pdfBytes = await pdfDoc.save();
248270
return Buffer.from(pdfBytes);

0 commit comments

Comments
 (0)