+ Start a Discussion
sai mogantisai moganti 

Pdf should generate when button is clicked

Hai all,
         I have 10 fields in VF page.I had button on VF page name "A".When i Clicked A button VF page data should be converted to PDF and that PDF should to gmail.Can any one help me.

Thanks in advance..
Harish M 8Harish M 8
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_sending_attachments.htm

follow this link,

in ur VF put renderAs like -->   <apex:page  renderAs="PDF">
then create a apex class "sendPdfAsEmail"
in this class write call apex page while sending email

then call this apex class in button.
sai mogantisai moganti
Hai harish,
            I had written  sample code but its not working.The problem is after writing code while saving the vf page only the fields are rendered as pdf and sending mail.My intension only when we click on "send" button then only the fields should be rendered as pdf and send mail. can you plzz modify the code and revert me back.

VF PAGE:
<apex:page Standardcontroller="Account" extensions="PdfAsEmail" showHeader="false" action="{!send}" renderAs="PDF" >
<apex:form >
   <apex:pageBlock >
   <apex:pageBlockSection columns="1" >
   <apex:pageBlockSectionItem >
     <apex:outputLabel value="Enter Name:"/>
     <apex:inputText id="name" value="{!Account.name}"/>
     </apex:pageBlockSectionItem>
     <apex:pageBlockSectionItem >
     <apex:outputLabel value="Enter Industry:"/>
     <apex:inputText id="industry" value="{!Account.Industry}"/>
     </apex:pageBlockSectionItem><apex:pageBlockSectionItem >
     <apex:outputLabel value="Enter Phone:"/>
     <apex:inputText id="phone" value="{!Account.phone}"/>
     </apex:pageBlockSectionItem>
     <apex:pageBlockSectionItem >
     <apex:outputLabel value="Enter type:"/>
     <apex:inputText id="type" value="{!Account.Type}"/>
     </apex:pageBlockSectionItem>   
     </apex:pageBlockSection>
     </apex:pageBlock>
     <apex:commandButton value="Send" action="{!send}"/>
</apex:form>
</apex:page>


Controller

public with sharing class PdfAsEmail {

    public PdfAsEmail(ApexPages.StandardController controller) {

    }
    public PageReference send(){
    
     Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
 
        PageReference pdf =  Page.attachmentPDF;
        //pdf.getParameters().put('id',(String)account.id);
        //pdf.setRedirect(true);

        Blob b = pdf.getContent();

        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName('attachment.pdf');
        efa.setBody(b);
    
    
    String[] toAddresses = new String[]{'sai.moganti@gmail.com'};
        email.setSubject( 'subject' );
        email.setToAddresses( toAddresses );
        email.setPlainTextBody( 'body' );
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
        Messaging.SendEmailResult [] r =Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    
    return null;
    }
}


Thanks in Advance










 
sai mogantisai moganti
can any one help meee waiting for the response