Fast sending to print reports in MVC
For harmony of Web report with the stylistics of the web page, many people are disable WebReport object toolbar. Thus the user is deprived of options to export and print reports. But if you still want to allow the user to print a report from a browser, you can create a special button to print. By clicking on it, the user will receive the print dialog box.
Now I'll show how to invoke the print dialog in MVC web project.
To start, add a button in the view. I placed it on the home page of my demo web application. Find the Index.html file in the Solution Explorer:
Add the button to the desired location:
Here Print - name of the handler in the controller. And Home - name of the controller.
Go to the Controllers folder. HomeController.cs file:
Add the method to the class code:
As you can see a method called Print, as in the button that we added. Let's take a closer look at the code.
In the first line, we created an instance of an object WebReport. Then, we created a DataSet to work with the data. DataSet can work with xml database. Actually in the third line, I specify the path to the database. Using the method RegisterData we register the data source in the report object. Then, load the report template into the report object. It is located in the same folder as the database. EmbedPictures property allows you to embed images in html log file. Finally, the last line starts the printing of the report in the browser.
If you want to save the report in PDF format, then change the last line to:
In this case, you can use options to format pdf file. For example:
This option gives the best image quality for printing.
Now you need to add the handler in the Web.config:
Now run the application. Here is the button on the Web page:
Сlick on it. And we get the page with the report and the print dialog:
Here is the saving in PDF:
Thus it is possible to call the print dialog using the user's button instead of WebReport object toolbar. This can be useful when you embed report controls in web page design.
In this example, we have not added any web report object on the web page. The report is based on a separate page, just before printing. This is useful when there is no need to display the report on a Web page.