As we know, our world is constantly evolving, and new technologies emerge almost every day. Nowadays, it is no longer necessary to set up the entire infrastructure in one's office, hire personnel to monitor equipment, and deal with issues that arise with this equipment and other difficulties.
Nowadays, more and more services offer business cloud solutions, for example, FastReport Cloud. Our service saves the development team from unnecessary work; you no longer need to think about how to deploy the project, where it would be best and most profitable to rent or buy servers, and what technologies to use for deployment. We have already settled all this, and all you need to do is take advantage of it.
In this article, we will look at how to create reports in FastReport Cloud using SDK and export them to any convenient format.
First, let's create a project and add the FastReport.Cloud.SDK.Web nuget package to it. Thanks to this package, we will conveniently communicate with FastReport Cloud without an API.
We will also need a report template. This example will use Box.frx from our Demo:
After creating the project and adding all the necessary dependencies to it, you can move on to analyzing the example.
At the very beginning, you need to create an API key in the FastReport Cloud workspace; to do this, follow the link https://fastreport.cloud.
Click on the tab with API keys and create a new one. If the key already exists, then you can copy it by right-clicking on it and selecting an action from the drop-down list.
After receiving the API key, we return to our application. We write the key into a separate variable as in the example below:
private const string ApiKey = "your API key";
Next, we need to create the main objects that we will use in the program:
var httpClient = new HttpClient(); httpClient.BaseAddress = new Uri("https://fastreport.cloud"); httpClient.DefaultRequestHeaders.Authorization = new FastReportCloudApiKeyHeader(ApiKey); var subscriptions = new SubscriptionsClient(httpClient); var rpClientTemplates = new TemplatesClient(httpClient); var rpClientExports = new ExportsClient(httpClient); var downloadClient = new DownloadClient(httpClient); var subscription = (await subscriptions.GetSubscriptionsAsync(0, 10)).Subscriptions.First(); var templateFolder = subscription.TemplatesFolder.FolderId; var exportFolder = subscription.ExportsFolder.FolderId;
After this, we move on to the stage of creating a report for the cloud. You can do it like this:
TemplateCreateVM templateCreateVM = new TemplateCreateVM() { Name = "box.frx", Content = Convert.FromBase64String(TestData.BoxReport) //we send the frx file in byte format };
In the example above, we already have a report in byte format. If you have a file in frx format, then you can use this example:
TemplateCreateVM templateCreateVM = new TemplateCreateVM() { Name = "box.frx", Content = File.ReadAllBytes("path to report") //we send the frx file in byte format to the path };
We upload the TemplateCreateVM object along with the report into our FastReport.Cloud workspace:
TemplateVM uploadedFile = await rpClientTemplates.UploadFileAsync(templateFolder, templateCreateVM);
Now we export the report to the format we need. First, you need to decide on the format and name of the future file.
ExportTemplateVM export = new ExportTemplateVM() { FileName = "box", Format = ExportFormat.Pdf //format to be exported };
We export to PDF format:
ExportVM exportedFile = await rpClientTemplates.ExportAsync(uploadedFile.Id, export) as ExportVM; string fileId = exportedFile.Id; int attempts = 3; exportedFile = rpClientExports.GetFile(fileId); while (exportedFile.Status != FileStatus.Success && attempts >= 0) { await Task.Delay(1000); exportedFile = rpClientExports.GetFile(fileId); attempts--; }
We finished the main part of the work with the report. We received a pdf file from our report:
If you want to download the file manually, then go to your workspace and download it as in the following example:
You can also download the file with the SDK using this example:
using (var file = await downloadClient.GetExportAsync(fileId)) { using (var pdf = File.Open("report.pdf", FileMode.Create)) { file.Stream.CopyTo(pdf); } }
Now you know how to create, export, and download files in FastReport Cloud using the SDK. You can find the example from this article at this link: https://github.com/FastReports/FastReport-Cloud.