Table of Contents

Export Report Task

Products: FastReport Cloud, FastReport Corporate Server, FastReport Publisher

In this article, we will consider working with the report export task (ExportReportTask). General instructions on how to work with tasks are described in the General Information section.

It should be noted that you can export a template not only using the task system, but also directly through a request to the reports 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 export task to PDF:

// Creating a task
var currentTask = await tasksClient.CreateTaskAsync(new CreateExportReportTaskVM
{
    SubscriptionId = subscriptionId,
    Name = "PDF Email Sending Task",
    InputFile = new InputFileVM
    {
        EntityId = template.Id
    },
    Format = ExportFormat.Pdf,
    ExportParameters = new Dictionary<string, string>
    {
        { "PrintOptimized", "true" },
        { "JpegQuality", "90" }
    },
    OutputFile = new OutputFileVM
    {
        FileName = "Report"
    }
});

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

Sending Document After Export

To send the document after export, you can use the Email, FTP, S3, or Webhook sending task.

Let's consider an example of creating a PDF export task with subsequent sending of the finished document via Email.

// Creating a task
var currentTask = await tasksClient.CreateTaskAsync(new CreateExportReportTaskVM
{
    SubscriptionId = subscriptionId,
    Name = "PDF Email Sending Task",
    InputFile = new InputFileVM
    {
        EntityId = template.Id
    },
    Format = ExportFormat.Pdf,
    ExportParameters = new Dictionary<string, string>
    {
        { "PrintOptimized", "true" },
        { "JpegQuality", "90" }
    },
    Transports = new List<CreateTransportTaskBaseVM>
    {
        new CreateEmailTaskVM
        {
            Name = "Email Distribution Task",
            From = "admin@company.com",
            To = new List<string> { "user@company.com","seller@company.com" },
            Subject = "Important Report",
            IsBodyHtml = false,
            Username = "admin@company.com",
            Password = "parol123",
            Server = "smtp.company.com",
            Body = "An important report is attached to this email.",
            Port= 587,
            EnableSsl = true
        }
    }
});

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 UpdateExportReportTaskVM
{
    Format = ExportFormat.Docx,
    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 RunExportReportTaskVM
{
    SubscriptionId = subscriptionId,
    InputFile = new RunInputFileVM
    {
        EntityId = template.Id,
        FileName = "Document"
        //Content = new byte[] { }
    },
    Format = ExportFormat.Docx,
    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 PDF document will be created and sent via email.

Note! In this case, the creation of the PDF document and its sending via email will be performed directly by this request and the task will not be saved in the database.