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);
});

На скриншоте ниже Вы можете увидеть результат выполнения данного кода:

Exporting a Report to PDF

Используя этот сайт, вы соглашаетесь на использование файлов Cookie для аналитики и персонализированного контента. Файлы Cookie хранят полезную информацию на вашем компьютере, чтобы помочь нам повысить эффективность и удобство использования. Для получения дополнительной информации, пожалуйста, прочтите Конфиденциальность и Использование Cookie.