function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anoop Krish KuniyilAnoop Krish Kuniyil 

Attach digital signature to PDF document

Hi All

We have requirement to add digial signature authrozied by CA in PDF to ensure the security of the document send in email. 

I have created Apex class and Apex page to implement this functionality. I am not sure how to attach the signature created by Crypto.sign method in pdf document. Can anyone help me to attach certificate to PDF document?

Apex class:
public class CryptoSign {
    public static void generateCryptoSign(){
        PageReference pageRef = Page.ApexCryptoTest;
        Blob pdfDoc = pageRef.getContentAsPDF();
        
        String algorithmName = 'RSA-SHA1';
        String key = 'PKCS12 Key';
        Blob privateKey = EncodingUtil.base64Decode(key);
        
        // Using Crypto.sign PDF document generated from Apex page is digital signing
        Blob pdfDocSigned = Crypto.sign(algorithmName, pdfDoc, privateKey);
       
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setSubject('Email Subject');
        mail.setHtmlBody('Email with Signed PDF attachment');
        List<String> toAddr = new List<String>();
        toAddr.add('test@test.com');
        mail.setToAddresses(toAddr);
        
        List<Messaging.Emailfileattachment> attachments = new List<Messaging.Emailfileattachment>();
                
        Messaging.Emailfileattachment attach = new Messaging.Emailfileattachment();
        attach = new Messaging.Emailfileattachment();
        attach.setFileName('SignedPDF.pdf');
        attach.setBody(pdfDocSigned);
        attachments.add(attach);
        mail.setFileAttachments(attachments);
        
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

Apex Page:
<apex:page >
    Test Apex page
</apex:page>
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Anoop,

May I suggest you please check the below link for reference. hope it helps.

Please mark it as best answer if the information is informative.

Thanks
Rahul Kumar
Sai Krishna GurijalaSai Krishna Gurijala
Hi Anoop,

I have the same requirement, have you acheived this? please help me if you have.

Thanks,
Anoop Krish KuniyilAnoop Krish Kuniyil
Hi Sai

Unfortunately, I haven't found any solution to do the singning from SF so I have done the signing from external system and pushed the document to SF to attach in the email.

Please note the process will impact the delivery time of email to end users so you should consider this option only if you are fine with it.

Thanks
Sai Krishna GurijalaSai Krishna Gurijala
Hi Anoop, 

Thanks for the reply, I am fine with the delay in the delivery time, Could you tell me how you did it using external System, I am in urgent need of this implementation, I exactly need what you mentioned above.

Please help me with the code.

Thanks
Keira ArnotKeira Arnot
Digital signatures can be attached with pdf files in a better manner with custom shapes. Salesforce developers have used an amazing forum for us. We introducing Cocosign signature for our users on this https://cocosign.com/electronic-signature/ website. Attaching a signature can make things more precise.
Mathews StarkMathews Stark
Hey! If you are searching for the best methods to add a signature to a PDF. Then read the blog How to add signature in PDF (https://www.transferemails.com/how-to-add-signature-in-pdf/)?