This example illustrates how to load a dashboard template, render the data and save the result to the PDF file. First of all, you should add Stimulsoft modules to the project. To do this, just use one universal package, all other dependencies will be loaded automatically:
// Stimulsoft dashboards module
var Stimulsoft = require('stimulsoft-dashboards-js');
console.log("Stimulsoft dashboards loaded");
For the correct rendering of dashboards and accurate calculation of the sizes of all elements, you should load the fonts you are going to use. In this example, the Roboto-Black font is used, load it using a special
addOpentypeFontFile()
static method. The font will be loaded and added to the internal font collection:
// Loading fonts
Stimulsoft.Base.StiFontCollection.addOpentypeFontFile("Roboto-Black.ttf");
console.log("Font loaded");
Now you should create a new
StiReport
object with dashboard page. You can use the special
createNewDashboard()
constructor. After that, load the dashboard template using the
loadFile()
method. As an argument, specify the path to the MRT template file:
// Creating new dashboard
var report = Stimulsoft.Report.StiReport.createNewDashboard();
console.log("New dashboard created");
// Loading dashboard template
report.loadFile("Dashboard.mrt");
console.log("Dashboard template loaded");
To save the dashboard to the PDF format, you first need to export it. To do this, you can use the special
exportDocumentAsync()
asynchronous method. As the first argument, you should specify the callback function, which will receive the export result after its completion. You may specify the required format as the second argument of the function, in this case, specify
StiExportFormat.Pdf
format. You can save result to a file using standard methods for working with files:
// Export to PDF
report.exportDocumentAsync((pdfData) => {
// Converting Array into buffer
var buffer = Buffer.from(pdfData);
// File System module
var fs = require('fs');
// Saving string with rendered dashboard in PDF into a file
fs.writeFileSync('./Dashboard.pdf', buffer);
console.log("Dashboard saved into PDF-file.");
}, Stimulsoft.Report.StiExportFormat.Pdf);
In the screenshot below you can see the result of the sample code: