Prepare Report Task
Products: FastReport Cloud, FastReport Corporate Server, FastReport Publisher
In this article, we will consider working with the report preparation task (PrepareTemplateTask). General instructions on how to work with tasks are described in the General Information section.
It should be noted that you can prepare a report not only using the task system, but also directly through a request to the templates controller. The advantage of using the task system is that it allows not only saving the document to a folder, but also transferring it to external systems (via Email, FTP, S3, Webhook).
Creating a Task
Let's consider creating a report preparation task:
// Creating a task
var currentTask = await tasksClient.CreateTaskAsync(new CreatePrepareTemplateTaskVM
{
SubscriptionId = subscriptionId,
Name = "Report Preparation Task",
InputFile = new InputFileVM
{
EntityId = template.Id
},
OutputFile = new OutputFileVM
{
FileName = "Report.fpx"
}
});
Real object identifiers should be entered in the EntityId and SubscriptionId fields. Otherwise, the task will be aborted with an error.
Since no folder for saving is specified in OutputFile, when the task runs, the document will be saved to the root folder.
Let's specify a different folder for saving:
"OutputFile" : {
"FolderId": "61cb2226d24478b172103075",
"FileName": "Report"
}
Running Task by Identifier
Now when the task runs, the PDF file will be saved to the specified folder.
await tasksClient.RunTaskByIdAsync(currentTask.Id);
Exporting Report After Preparation
To export the report after preparation, you can use the export task.
Let's consider an example of creating a report preparation task with subsequent export to PDF.
// Creating a task
var currentTask = await tasksClient.CreateTaskAsync(new CreatePrepareTemplateTaskVM
{
SubscriptionId = subscriptionId,
Name = "Report Preparation Task",
InputFile = new InputFileVM
{
EntityId = template.Id
},
OutputFile = new OutputFileVM
{
FileName = "Report.fpx"
},
Exports = new List<CreateExportReportTaskVM>()
{
new CreateExportReportTaskVM
{
SubscriptionId = subscriptionId,
Name = "PDF Export Task",
Format = ExportFormat.Pdf,
OutputFile = new OutputFileVM
{
FileName = "Document.pdf"
}
}
}
});
Editing a Task
After a task is created, you can change some of its parameters.
// Task editing
var editedTask = await tasksClient.UpdateTaskAsync(currentTask.Id, new UpdatePrepareTemplateTaskVM
{
ReportParameters = new Dictionary<string, string>
{
{ "param1", "val1" },
{ "param2", "val2" }
},
TransportIds = new List<string> { "{transportId}" }
});
Note that when updating a task, you need to specify TransportIds - a list of transport task identifiers (Email, FTP, Webhook, S3), not the tasks themselves. The task itself can be created in advance either separately or when creating a transformer task. For more details on their creation, read the section Creating Transport Tasks.
Executing Task from Request Body
var runTask = await tasksClient.RunTaskAsync(new RunPrepareTemplateTaskVM
{
SubscriptionId = subscriptionId,
InputFile = new RunInputFileVM
{
EntityId = template.Id,
FileName = "Document"
//Content = new byte[] { }
},
Transports = new List<RunTransportTaskBaseVM>
{
new RunEmailTaskVM
{
From = "admin@company.com",
To = ["user@company.com", "seller@company.com"],
Subject = "Important Report",
Body = "An important report is attached to this email.",
EnableSsl = true,
Username = "admin@company.com",
Password = "parol123",
Server = "smtp.company.com",
Port = 587
}
}
});
Thus, as a result of executing this request, a report will be prepared and sent via email.
Note! In this case, the creation of the report and its sending via email will be performed directly by this request and the task will not be saved in the database.