Rahi
Rahi

Reputation: 1

Getting error while trying to apply Digital signature in PDF file on PDFTron webviewer (Apyrse)

I am trying to apply digital signature in the pdf file in web viewer but I am getting error. Below is my code to apply digital signature.

WebViewer({
path: “/PdfTronWebViewer/”,
enableMeasurement: true,
licenseKey: ‘key’,
backendType: ‘ems’,
fullAPI:true
}, viewer
).then(async instance => {

const { PDFNet, documentViewer } = instance.Core;
documentViewer.addEventListener(‘documentLoaded’, async () => {
const doc = await documentViewer.getDocument().getPDFDoc();

        // Run PDFNet methods with memory management
        await PDFNet.runWithCleanup(async () => {

            doc.lock();
            const page1 = await doc.getPage(1);
            const sigHandlerId = await doc.addStdSignatureHandlerFromURL("/CertificatePath.pfx", 'password');

            const certification_sig_field = await doc.createDigitalSignatureField("pdfSignatureField");
            const widgetAnnot = await PDFNet.SignatureWidget.createWithDigitalSignatureField(doc, new PDFNet.Rect(0, 100, 200, 150), certification_sig_field);

            await page1.annotPushBack(widgetAnnot);

            const foundCertificationField = await doc.getField("pdfSignatureField");
            const certificationSigField = await PDFNet.DigitalSignatureField.createFromField(foundCertificationField);

            const img = await PDFNet.Image.createFromURL(doc, "/signature.png");
            const certifySignatureWidget = await PDFNet.SignatureWidget.createWithDigitalSignatureField(
                doc,
                await PDFNet.Rect.init(0, 100, 200, 150),
                certificationSigField
            );

            await certifySignatureWidget.createSignatureAppearance(img);
            page1.annotPushBack(certifySignatureWidget); 

            certificationSigField.setDocumentPermissions(PDFNet.DigitalSignatureField.DocumentPermissions.e_no_changes_allowed);


            await certificationSigField.certifyOnNextSaveWithCustomHandler(sigHandlerId);

            // The actual certification signing will be done during the save operation.
            const buf = await doc.saveMemoryBuffer(0);
            const blob = new Blob([buf], { type: 'application/pdf' });
            saveAs(blob, 'certified_doc.pdf');
        });
    }); 
 }); 

Below is the error which I am getting in the above code:

PDFNet.js:1171 Uncaught (in promise) Error: Function DigitalSignatureField.setDocumentPermissions recently altered a struct object without yielding. That object is now being accessed by function 'certifyOnNextSaveWithCustomHandler'. Perhaps a yield statement is required for DigitalSignatureField.setDocumentPermissions? at checkThisYieldFunction (PDFNet.js:1171:423) at PDFNet.DigitalSignatureField.certifyOnNextSaveWithCustomHandler (PDFNet.js:831:155) at DocumentViewer.js:108:45

Please let me know why am I getting this error.

Upvotes: 0

Views: 55

Answers (0)

Related Questions