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
Akshay More 11Akshay More 11 

Hello , I'm trying to send visualforce page content as an email attachment in pdf format ,but when I download the attachment from mail and try to open it it gives me error like corrupted file or unsupported file

Hello , I'm trying to send visualforce page content as an  email attachment in pdf format ,but when I download the attachment from mail and try to open it it gives me error like corrupted file or unsupported file

VF Page : -
 
 <apex:page standardController="VS_Quote__c" extensions="Quoteaspdf,sendQEmail,pmsg"  lightningStylesheets="true" showHeader="false" sidebar="false">
<div style="margin-left:240px;margin-right:240px;">
    <head>
        <style>
            body { font-family: 'Arial Unicode MS'; }
        </style>
    </head>
    <apex:form >
        <table style="width:100%;">
            <tbody>
                <tr>
                    <td style="width:256px;">
                        <img style="float: left;width:90px;height:30px;" src="https://c.ap4.visual.force.com/resource/1533719597000/vyomlogo" alt="" width="227" height="200" />
                       
                    </td>
                    <td style="text-align:right;">
                         <p style="font-size:10px;"><br/>Phone:7276852020<br/>Website: www.Vyomlabs.com</p>    
                    </td>
               </tr>
            </tbody>
        </table>
        
            
       
        <apex:pageBlock  >
            <div style="margin-left:450px;margin-top:40px;font-size:14px;">
                   <table>
                          <tr>
                            <th>Created Date</th>
                            <td>&nbsp;<apex:outputText value="{0,date,MM'/'dd'/'yyyy}"><apex:param value="{!VS_Quote__c.Created_Date__c}" /> </apex:outputText></td>
                          </tr>
                          <tr>
                            <th>Expiration Date</th>
                            <td>&nbsp;<apex:outputText value="{0,date,MM'/'dd'/'yyyy}"><apex:param value="{!VS_Quote__c.Expiration_Date__c}" /> </apex:outputText></td>
                          </tr>
                          <tr>
                            <th>Quote Number</th>
                            <td>&nbsp;{!VS_Quote__c.Quote_Number__c}</td>
                          </tr>
                </table>
            </div>
            <br/><br/>
            <div style="margin-left:450px;font-size:14px;">
                 <table>
                          <tr>
                            <th>Contact Name</th>
                            <td>&nbsp;&nbsp;{!VS_Quote__c.Contact_Name__c}</td>
                          </tr>
                          <tr>
                            <th>Phone</th>
                            <td>&nbsp;&nbsp;{!VS_Quote__c.Phone__c}</td>
                          </tr>
                          <tr>
                            <th>Email</th>
                            <td>&nbsp;&nbsp;{!VS_Quote__c.Email__c}</td>
                          </tr>
                </table>
            </div>
             <div style="margin-left:80px;margin-top:-58px;font-size:14px;">
                   <table>
                          <tr>
                            <th>Prepared By</th>
                            <td></td>
                          </tr>
                          <tr>
                            <th>Email</th>
                            <td>&nbsp;&nbsp;{!VS_Quote__c.Email__c}</td>
                          </tr>
                          
                </table>
            </div>
            <div style="margin-left:80px;margin-top:26px;font-size:14px;">
                   <table>
                          <tr>
                            <th>Bill To Name</th>
                            <td>&nbsp;&nbsp;{!VS_Quote__c.Bill_To_Name__c}</td>
                          </tr>
                          <tr>
                            <th>Bill To</th>
                            <td>&nbsp;&nbsp;{!VS_Quote__c.Bill_To_Street__c} , {!VS_Quote__c.Bill_To_City__c}</td>
                          </tr>
                          
                </table>
            </div>
            <div style="margin-left:450px;margin-top:-47px;font-size:14px;">
                   <table>
                          <tr>
                            <th>Ship To Name</th>
                            <td>&nbsp;&nbsp;{!VS_Quote__c.Ship_To_Name__c}</td>
                          </tr>
                </table>
            </div>
            <div style="margin-top:60px;">
                <table style="width:80%;margin-left:80px;">
                    <tr style="text-align:right;color:white;font-size:10pt;text-color:white;background-color:#5D6D7E;">
                        <th style="text-align:left;">Product</th>
                        <th>List Price</th>
                        <th>Sales Price</th>
                        <th>Quantity</th>
                        <th>Total Price</th>
                    </tr>
                     
                    <apex:repeat value="{!opportunityProd}" var="a">
                        <tr>
                            <td style="text-align:left;" >{!a.Name}</td>
                            <td style="text-align:center;" >-</td>
                            <td style="text-align:center;">{!a.Sales_Price__c}</td>
                            <td style="text-align:center;">{!a.Quantity__c}</td>
                            <td style="text-align:right;">{!a.Total_Amount__c}</td>
                        </tr>
                     
                     </apex:repeat>
                </table>    
            </div>
            
            
            <apex:pageBlockButtons style="align:center;">
                
                <apex:commandButton action="{!send}" value="Email Quote" onclick="OpenPage('blog'); return false;"/>
                <apex:commandButton value="Save As PDF" onclick="OpenPage('google'); return false;"/>
                
            </apex:pageBlockButtons>    
        </apex:pageBlock>
    </apex:form>
</div>    
</apex:page>


Controller Extension : -

public class sendQEmail
{
    public String subject{get;set;}
    public String body{get;set;}
    String QuoteID;
    public List <String> ToAddresses{get;set;}
    public sendQEmail(ApexPages.StandardController cltr)
    {
        ToAddresses = new List <String>();
        QuoteID=cltr.getRecord().id;
        ToAddresses.add('**********@gmail.com');
    }
    public PageReference send()
    {    
        
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        PageReference pdf =  Page.createquotepdf;
        pdf.getParameters().put('id',QuoteID);
        pdf.setRedirect(true);
        Blob b = pdf.getContent();
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setContentType('application/pdf');
        efa.setFileName('Quote.pdf');
        efa.setBody(b);
              
        
        email.setSubject('Quote PDF');
        email.setToAddresses(ToAddresses);
        email.setSaveAsActivity(true);
        
        email.setPlainTextBody('Please find the attachment containing the pdf of Quote');
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});  
        return null;
    }
}


User-added image

Thanks In advanced
Akshay
Best Answer chosen by Akshay More 11
Akshay More 11Akshay More 11
Thanks 
Khan Anas,
for your quick response. I have fixed my issue, it was because of   "lightningStylesheets "  tag in a visualforce page, and the 'gel content()' method.
I changed the method to getContentAsPDF() and removed the tag from the visualforce page, now it is working fine as per expectation.

Thanks,
Akshay More


 

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Akshay,

I trust you are doing very well.

The problem could be that you're using Components That Are Unsafe to Use When Rendering as PDF such as <apex:form>, <apex:pageBlock>, <apex:pageBlockButtons>. If you look at Best Practices for Rendering PDFs (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_output_pdf_component_behavior.htm) you'll see that these components are listed under "Components That Are Unsafe to Use in a PDF". You can use HTML. And, you might have an image referenced from Static Resource which could cause this issue.

Please refer to the below link with a similar discussion which might help you further with the above issue.

https://salesforce.stackexchange.com/questions/18675/corrupt-pdf-when-sending-as-email-attachment-from-apex

https://salesforce.stackexchange.com/questions/82127/visual-force-page-when-attached-as-pdf-in-email-is-getting-corrupted

https://developer.salesforce.com/forums/?id=906F000000093T6IAI

https://developer.salesforce.com/forums/?id=906F00000009448IAA


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas

 
Akshay More 11Akshay More 11
Thanks 
Khan Anas,
for your quick response. I have fixed my issue, it was because of   "lightningStylesheets "  tag in a visualforce page, and the 'gel content()' method.
I changed the method to getContentAsPDF() and removed the tag from the visualforce page, now it is working fine as per expectation.

Thanks,
Akshay More


 
This was selected as the best answer