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.

 

dynamic dot = null, filename = null, var_params = XVar.Array();
        filename = XVar.Clone(MVCFunctions.str_replace((XVar)(new XVar(0, "/", 1, "\\")), new XVar(""), (XVar)(var_params["filename"])));
        dot = XVar.Clone(MVCFunctions.strrpos((XVar)(filename), new XVar(".")));
        if(!XVar.Equals(XVar.Pack(MVCFunctions.strtolower((XVar)(MVCFunctions.substr((XVar)(filename), (XVar)(dot))))), XVar.Pack(".pdf")))
        {
          filename = MVCFunctions.Concat(filename, ".pdf");
        }
        MVCFunctions.runner_save_file((XVar)(filename), (XVar)(var_params["pdf"]));
        return null;

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