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 protected]'
     },
     {
        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
set attachments = CreateDictionary()
for i=0 to i< params("recordCount")
  set attach = CreateDictionary()
  attach("path") = button.saveTempFile( params("pdf"  & i))
  attach("name") = params("filenames")(i)
  set attachments(asp_count(attachments)) = attach
next
 
set mail = CreateDictionary()
mail("to") = params("email")
mail("subject") = "ASPRunner PDF and Email demo"
mail("body") = "Check the results"
set mail("attachments") = array( attachment )
ret = runner_mail( mail )
if ret("mailed") then
  result("success") = true
else
   result("message") = ret("message")
   result("success") = false
end if

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