Search code examples
triggerssalesforcepdf-generationapexvisualforce

Generating a coruppted PDF file from a visual force page


I am trying to generate a PDF file attachment in an after update trigger and below is the trigger handler method but the file sent is corrupted.

public static void sendEOB(List<Claim_Payment__c> newList,Map<Id, Claim_Payment__c> oldMap){
        for(Claim_Payment__c claimPayment : newList){
            if (claimPayment.Status__c == 'Pending'){
                String sfUrl = URL.getSalesforceBaseUrl().getHost();
                String myURL = 'https://'+sfUrl+'/apex/ClaimPayments?Id='+ claimPayment.Id;
                PageReference pdf = new PageReference(myURL);
                
                // the contents of the report in pdf form
                Blob body;
                
                try {
                    body = pdf.getContent();
                } catch (VisualforceException e) {
                    body = Blob.valueOf('PDF Get Failed');
                }
                
                Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
                attach.setContentType('application/pdf');
                attach.setFileName('Payment_Statement.pdf');
                attach.setInline(false);
                attach.Body = body;
                
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setUseSignature(false);
                mail.setToAddresses(new String[] {'[email protected]' });
                mail.setSubject('Payment Statement');
                mail.setHtmlBody('Important Statement Attachment!');
                mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); 
                
                // Send the email
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            }
        }
    } 

Update :

Using @future(callout=true) & getContentAsPDF() now i get this error message FATAL_ERROR Internal Salesforce.com Error on executing body = pdf.getContent();

public static void sendEOB(List<Claim_Payment__c> newList,Map<Id, Claim_Payment__c> oldMap){
        for(Claim_Payment__c claimPayment : newList){
            if (claimPayment.Status__c == 'Pending'){
                String sfUrl = URL.getSalesforceBaseUrl().getHost();
                String myURL = 'https://'+sfUrl+'/apex/ClaimPayments?Id='+ claimPayment.Id;
                
                sendPDFEmail.sendPDF(myURL);
            }
        } 
    }
Public class sendPDFEmail{
    @future(callout=true)
    public static void sendPDF(String myURL){
        PageReference pdf = new PageReference(myURL);
        
        // the contents of the report in pdf form
        Blob body;
        body = pdf.getContentAsPDF();
        
       // Rest of the implementation goes below
    }
}

Solution

  • What errors (if any) you see in debug log? From what I remember you might need @future(callout=true) or other asynchronous processing (Queueable etc) because calling getContent counts as callout. And shouldn't that be getContentAsPdf? or is the page already set to renderas="pdf"?

    If you download the file - what size it is, couple bytes or bigger? That try-catch is swallowing the actual error message, you don't even show it as System.debug(e); Remove the try-catch and check in the debbug what exactly it explodes on.