• Anoop Krish Kuniyil
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 2
    Replies
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>
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>
Hi All

I would like to add digial signature on the pdf attachement send in email to secure the document from forgery. Is this possible when we use email template with type visualforce?

<messaging:emailTemplate subject="Quote Created" recipientType="Contact" relatedToType="Quote" >
<messaging:htmlEmailBody >
<c:QuoteEmailBody recordID="{!relatedTo.Id}"/>
</messaging:htmlEmailBody>
<messaging:attachment renderAs="pdf" filename="QuotationSlip">
    <c:QuoteSlipDocAttachment recordID="{!relatedTo.Id}"/>
</messaging:attachment>
</messaging:emailTemplate>

QuoteSlipDocAttachment is visualfoce component which will be rendered as pdf when the workflow trigger. Is there anyway i can protect the document using digital signature?
There is a requirement to make http callout using PATCH method to update some records on the external application. I understand from the forum HttpRequest doesnt support PATCH method and the workaround solution to use the PATCH request with HttpRequest Class is,

#1. setHeader('X-HTTP-Method-Override','PATCH');
#2. Append "?_HttpMethod=PATCH" as prefix in the endpoint URL

I have tried both the option but the server is returing 404  - Not Found Error. Can someone tell me other alternative option available?
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>
There is a requirement to make http callout using PATCH method to update some records on the external application. I understand from the forum HttpRequest doesnt support PATCH method and the workaround solution to use the PATCH request with HttpRequest Class is,

#1. setHeader('X-HTTP-Method-Override','PATCH');
#2. Append "?_HttpMethod=PATCH" as prefix in the endpoint URL

I have tried both the option but the server is returing 404  - Not Found Error. Can someone tell me other alternative option available?