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 protected]'
     },
     {
        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.

 

$path = $button->saveTempFile( $params["pdf"] );
 
$mail = array();
$mail["to"] = $params["email"];
$mail["subject"] = "PHPRunner PDF and Email demo";
$mail["body"] = "Check the results";
$attachment = array(
  "path" => $path,
"name" => $params["filename"]
);
$mail["attachments"] = array( $attachment );
 
$ret = runner_mail( $mail );
 
if( $ret["mailed"] )
{
  $result["success"] = true;
}
else
{
  $result["message"] = $ret["message"];
$result["success"] = false;
}

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

 

Created with Help+Manual 7 and styled with Premium Pack Version 3 © by EC Software