This example shows how to export a dashboard from code in Stimulsoft PHP. First, include the necessary libraries:
<?php
require_once '../vendor/autoload.php';
use Stimulsoft\Enums\StiHtmlMode;
use Stimulsoft\Export\Enums\StiExportFormat;
use Stimulsoft\Report\StiReport;
?>
Create a dashboard object and set JavaScript options:
$report = new StiReport();
$report->javascript->relativePath = '../';
Process the request using
process():
$report->process();
Load the dashboard template using
loadFile():
$report->loadFile('../reports/Christmas.mrt');
Render the necessary JavaScript code for the dashboard engine:
$report->javascript->renderHtml();
Define JavaScript functions for exporting the dashboard in different formats. Each function calls the
exportDocument() method and then renders only the scripts with
getHtml(StiHtmlMode::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, create buttons in the HTML body that call the corresponding export functions:
<button onclick="exportToPdf();">Export Dashboard to PDF</button>
<br><br>
<button onclick="exportToExcel();">Export Dashboard to Excel</button>
<br><br>
<button onclick="exportToHtml();">Export Dashboard to HTML</button>
In the screenshot below you can see the result of the sample code: