Please enable JavaScript to view this site.

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

How to create a PDF file and save it to the output folder

Scroll Prev Next More

 

This example shows how to create a PDF file and save it into the ouput folder.

Client before:

First, we create a PDF file. Then, we make a dialog appear on the web page where a user can specify the filename.

 

ajax.addPDF( 'pdf', {}, function() {
 
  return ctrl.dialog( {
     title: 'Save results as PDF',
     fields: [{
        name: 'filename',
        value: 'file.pdf'
     }]
  });
});
return false;

Server:

After the user entered the filename, we transfer it to the Server event.

 

The server code checks the entered filename so that the file is saved in the correct format. In essence, the file is saved with a correct extension, whether the .pdf extension was specified by the user or not.

 

After the name check, the file is saved to the output folder.

 

$filename = str_replace( array( '/', '\\'), '', $params["filename"]);
 
$dot = strrpos( $filename, "." );
if( strtolower(substr( $filename, $dot )) !== '.pdf' ) {
  $filename .= '.pdf';
}
file_put_contents( $filename, $params["pdf"] );

Client after:

After the file is saved to the disk, it is opened in a new browser window.

 

window.open( params['filename'] );

 

Note: the Client after is optional. Add it if you want to open the downloaded file in a new window.

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