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 

Add digital Certificate on the pdf attached in email

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 or Crypto.signWithCertificate 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 contents = pageRef.getContentAsPDF();
        Blob contentNormal = pageRef.getContentAsPDF();
        
        String algorithmName = 'RSA-SHA1';
        String key = 'PKCS12 Key';
        Blob privateKey = EncodingUtil.base64Decode(key);
        Blob signature = Crypto.sign(algorithmName, contents, privateKey);
        
        //Blob certificate = Crypto.signWithCertificate('RSA-SHA1', contents, 'Roojai_Web_Portal');
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setSubject('Test Subject');
        mail.setHtmlBody('certificate');
        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.setFileName('Test.pdf');
        //attach.setBody(signature);
        //attachments.add(attach);
        attach = new Messaging.Emailfileattachment();
        attach.setFileName('TestNormal.pdf');
        attach.setBody(contentNormal);
        attachments.add(attach);
        mail.setFileAttachments(attachments);
        
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

Apex Page:
<apex:page >
    This is a test page
</apex:page>
Adrian cisAdrian cis
Hi Anoop,

I would love to help you. Can we talk ?
Regards
Adrian
adrian(DOT)cis22(AT)gmail(DOT)com