How to save report in HTML into ZIP

2016-04-13

When I was developing another PHP application, it was necessary in the financial statements. Previously I'd had great experience with FastReport.Net report generator. So I decided to use it for this purpose too. Unfortunately, the option with the Web-based reporting was irrelevant since we did not use ASP .Net in this project. The idea was born to use a REST web service that will build a report and give it to a php application. I will explain development of the service later. And now let's focus on the process of report building and preparation to be sent via REST. For simplicity, I'll demonstrate it on an example of a console application.

We need FastReport libraries:

1
2
3
using FastReport;
using FastReport.Export.Html;
using FastReport.Utils;

 I will pass the report title via the parameter:

1
2
3
4
5
6
7
 static void Main(string[] args)
 {
 if (args.Length > 0)
 DoExport(args[0]);
 else
 Console.WriteLine("Set the report file (*.frx) as parameter");
 }

Create the method of exporting report to HTML and archiving in ZIP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
private static void DoExport(string reportFile)
 {
 if (File.Exists(reportFile))
 {
 Config.WebMode = true; // set WebReport mode for disable all progress and enable thread-safe code
 using (Report report = new Report()) // create new report object
 {
 report.Load(reportFile); // load report from file
 report.Prepare(); // prepare report
 using (HTMLExport html = new HTMLExport()) // create new export object
 {
 html.SaveStreams = true; // enable saving in streams
 report.Export(html, (Stream)null); // set target stream in null - we have multiple streams inside export object
 if (html.GeneratedFiles.Count > 0)
 {
 ZipArchive zip = new ZipArchive(); // create ZIP object
 for(int i = 0; i < html.GeneratedFiles.Count; i++)
 zip.AddStream(html.GeneratedFiles[i], html.GeneratedStreams[i]); // add streams with file names in zip
 zip.SaveToFile(Path.GetFileNameWithoutExtension(reportFile) + ".zip"); // write zip in file
 }
 } 
 }
 }
 else
 Console.WriteLine("File " + reportFile + " not found!");
 }

Here it should be noted about Config.WebMode property - it enables the "quiet" mode of reporting without issuing any dialogs and progress bars.

In this example I pack one report, but there's no reason why not to put a few pieces in archive.

Now start the application in the console with the parameter. The parameter specifies the path to the report. And get a zip-file in the folder with the application. The archive is a packed report in html format. Thus, using a web service we can pass into our web application an archive with one or more reports.

 

.NET .NET FastReport FastReport HTML HTML
March 11, 2025

How to Use FastReport .NET Avalonia on Fedora Workstation with Wayland Protocol

In this article, we will discuss how to run FastReport .NET Avalonia on the "Fedora Workstation 39" operating system with Wayland protocol.
March 07, 2025

How to Create a QR Code with an Image in FastReport .NET

The article figured out how to insert a picture into a QR Code from the report designer FastReport.NET in just a couple of clicks.
February 20, 2025

Using Dot Matrix Printers for Printing Receipts

This article will explore how to integrate a dot matrix printer into the process of creating and outputting reports using FastReport .NET.
Fast Reports
  • 800-985-8986 (English, US)
  • +31 97 01025-8466 (English, EU)
  • +49 30 56837-3928 (German, DE)
  • +55 19 98147-8148 (Portuguese, BR)
  • info@fast-report.com
  • 66 Canal Center Plaza, Ste 505, Alexandria, VA 22314

© 1998-2025 Fast Reports Inc.