+ Start a Discussion
lakshman.mattilakshman.matti 

Sending Vf page as PDF as attachment.

Hi Everyone,

I have create a vf page and custom controller .page contains 2 input to take user inputs.when user gives values and saves the record using custom button.once it is saved i need to send that page vf page as attachment in pdf format to the mail what we have speciefied in vf page.
here i'm pasting my code.please corrrect my code.
VF PAGE:
---------------------

<apex:page controller="sendingMailAttachment"> <apex:form > <apex:messages /> <apex:outputLabel >Name</apex:outputLabel>&nbsp; &nbsp; &nbsp; <apex:inputText value="{!name}"/><br/> <apex:outputLabel >Mail</apex:outputLabel>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; <apex:inputText value="{!cmail}"/><br/> <apex:commandButton action="{!Submit}" value="Submit"/> </apex:form> </apex:page>
controller:
-----------
public class sendingMailAttachment {

    public String cmail { get; set; }

    public String name { get; set; }
   
    public sendingMailAttachment (){}
    public pagereference Submit(){
    
    contact Conval=new contact();
    conval.lastname=name ;
    conval.Email=cmail ;
    insert conval;
    
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.info, 'contact inserted');
        ApexPages.addMessage(myMsg); 
        
        
        
        // Reference the attachment page and pass in the  ID
        PageReference pdf =  Page.emailAttachmentEx1;
       pdf.getParameters().put('id',conval);
        // Take the PDF content
        Blob b = pdf.getContentAsPDF();
        // Create the email attachment
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setContentType('application/pdf');
        efa.setFileName('attachment.pdf');
        efa.setInline(false);
        efa.setBody(b);
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {conval.Email};
        mail.setToAddresses(toAddresses);
        mail.setSubject('New contact is created'); 
        
        mail.plainTextBody = 'Name : '+conval.lastname+'\n'+'Mail Id : '+conval.email;
        
        mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
        Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
        return null;
    }
    
}
---
above code is sending attachment with just labels. i want the inputs user give along with labels.

Thanks
Lakshman
 
Rahul SharmaRahul Sharma
Shouldn't following line:
pdf.getParameters().put('id',conval);

be:
pdf.getParameters().put('id',conval.Id);

Can you elaborate your problem:
----
above code is sending attachment with just labels. i want the inputs user give along with labels.
----
Abhishek_DEOAbhishek_DEO
It seems you are generating PDF of same page where user inputs the data. It is not a good idea as it contains inputText compoent. 

You may creata a new VF page and controller. Query the record using ID and just display the data on page. Use this page as a refernce of PDF in above controller "sendingMailAttachment". 

Also, as Rahul mentioned it should be ID that you have to pass instead of object itsself. 
Arun Deepan LJArun Deepan LJ
Duplicate the same visual page, and just add render as PDF , in that duplicated page with same controllers and extension