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
Niki Shah 12Niki Shah 12 

How to create functionality for save as pdf button on same page and send to email in visual force page

How to create save and email functionality on custom pdf page 
Deepali KulshresthaDeepali Kulshrestha
Hi Niki,

Please try these line of code as it may help you

Create a button in your VF page and call this method on "onClick of that button".

put this method in your controller.
 
public static void sendPDf() {

    PageReference pageRef = ApexPages.currentPage();
    Blob pdf1 = pageRef.getcontentAsPdf();
    Attachment d = new Attachment();
    d.ParentId = UserInfo.getUserId(); //you can also set the Id of the record in which you want to save this pdf attachment
    d.Body = Pdf1;
    d.Name = 'pdf_file.pdf';
    d.ContentType = 'application/pdf';
    insert d;


    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
    efa.setFileName('Confirmation of product.PDf');
    efa.setBody(pdf1);
    List < String > listEmailMembers = new List < String > ();
    listEmailMembers.add(Email); // add Email of the members.
    emailTobeSent.setToAddresses(listEmailMembers);
    emailTobeSent.setSubject('Confirmation of product');
    emailTobeSent.setHtmlBody('Hi');
    emailTobeSent.setFileAttachments(new Messaging.EmailFileAttachment[] {
        efa
    }); // Sends the email
    Messaging.SendEmailResult[] r1 = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {
        emailTobeSent
    });
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha