Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions apps/api/src/trust-portal/nda-pdf.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Injectable, Logger } from '@nestjs/common';
import { PDFDocument, rgb, StandardFonts, degrees } from 'pdf-lib';
import {
PDFDocument,
rgb,
StandardFonts,
degrees,
EncryptedPDFError,
} from 'pdf-lib';
import { AttachmentsService } from '../attachments/attachments.service';

@Injectable()
Expand Down Expand Up @@ -242,7 +248,23 @@ By signing below, the Receiving Party agrees to be bound by the terms of this Ag
},
): Promise<Buffer> {
const { name, email, docId, watermarkText } = params;
const pdfDoc = await PDFDocument.load(pdfBuffer);

let pdfDoc: PDFDocument;
try {
pdfDoc = await PDFDocument.load(pdfBuffer);
} catch (error) {
if (error instanceof EncryptedPDFError) {
// Encrypted PDF - return as-is without watermark
// User already signed NDA for accountability, encrypted PDFs require password anyway
this.logger.debug(
`Skipping watermark for PDF ${docId} - file is encrypted`,
);
return pdfBuffer;
}
// Re-throw other parsing errors
throw error;
}

await this.addWatermark(pdfDoc, name, email, docId, watermarkText);
const pdfBytes = await pdfDoc.save();
return Buffer.from(pdfBytes);
Expand Down