From 245b48464bc0530a97aaa58872b364a70a83aa21 Mon Sep 17 00:00:00 2001 From: alam00000 Date: Sun, 29 Mar 2026 20:29:31 +0530 Subject: [PATCH] feat: add error handling for flattening PDF forms and annotations --- src/js/logic/sign-pdf-page.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/js/logic/sign-pdf-page.ts b/src/js/logic/sign-pdf-page.ts index 3e1497b..e2af79d 100644 --- a/src/js/logic/sign-pdf-page.ts +++ b/src/js/logic/sign-pdf-page.ts @@ -8,6 +8,7 @@ import { import { loadPdfWithPasswordPrompt } from '../utils/password-prompt.js'; import { t } from '../i18n/i18n'; import { loadPdfDocument } from '../utils/load-pdf-document.js'; +import { flattenAnnotations } from '../utils/flatten-annotations.js'; import type { SignState, PDFViewerWindow } from '@/types'; const signState: SignState = { @@ -283,7 +284,20 @@ async function applyAndSaveSignatures() { ); const pdfBytes = new Uint8Array(rawPdfBytes); const pdfDoc = await loadPdfDocument(pdfBytes); - pdfDoc.getForm().flatten(); + try { + pdfDoc.getForm().flatten(); + } catch (e: unknown) { + const msg = e instanceof Error ? e.message : String(e); + if (!msg.includes('getForm')) { + throw e; + } + } + try { + flattenAnnotations(pdfDoc); + } catch (e: unknown) { + const msg = e instanceof Error ? e.message : String(e); + console.warn('Could not flatten annotations:', msg); + } const flattenedPdfBytes = await pdfDoc.save(); const blob = new Blob([new Uint8Array(flattenedPdfBytes)], {