Table of Contents

Export report to PDF

Products: FastReport Cloud, FastReport Corporate Server, FastReport Publisher

This article covers the process of exporting a report using the Service solutions report processor.

Getting started

You will need the following tools and facilities:

  1. Know how to use API key in Service solutions.

    This article will skip over additional information on authentication and authorization.

  2. .NET SDK.

  3. C# code editor or text editor, e.g., Visual Studio Code.

  4. Report.

    You can learn how to create a report in the Create report article.

  5. Active subscription to one of the following products: FastReport Cloud, FastReport Corporate Server, or FastReport Publisher.

  6. Internet access.

Important! These guides assume that you already know how to develop your application in the C# programming language.

Note. The items above describe recommended tools.

Annotation

You can export a report directly from the template, without intermediate saving of the report. To do this run the same commands for the report template, replacing Report in methods with Template use the template ID, not the report ID.

Instruction

  1. You will need a report ID to export to PDF. To retrieve it, use the following method GetFilesListAsync(string, int?, int?, string, FileSorting?, bool?, bool?, 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 root directory of the user's default workspace, then requests 10 reports and returns the first one.

  2. To export the report, you will need a directory to save the export to.

    Retrieve the root directory of exports by using the method GetRootFolderAsync(string, 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, the workspace ID can be omitted. In this case, the root directory for the user's default workspace will be returned.

  3. To export the report, use the following method ExportAsync(string, ExportReportVM, 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 — name of the resulting file. If you do not specify the extension, or specify it incorrectly, the server will replace it by itself.
    • FolderId — ID of the directory where the export will be placed. If left blank, the export will be placed in the root folder for exports 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 stored in the user documentation in the export parameters section.

    In this example, the function creates a task to export a report.

    Important! The report has not been exported yet, but an ID has already been assigned to the export. After some time, the builder's turn will come to this task and the report will be exported.

If you do not specify FolderId, the export will be saved to the root folder.

  1. For information about the file, use the following method GetFileAsync(string, 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 ID and returns the status. You must wait for the Success status. Check it every few seconds.

  2. Check the status in the 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);
    
  3. Use the following method to download the report GetExportAsync(string, bool?, 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 to memory.

What next?