This example shows how to save an exported report on the server side using
Stimulsoft Viewer for PHP. First, include the Stimulsoft libraries:
<?php
require_once '../vendor/autoload.php';
use Stimulsoft\Events\StiExportEventArgs;
use Stimulsoft\Report\StiReport;
use Stimulsoft\StiResult;
use Stimulsoft\Viewer\StiViewer;
?>
Next, create a
Viewer object and configure JavaScript-related options:
$viewer = new StiViewer();
$viewer->javascript->relativePath = '../';
$viewer->javascript->appendHead('<link rel="shortcut icon" href="/../favicon.ico" type="image/x-icon">');
Define viewer events before processing. You can assign a PHP function, the name of a JavaScript function, or a JavaScript function as a string. Multiple event handlers can be added using the
append() method. In this example, the
onEndExportReport event is used to save the exported report on the server:
$viewer->onEndExportReport = function (StiExportEventArgs $args) {
$reportName = $args->fileName;
if (substr($reportName, -strlen($args->fileExtension) - 1) !== '.' . $args->fileExtension)
$reportName .= '.' . $args->fileExtension;
$reportPath = "../reports/$reportName";
file_put_contents($reportPath, base64_decode($args->data));
return StiResult::getSuccess("The exported report has been successfully saved to '$reportPath' file.");
};
Process the request on the server side using
process():
$viewer->handler->process();
Create a report object and load a report template. The
loadFile() method does not load the report on the server side; it only generates JavaScript code for the client side:
$report = new StiReport();
$report->loadFile('../reports/SimpleList.mrt');
Assign the report to the viewer:
$viewer->report = $report;
Finally, render the viewer as a complete HTML page using
printHtml():
$viewer->printHtml();
In the screenshot below you can see the result of the sample code: