Please enable JavaScript to view this site.

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

Emailing selected records as separate PDF files

Scroll Prev Next More

 

This example shows how to email selected records as separate PDF files.

 

Client before:

First, we need to get information about the user-selected records, since the PDF files are created from these records. If no entries are selected, the code execution ends.

 

Then, we make a dialog appear on the web page. Using this dialog, the user can specify an email recipient's address, email subject and body.

 

We also set the parameters of the the PDF pageType to ‘View’.

 

var pdfParams = {},
  selectedRecords = pageObj.getSelectedRecords();
 
if( selectedRecords.length == 0 )
  return false;
 
params.recordCount = selectedRecords.length;
params.filenames = [];
 
var createOnePdf = function( idx ) {
  ajax.addPDF( 'pdf'+idx, pdfParams[idx], function() {
    delete pdfParams[idx];
    if( Object.keys( pdfParams ).length == 0 ) {
        showDialog();
     }
  });
}
 
var showDialog = function() {
 ctrl.dialog( {
  title: 'Email ' + params.recordCount + ' PDF files',
  fields: [
     {
        name: 'email',
        value: 'email@address.com'
     },
     {
        name: 'subject',
        value: 'Check out this data'
     },
     {
        name: 'body',
        value: 'This email is generated by Runner-created application',
        type: 'textarea'
     },
     ]
  });
}
 
selectedRecords.forEach( function( ajaxRow, idx ) {
  pdfParams[ idx ] = {
     pageType: 'view',
     keys: ajaxRow.getKeys()
  };
  params.filenames.push( '_viewpage_id'+ajaxRow.getKeys()+'.pdf')
  createOnePdf( idx );
 
});
 
return false;

Server:

In the Server event, we create temporary PDF files for each record. After that, we send the email. The result is returned to the Client after event:

$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.

 

// save generated files to disk and create an array of attachments
 
$attachments = array();
for( $i=0; $i< $params["recordCount"]; ++$i ) {
  $attachments[] = array(
    "path" => $button->saveTempFile( $params["pdf" . $i] ),
    "name" => $params["filenames"][$i]
  );
}
$mail = array();
$mail["to"] = $params["email"];
$mail["subject"] = $params["subject"];
$mail["body"] = $params["body"];
$mail["attachments"] = $attachments;
 
$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