This example shows how to render a report from code using
Stimulsoft Reports for PHP. First, you need to add the Stimulsoft libraries required for the component to work:
<?php
require_once '../vendor/autoload.php';
use Stimulsoft\Report\StiReport;
?>
Next, create a report object and configure JavaScript-related options:
$report = new StiReport();
$report->javascript->relativePath = '../';
Next, define report 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:
$report->onAfterRender = 'onAfterRender';
After defining events, process the report request. If the request is successful, it will be handled immediately on the server side using
process():
$report->process();
Next, load a report template file. The
loadFile() method does not load the report on the server side; it only generates the necessary JavaScript code to load the report on the client side:
$report->loadFile('../reports/SimpleList.mrt');
Next, call the report build. This method does not render the report on the server side; it only generates JavaScript code that will render the report in the browser using
render():
$report->render();
Finally, render the necessary JavaScript code and visual HTML part of the component using
renderHtml(). You can define a JavaScript function that will be called after the report is built:
<script>
function onAfterRender(args) {
document.getElementById("message").innerText = "The report rendering is completed. Pages: " + args.report.renderedPages.count;
document.getElementById("reportJson").innerText = args.report.saveDocumentToJsonString();
}
</script>
Render the HTML page with the report engine and placeholders for messages and JSON output:
<?php
$report->javascript->renderHtml();
$report->renderHtml();
?>
<div id="message"></div>
<pre id="reportJson"></pre>
In the screenshot below you can see the result of the sample code: