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

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.

<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>
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(, 'contact inserted');
        // Reference the attachment page and pass in the  ID
        PageReference pdf =  Page.emailAttachmentEx1;
        // Take the PDF content
        Blob b = pdf.getContentAsPDF();
        // Create the email attachment
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {conval.Email};
        mail.setSubject('New contact is created'); 
        mail.plainTextBody = 'Name : '+conval.lastname+'\n'+'Mail Id : ';
        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.

Rahul SharmaRahul Sharma
Shouldn't following line:


Can you elaborate your problem:
above code is sending attachment with just labels. i want the inputs user give along with labels.
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