Exporting report to PDF
This article describes how to export a report using the FastReport Corporate Server report processor.
Getting started
You will need the following tools and features:
Knowledge on how to use API key in FastReport Corporate Server.
This article does not include the information about authentication and authorization.
A C# code editor or a text editor like Visual Studio Code.
A report.
You can learn how to build a report from the article Prepeare a report.
Active subscription for FastReport Corporate Server.
Internet access.
Please, note: This tutorial assumes that you already know how to develop your application with the C# programming language.
Please, note: The descriptions above are provided for the recommended tools.
Remark
Please note that you can export the report directly from the template, without saving it in the process. 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.
Instructions
To export a report to PDF, you will need a report identifier. To get it, use the <xref:FastReport.Cloud.ITemplatesClient.GetFilesListAsync(System.String,System.Nullable{System.Int32},System.Nullable{System.Int32})> method.
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 root directory of the user's first subscription, then it requests 10 reports and returns the first.
To export a report, you need a directory where the export should be saved.
To get the root directory for exports, use the <xref:FastReport.Cloud.IExportFoldersClient.GetRootFolder(System.String)> method.
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, the subscription identifier can be omitted, in which case the root directory for the user's first subscription will be returned.
To export the report, use the <xref:FastReport.Cloud.IReportsClient.ExportAsync(System.String,FastReport.Cloud.ExportReportTaskVM)> method.
public async Task<string> ExportReport(HttpClient httpClient, string folderId, string reportId, string fileName) { IReportsClient reportsClient = new ReportsClient(httpClient); ExportReportTaskVM task = new ExportReportTaskVM() { FileName = Path.ChangeExtension(fileName, ".pdf"), FolderId = folderId, Format = ExportReportTaskVMFormat.Pdf }; ExportVM result = await reportsClient.ExportAsync(reportId, task); return result.Id; }
In this example, the function creates a task to export the report.
Please, note: Although the report has not yet been exported, at this stage the export already has an identifier, so after a while the builder will deal with this task and the report will be exported.
To get information about a file, use the <xref:FastReport.Cloud.IExportsClient.GetFileAsync(System.String)> method.
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 the export by its identifier and returns the status. You need to wait for the status <xref:FastReport.Cloud.ExportVMStatus.Success>. Check status every few seconds.
To download the report, use the <xref:FastReport.Cloud.IDownloadClient.GetExportAsync(System.String)> method.
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.