0

I have a vf page with a custom iframe url. This page also has a button which on click captures the screenshot and mails it to the given email id.

Below is my vf page and controller:

VF Page:

<apex:page  showheader="false" sidebar="false" controller="sendEmailWithImage">
    <apex:form id="frm">
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"/>
          <button onclick="callActionFunction()">SendEmail</button>
    <apex:actionFunction action="{!sendEmail}" name="sendEmailAF" rerender="frm">
        <apex:param assignTo="{!strImageBlob}" name="imageBlob" value=""/>
    </apex:actionFunction>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
    
    <script type="text/javascript">
        window.onload = function(){
            html2canvas(document.body, 
            {
                useCORS: true,
                onrendered: function(canvas) 
                {
                    document.body.appendChild(canvas);
                    document.getElementById("mapDiv").style.display = 'none';
                }
            });
        }
        //
        function callActionFunction()
        {
            sendEmailAF(document.getElementsByTagName('canvas')[0].toDataURL());
        }
       
   </script>
        <apex:iframe src="https://app.powerbi.com/view?r=<custom-URL>" height="540" width="100%" frameborder="0">
  </apex:iframe>

    </apex:form>
    
  
</apex:page>

Controller:

public class sendEmailWithImage 
{
    public string strImageBlob {get;set;}
    
    public void sendEmail()
    {
        Messaging.SingleEmailMessage mail =  new Messaging.SingleEmailMessage();
        List<String> lstString = new List<String>();
        mail.setToAddresses(new String[]{'testemail@gmail.com'});
        mail.setSubject('Email found');
        Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
        attach.setContentType('image/png');
        attach.setFileName('Test.png');
        attach.setInline(false);
        attach.Body = EncodingUtil.base64Decode(strImageBlob.replaceFirst('data:image/png;base64,',''));
        mail.setHtmlBody('Please find the attached Image');
        mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
    }
}

Now, when i click on the send email button, I am getting the email but with the blank/plain screen. Can anyone please suggest the changes in the above code so that i can email the proper screenshot on button click.

Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.