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
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

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.

<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="{!}"/>
     <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="{!}"/>
     <apex:pageBlockSectionItem >
     <apex:outputLabel value="Enter type:"/>
     <apex:inputText id="type" value="{!Account.Type}"/>
     <apex:commandButton value="Send" action="{!send}"/>


public with sharing class PdfAsEmail {

    public PdfAsEmail(ApexPages.StandardController controller) {

    public PageReference send(){
     Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        PageReference pdf =  Page.attachmentPDF;

        Blob b = pdf.getContent();

        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
    String[] toAddresses = new String[]{''};
        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