Skip to content

Commit

Permalink
fix: handle signature annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Mythie committed Mar 3, 2024
1 parent 73aae6f commit a17d4a2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
27 changes: 25 additions & 2 deletions packages/lib/server-only/document/seal-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { nanoid } from 'nanoid';
import path from 'node:path';
import { PDFDocument } from 'pdf-lib';
import { PDFDocument, PDFSignature, rectangle } from 'pdf-lib';

import PostHogServerClient from '@documenso/lib/server-only/feature-flags/get-post-hog-server-client';
import { DOCUMENT_AUDIT_LOG_TYPE } from '@documenso/lib/types/document-audit-logs';
Expand Down Expand Up @@ -91,8 +91,31 @@ export const sealDocument = async ({

const doc = await PDFDocument.load(pdfData);

const form = doc.getForm();

// Remove old signatures
for (const field of form.getFields()) {
if (field instanceof PDFSignature) {
field.acroField.getWidgets().forEach((widget) => {
widget.ensureAP();

try {
widget.getNormalAppearance();
} catch (e) {
const { context } = widget.dict;

const xobj = context.formXObject([rectangle(0, 0, 0, 0)]);

const streamRef = context.register(xobj);

widget.setNormalAppearance(streamRef);
}
});
}
}

// Flatten the form to stop annotation layers from appearing above documenso fields
doc.getForm().flatten();
form.flatten();

for (const field of fields) {
await insertFieldInPDF(doc, field);
Expand Down
16 changes: 15 additions & 1 deletion packages/signing/helpers/addSigningPlaceholder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import signer from 'node-signpdf';
import { PDFArray, PDFDocument, PDFHexString, PDFName, PDFNumber, PDFString } from 'pdf-lib';
import {
PDFArray,
PDFDocument,
PDFHexString,
PDFName,
PDFNumber,
PDFString,
rectangle,
} from 'pdf-lib';

export type AddSigningPlaceholderOptions = {
pdf: Buffer;
Expand Down Expand Up @@ -39,6 +47,12 @@ export const addSigningPlaceholder = async ({ pdf }: AddSigningPlaceholderOption
P: pages[0].ref,
});

const xobj = widget.context.formXObject([rectangle(0, 0, 0, 0)]);

const streamRef = widget.context.register(xobj);

widget.set(PDFName.of('AP'), widget.context.obj({ N: streamRef }));

const widgetRef = doc.context.register(widget);

let widgets = pages[0].node.get(PDFName.of('Annots'));
Expand Down

0 comments on commit a17d4a2

Please sign in to comment.