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
AbAb 

covert webpage to pdf and attach as attachment to email or an account or anything


Hello,

I am trying to send a pdf version of webpage to an email address.
but i always receive a blank page as attachment.
How can i resolve it.
 
<apex:page docType="html-5.0" showHeader="false" controller="TestWebPageToPdfToDelete" sidebar="false">
<apex:form>
<apex:commandButton action="{!SendThisEmail}" value="send" />
<apex:iframe src="https://google.com" scrolling="true"/> 
</apex:form>
</apex:page>


 
public class TestWebPageToPdfToDelete {

     public void SendThisEmail()
    {
        
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
        attach.setContentType('application/pdf');
        attach.setFileName('Report.pdf');
        pagereference Pg = Page.TestWebPageToPdfToDelete;
        Blob body = pg.getcontentAsPdf();
        System.debug('$$'+body.size());
        System.debug('$$'+body);
        attach.Body = body;
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new String[] { 'test@gmail.com' });
        mail.setSubject('PDF Generation');
        mail.setHtmlBody('PFA');
        mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });    
        mails.add(mail); 
       
        if(!mails.isEmpty()) {
            Messaging.SendEmail(mails);
        }
    }
}

It is not necessary for me to send email but i just want to attach it as attachment somewhere and i should see as Pdf.

thanks for suggestion
Best Answer chosen by Ab
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Sandrine,

May I request you to please refer the below link for reference. I hope it will be helpful.

BestRegards
RahulKumar