We have recently released an article on using the Quartz.NET library to automate reporting tasks.
In addition, to add and use a scheduler, you should install the Node Schedule package.
What is a Node Schedule?
Node Schedule stands as a flexible job scheduler for Node.js, operating on cron syntax. It executes tasks, allowing optional recurrence rules based on time, rather than intervals. Node Schedule can be used for various purposes like sending report emails, conducting routine database maintenance, or executing periodic data analysis tasks. Read more about its functionalities here.Starting a project
Our initial step involves setting up a project on the JavaScript platform. For this purpose, we will use a JS reporting tool (a diverse array of reporting components developed entirely in JavaScript). This tool can be integrated into custom applications through simple installation of the corresponding npm package.Installing packages
To work with reports we will need the stimulsoft-reports-js package, and for dashboards - stimulsoft-dashboards-js.In addition, to add and use a scheduler, you should install the Node Schedule package.
Defining tasks and setting up a schedule
Let's get right to the main point of our article. We have chosen exporting a report to PDF as our example task. Next, we will configure the export to execute daily at 10:00.// Node Schedule module
var schedule = require("node-schedule");
console.log("Node Schedule loaded");
// Stimulsoft Reports module
var Stimulsoft = require("stimulsoft-reports-js");
console.log("Stimulsoft Reports loaded");
// 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");
// Run the task every day at 10:00
var rule = new schedule.RecurrenceRule();
rule.hour = 10;
rule.minute = 0;
console.log("Rule for the task created");
// Scheduling the task
schedule.scheduleJob(rule, function () {
// 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);
});
});
We demonstrated the automation process with an example. From now on, the report will be converted to a PDF file and saved to disk daily at 10:00.