Please enable JavaScript to view this site.

Navigation: Advanced topics > Programming topics > PDF API > Examples

Emailing the current page as PDF

Scroll Prev Next More

 

This example shows how to email the current page as PDF.

Client before:

First, we create a PDF file. Then, we make a dialog appear on the web page. With this dialog, the user can specify the filename, and the email address to which the PDF file should be sent.

 

ajax.addPDF( 'pdf', {}, function() {
  return ctrl.dialog( {
     title: 'Email this page',
     fields: [{
        name: 'email',
        value: 'email@address.com'
     },
     {
        name: 'filename',
        value: 'results.pdf'
     }]
  });
});
return false;

Server:

In the Server event, we create a temporary PDF file.

 

Next, we create a mail array. The email parameters are placed into this array.

 

You can modify the email subject - mail["subject"], and the email body (message) - mail["body"].

 

After the email is sent, the result is returned to the "Client after":

 

result "success" = true - everything is fine, the message is sent.

result "success" = false - the message is not sent. In this case, an error message defined in the result variable appears.

 

dynamic attachment = null, a_button = null, mail = XVar.Array(), path = null, a_result = XVar.Array(), ret = XVar.Array(), var_params = XVar.Array();
        path = XVar.Clone(button.saveTempFile((XVar)(var_params["pdf"])));
        mail = XVar.Clone(XVar.Array());
        mail.InitAndSetArrayItem(var_params["email"], "to");
        mail.InitAndSetArrayItem("Runner PDF and Email demo", "subject");
        mail.InitAndSetArrayItem("Check the results", "body");
        attachment = XVar.Clone(new XVar("path", path, "name", var_params["filename"]));
        mail.InitAndSetArrayItem(new XVar(0, attachment), "attachments");
        ret = XVar.Clone(MVCFunctions.runner_mail((XVar)(mail)));
        if(XVar.Pack(ret["mailed"]))
        {
           result.InitAndSetArrayItem(true, "success");
        }
        else
        {
           result.InitAndSetArrayItem(ret["message"], "message");
           result.InitAndSetArrayItem(false, "success");
        }
        return null;

Client after:

The user gets a notification with the result.

 

if( result.success ) {
  ctrl.setMessage("sent ok");
} else {
  ctrl.setMessage("error sending " + result.message );
}

See also:

AJAX helper object: ajax.addPDF

PDF Parameters

About Dialog API

Tri-part events

About PDF API