从 visualforce 页面生成损坏的 PDF 文件

Generating a coruppted PDF file from a visual force page

我正在尝试在更新后触发器中生成 PDF 文件附件,下面是触发器处理程序方法,但发送的文件已损坏。

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[] {'whatever@gmail.com' });
                mail.setSubject('Payment Statement');
                mail.setHtmlBody('Important Statement Attachment!');
                mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); 
                
                // Send the email
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            }
        }
    } 

更新 :

现在使用@future(callout=true) & getContentAsPDF() 我收到此错误消息FATAL_ERROR 内部Salesforce.com 错误 执行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
    }
}

您在调试日志中看到什么错误(如果有)?据我所知,您可能需要 @future(callout=true) 或其他异步处理(Queueable 等),因为 calling getContent counts as callout。那不应该是 getContentAsPdf 吗?或者页面是否已设置为 renderas="pdf"?

如果您下载文件 - 它有多大,几字节还是更大? try-catch 吞没了实际的错误消息,你甚至没有将其显示为 System.debug(e); 删除 try-catch 并检查调试器它究竟在什么地方爆炸。