Article Overview
- Background
- Prerequisites
- How to print rdlc report directly to the printer in MVC
- Complete example
- Output
- Summary
Background
There was a situation where I had to render an RDLC report into an MVC application and directly open it to the print dialog box.
Hence, I have created an rdlc report in MVC rendered that report as bytes using ReportViewer, and saved it to a pdf file. I have used PdfReader and added JavaScript to the pdf file through PdfStamper for the Mozilla browser issue. After that, I opened a print dialog using JavaScript.
Here, I have kept all the implementation details along with a complete example.
Prerequisites
- You should have a basic knowledge of RDLC report implementation as well as MVC applications.
- Reference of "Microsoft.ReportViewer.WebForms" and "iTextSharp"
How to print the RDLC report directly to the printer in MVC?
There are mainly four key steps to implement this.
- Create rdlc report in MVC
- Use ReportViewer and render as bytes
- Create pdf file using PdfReader and add JavaScript to pdf file through PdfStamper
- Open Print dialog using JavaScript
Now, let us see in detail.
Create RDLC report
Our first task is to add and create a report file for the project.
First, create an MVC project.
Second, add a new RDLC report by “Add New Item” => “Visual C#” => “Report” selection.
![MVC Project]()
Third, design report. To make it easy to understand I have created one simple static report without any database connection just for simplicity.
![Static report]()
User ReportViewer and render as bytes
Now, our second task is to use the (consume) report in the C# (code).
First, add the “Microsoft.ReportViewer.WebForms” reference to the project.
![Web form]()
Second, add the “Microsoft.ReportViewer.WebForms” namespace.
Third, write code and set the report path.
Create pdf file using PdfReader and add JavaScript to pdf file through PdfStamper.
Now, we have to render (convert) the report into bytes and save it as a PDF file along with some JavaScript settings like the following.
First, render the report into bytes.
Second, add iTextShart using NuGet to save the pdf file and add some properties to that pdf file as described below.
![Nuget]()
Here, PdfStamper is being used to add JavaScript so that a print popup will open especially for the Firefox web browser.
Third, return the created pdf file’s path.
Note, that here I am creating a pdf file and storing it into the “TempFiles” folder location.
Open Print dialog
Now, our last task is to open a file into the print dialog.
First, place a placeholder for pdf to show.
Second, assign pdf and call print option from JavaScript.
That’s it.
Complete example
For your reference, I have kept the complete example in a single folder and uploaded that with this article and it contains the below files.
- Index.cshtml (View)
- HomeController.cs (Controller)
- Report1.rdlc (Report)
- TempFiles (Folder to store created pdf file)
The complete code is like the following.
View (Index.cshtml)
Controller (HomeController.cs)
Output
The output will be like the following in Chrome browser after clicking on the "Export to PDF" button.
![Output]()
Summary
Now, I believe you will be able to print rdlc reports directly to the printer in MVC.