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
Mahendiran JayavarmaMahendiran Jayavarma 

convert pageblocktable data into PDF adn send mail as attachment PDF

Hi,

 

If i am having pageblocktable and button control in a page.

 

when i click that button I need to send mail for pageblocktable records as a PDF format.

 

I tried like

 

 


<apex:page controller="sendEmail">
<apex:form>
<apex:pageblockTable value="{!Activities}" var="c" id="results" > <apex:column > <apex:facet name="header"> <apex:inputCheckBox value="{!selectAllCheckbox}" onclick="checkAll(this)"> <apex:actionSupport event="onclick" action="{!selectAll}" rerender="results"/> </apex:inputCheckBox> </apex:facet> <apex:inputCheckBox value="{!c.selected}"/> </apex:column> <apex:column value="{!c.acct.subject}"/> <apex:column value="{!c.acct.Id}"/> <apex:column value="{!c.acct.owner.name}"/> <apex:column value="{!c.acct.Billable_Hrs__c}"/> <apex:column value="{!c.acct.Rate_Hrs__c}"/> <apex:column value="{!c.acct.Billable_Amount__c}"/> </apex:pageblockTable>
<apex:commandbutton value="send mail" action="{!send}"/>

</apex:form>
</apex:page>





conroller:

public class sendEmail { 
 

    public PageReference send() {
        // Define the email  
    
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 

  

        // Take the PDF content
 
  PageReference pdf = page.sendemail;
  pdf.getParameters().get('results');
  Blob b = pdf.getContent();
  

        // Create the email attachment
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName('attachment.pdf');
        efa.setBody(b);


  

        // Sets the paramaters of the email  
    
        email.setSubject( subject );
        email.setToAddresses(new string[] {'mahe2682@gmail.com'});
 
        email.setPlainTextBody( body );

        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

        // Sends the email  
    
        Messaging.SendEmailResult [] r = 
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   
        
        return null;
    }
}



 

 

 

 

actually It will generate pdf  file but i could not open the file.

 

give me solution for how to resove it.

 

Thanks,

Mahendiran.

 

 

 

 

 

bob_buzzardbob_buzzard

You can't pull back just a section of the page like that.  You'll need to create a new page that has the renderAs attribute set to PDF and only contains the pageblocktable.  You should be able to refactor the table out to a component so that you don't have to duplicate it.

Mahendiran JayavarmaMahendiran Jayavarma

can u send me sample.

 

thanks,

Mahendiran

bob_buzzardbob_buzzard

I'm afraid I don't have a sample to send you.  If you hit problems I'll endeavour to assist.

 

Mahendiran JayavarmaMahendiran Jayavarma

I done it. thanks for your support.

Aki99Aki99

Hi Mahendiran,

Could you please share the code as to how were you able to crack it.

Thank you