Template Export Task
Products: FastReport Cloud, FastReport Corporate Server, FastReport Publisher
This article covers working with the template export task (ExportTemplateTask). General instructions on how to work with tasks are described in the General Information section.
It should be noted that templates can be exported not only using the task system, but also directly through a request to the path /api/rp/v1/Templates/File/{id}/Export. The advantage of using the task system is that it allows not only saving the document to a folder, but also transmitting it to external systems (via Email, FTP, S3, Webhook).
Getting Started
You will need the following tools and capabilities:
Knowledge of using API key in Service solutions.
This article will skip additional information on authentication and authorization.
curl tool.
Any other REST client will work, but examples will be built for curl.
Active subscription to one of the following products: FastReport Cloud, FastReport Corporate Server, or FastReport Publisher.
Internet access.
Configured and accessible S3 server.
Note! The points above describe recommended tools.
Creating a Task
Let's look at creating a template export task to PDF:
// Creating a task
curl --location 'https://{host_name}/api/tasks/v1/Tasks' \
--header 'accept: application/json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic {base64(apikey:api-key-value)}' \
--data '{
"$t": "CreateExportTemplateTaskVM",
"name": "PDF export task",
"subscriptionId": "{workspace id}",
"inputFile": {
"entityId": "{template id}",
"type": "Template"
},
"outputFile" : {
"fileName": "Report"
},
"format": "Pdf",
"reportParameters": {
"param1": "val1",
"param2": "val2"
},
"exportParameters": {
"PrintOptimized": "true",
"JpegQuality": "90"
}
}'
Since no folder is specified for saving in outputFile, the document will be saved to the root folder when the task is run.
Let's set a different folder for saving:
"outputFile" : {
"folderId": "61cb2226d24478b172103075",
"fileName": "Report"
}
Now when the task is run, the PDF file will be saved to the specified folder.
// Running task by identifier
curl -X 'POST' \
'https://{host_name}/api/tasks/v1/Tasks/{task identifier}/run' \
-H 'accept: */*' \
-d ''
In the EntityId and SubscriptionId fields, you should enter real object identifiers. Otherwise, the task will be terminated with an error.
Sending Document After Export
To send a document after export, you can use Email, FTP, S3 or Webhook sending tasks. Let's look at an example of creating a PDF export task followed by sending the finished document via Email.
// Creating a task
curl --location 'https://{host_name}/api/tasks/v1/Tasks' \
--header 'accept: application/json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic {base64(apikey:api-key-value)}' \
--data '{
"$t": "CreateExportTemplateTaskVM",
"name": "PDF export task",
"subscriptionId": "{workspace id}",
"inputFile": {
"entityId": "{template id}",
"type": "Template"
},
"outputFile" : {
"fileName": "Report"
},
"format": "Pdf",
"reportParameters": {
"param1": "val1",
"param2": "val2"
},
"exportParameters": {
"PrintOptimized": "true",
"JpegQuality": "90"
},
"transports": [
{
"$t": "CreateEmailTaskVM",
"name": "Email sending task",
"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
}
]
}'
Editing Task
After a task is created, you can change some of its parameters.
// Editing task
curl --location --request PUT 'https://{host_name}/api/tasks/v1/Tasks/{task id}' \
--header 'accept: application/json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic {base64(apikey:api-key-value)}' \
--data '{
"$t": "UpdateExportTemplateTaskVM",
"reportParameters": {
"param1": "val1",
"param2": "val2"
},
"transportIds" : ["{transport task id (email sending, FTP upload, etc.)}"]
}'
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 creating them, read the section Creating Transport Tasks.
Executing Task from Request Body
curl --location 'https://{host_name}/api/tasks/v1/tasks/run' \
--header 'accept: application/json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic {base64(apikey:api-key-value)}' \
--data '{
"$t": "RunExportTemplateTaskVM",
"subscriptionId": "{workspace id}",
"inputFile": {
"$t": "RunInputFileVM",
"entityId": "{template id}",
"type" : "Template",
"fileName": "Template.frx"
},
"format": "Pdf",
"transports": [
{
"$t": "RunS3UploadTaskVM",
"accessKey": "{public_access_key}",
"secretKey": "{secret_access_key}",
"bucketName": "storage",
"destinationFolder": "test",
"useAws": true,
"enableSsl": true,
"region": "us-east-1"
}
]
}'
Thus, as a result of executing this request, a PDF document will be created and sent to S3 storage.
Note! In this case, creating the PDF document and sending it to S3 storage will be performed directly by this request and the task will not be saved in the database.