This example illustrates how to render a report 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 Reports module
var Stimulsoft = require('stimulsoft-reports-js');
console.log("Stimulsoft Reports loaded");

For the correct rendering of reports 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 need to create a new StiReport object and load the previously prepared report template using the loadFile() method. As an argument, specify the path to the MRT template file:
// Creating new report
var report = new Stimulsoft.Report.StiReport();
console.log("New report created");

// Loading report template
report.loadFile("SimpleList.mrt");
console.log("Report template loaded");

To render a report, call the asynchronous renderAsync() method, which will perform all the necessary actions. Also, you can specify a callback function in the method arguments, which will be called after rendering:
// Renreding report
report.renderAsync(() => {
    console.log("Report rendered. Pages count: ", report.renderedPages.count);

    ...
});

To save the rendered report to the PDF format, you first need to export the report. To do this, you can use the special asynchronous exportDocumentAsync() 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. It is correct to call this method in the callback function after the report rendering is complete. You can save result to a file using standard methods for working with files:
// Renreding report
report.renderAsync(() => {
    console.log("Report rendered. Pages count: ", report.renderedPages.count);

    // 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 report in PDF into a file
        fs.writeFileSync('./SimpleList.pdf', buffer);
        console.log("Rendered report saved into PDF-file.");
    }, Stimulsoft.Report.StiExportFormat.Pdf);
});

In the screenshot below you can see the result of the sample code:

Exporting a Report to PDF

By using this website, you agree to the use of cookies for analytics and personalized content. Cookies store useful information on your computer to help us improve efficiency and usability. For more information, please read the privacy policy and cookie policy.