Exporting a report to PDF
This article walks you through the process of exporting a report with the FastReport Cloud report processor.
Getting Started
You will need the following tools and features:
Knowledge of using API key in FastReport Cloud.
This article will skip additional information on authentication and authorization.
A C# code editor or a text editor such as Visual Studio Code.
Report.
You can find how to build a report in the article Building a report.
Active FastReport Cloud subscription.
Access to the Internet.
Note! This guide assumes that you already know how to develop your application in the C# programming language.
Note! The paragraphs above describe the recommended tools.
Comment
Please note that the report can be exported directly from the template, without intermediate saving of the report. To do this, run the same commands for the report template, replacing Report
in the methods with Template
, also use the template identifier, not that of the report.
Instruction
You will need a report identifier to export to PDF, to get it use the method GetFilesListAsync(String, Nullable<Int32>, Nullable<Int32>, String, Nullable<FileSorting>, Nullable<Boolean>, Nullable<Boolean>, System.Threading.CancellationToken)
public async Task<string> GetReportId(HttpClient httpClient) { IReportFoldersClient reportFoldersClient = new ReportFoldersClient(httpClient); IReportsClient reportsClient = new ReportsClient(httpClient); FileVM rootFolder = await reportFoldersClient.GetRootFolderAsync(null); IEnumerable<ReportVM> reports = await reportsClient.GetFilesListAsync(rootFolder.Id, 0, 10); ReportVM report = reports.First(); return report.Id; }
In this example, the function requests the user's default workspace root directory, then requests 10 reports, and returns the first one.
To export the report, a directory will be needed to save the export to.
Get the exports root directory by using the method GetRootFolderAsync(String, System.Threading.CancellationToken).
public async Task<string> GetExportsRoot(HttpClient httpClient, string subscriptionId = null) { IExportFoldersClient exportFoldersClient = new ExportFoldersClient(httpClient); FileVM result = await exportFoldersClient.GetRootFolderAsync(subscriptionId); return result.Id; }
In this example, the function requests the root directory, and the workspace identifier can be omitted, in which case the root directory for the user's default workspace will be returned.
To export a report, use the method ExportAsync(String, ExportReportVM, System.Threading.CancellationToken).
public async Task<string> ExportReport(HttpClient httpClient, string folderId, string reportId, string fileName) { IReportsClient reportsClient = new ReportsClient(httpClient); ExportReportVM task = new ExportReportVM() { FileName = Path.ChangeExtension(fileName, ".pdf"), FolderId = folderId, Format = ExportReportTaskVMFormat.Pdf, ExportParameters = new Dictionary<string, string> { { "additionalProp1", ""}, { "additionalProp2", ""}, { "additionalProp3", ""} } }; ExportVM result = await reportsClient.ExportAsync(reportId, task); return result.Id; }
FileName
— the name of the resulting file. If you do not specify the extension or specify it incorrectly, the server will replace it automatically.FolderId
— identifier of the directory where the export will be placed. If left blank, the export will be placed in the exports root folder in the workspace.Format
— export format.ExportParameters
— export parameters. They are set similarly to the export parameters from the FastReport .NET library. A more detailed description is available in the User Manual in the section export parameters.
In this example, the function creates a report export task.
Note! Although the report has not yet been exported, the export already has an assigned identifier at this stage. In a while, the builder queue will reach this task, and the report will be exported.
If FolderId is not specified, then the export will be saved to the root folder.
To get information about the file, use the method GetFileAsync(String, System.Threading.CancellationToken).
public async Task<ExportVMStatus> CheckStatus(HttpClient httpClient, string exportId) { IExportsClient exportsClient = new ExportsClient(httpClient); ExportVM result = await exportsClient.GetFileAsync(exportId); return result.Status.GetValueOrDefault(); }
In this example, the function requests an export by its identifier and returns the status. You need to wait for the Success status, check the status every few seconds.
We check the status in a loop and download the export.
int tries = 10; FileStatus status; do { status = await CheckStatus(httpClient, reportId); tries--; } while (status != FileStatus.Success && tries > 0); var report = await DownloadExport(httpClient, reportId);
To download a report, use the method GetExportAsync(String, Nullable<Boolean>, System.Threading.CancellationToken).
public async Task<byte[]> DownloadExport(HttpClient httpClient, string exportId) { IDownloadClient downloadClient = new DownloadClient(httpClient); FileResponse file = await downloadClient.GetExportAsync(exportId); using (MemoryStream ms = new MemoryStream()) { file.Stream.CopyTo(ms); return ms.ToArray(); } }
In this example, the function requests a file and copies it into memory.