This example shows how to export a report from code using
Stimulsoft Reports for PHP. First, you need to include the Stimulsoft libraries:
<?php
require_once '../vendor/autoload.php';
use Stimulsoft\Enums\StiHtmlMode;
use Stimulsoft\Export\Enums\StiExportFormat;
use Stimulsoft\Report\StiReport;
?>
Next, create a report object and configure JavaScript-related options:
$report = new StiReport();
$report->javascript->relativePath = '../';
Process the report request on the server side using
process():
$report->process();
Load a report template file. The
loadFile() method does not load the report on the server side; it only generates the necessary JavaScript code for the client side:
$report->loadFile('../reports/SimpleList.mrt');
Call the report build using
render(). This generates JavaScript code that will render the report in the browser:
$report->render();
Render the necessary JavaScript code of the report engine using
renderHtml():
<?php
$report->javascript->renderHtml();
?>
Define JavaScript functions to export the report to different formats. Use the
exportDocument() method to generate JavaScript code for exporting, and
getHtml() with
StiHtmlMode::Scripts to render the scripts:
<script type="text/javascript">
function exportToPdf() {
<?php
$report->exportDocument(StiExportFormat::Pdf);
echo $report->getHtml(StiHtmlMode::Scripts);
?>
}
function exportToExcel() {
<?php
$report->exportDocument(StiExportFormat::Excel);
echo $report->getHtml(StiHtmlMode::Scripts);
?>
}
function exportToHtml() {
<?php
$report->exportDocument(StiExportFormat::Html);
echo $report->getHtml(StiHtmlMode::Scripts);
?>
}
</script>
Finally, render the HTML page with buttons to trigger export to PDF, Excel, and HTML:
<h2>Exporting a Report from Code</h2>
<hr>
<button onclick="exportToPdf();">Export Report to PDF</button>
<br><br>
<button onclick="exportToExcel();">Export Report to Excel</button>
<br><br>
<button onclick="exportToHtml();">Export Report to HTML</button>
In the screenshot below you can see the result of the sample code: