DEV Community

Cover image for Print SSRS Reports in ASP.NET Core with Report Writer and PrintJS
UmamaheswariPrakash for Bold Reports

Posted on • Edited on

Print SSRS Reports in ASP.NET Core with Report Writer and PrintJS

ASP.NET Core is a popular framework for building web applications that can run on various platforms, and one of the requirements for many web applications is to generate and print SSRS reports. In this blog, we will explore how to generate and print SSRS reports using Bold Reports’ Report Writer and PrintJS in an ASP.NET Core application. We’ll discuss the benefits of using Bold Reports ASP.NET Core Report Writer, a powerful reporting tool that allows developers to create and design sophisticated reports using various data sources. Additionally, we’ll learn how PrintJS, a lightweight and straightforward library, can facilitate the printing of PDF files generated from RDL reports using Bold Reports Report Writer.

Let’s dive in!

Prerequisites

Ensure your development environment includes the following:

  1. Visual Studio Code
  2. .NET6.0 SDK(or) Higher.

Create an ASP.NET Core 6 application

  1. Open Visual Studio Code and go to the terminal menu; click on the New Terminal menu item.
  2. Create an ASP.NET Core Web MVC application. On executing the following command, the ASP.NET Core Web App (MVC) template will be created.


dotnet new mvc --framework net6.0 --name CoreReportWriter


Enter fullscreen mode Exit fullscreen mode

3.The ASP.Net Core Web App (MVC) is created.
Create a new .NET 6 project.

Install the NuGet Packages

  1. To use the Bold Reports Report Writer library in your project, you need to install the BoldReports.Net.Core NuGet package.
  2. Run the following commands to install the BoldReports.Net.Core NuGet package in the application.


    cd CoreReportWriter
    dotnet add package BoldReports.Net.Core


Enter fullscreen mode Exit fullscreen mode

Install Print.js package

  1. First, you need to install Print.js in your ASP.NET Core project to use the PrintJS related APIs. You can install it using npm or by downloading it from the Print.js website.
  2. To install it using npm, in your project’s Terminal, run the following command.


     npm install print-js


Enter fullscreen mode Exit fullscreen mode

Create a controller action to Print SSRS reports

Next, you need to create a controller action that generates and prints SSRS reports in PDF format.

  1. Open the Controllers/HomeController.cs file and create local variables inside the HomeController class, like the following. ```

private Microsoft.AspNetCore.Hosting.IWebHostEnvironment _hostingEnvironment;
public HomeController(Microsoft.AspNetCore.Hosting.IWebHostEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}

2.Add the **PrintPDF()** function to create a **FileStream** object called **inputStream** to open the “sales-order-detail.rdl” file in read-only mode. Refer to the following code snippet.  

Enter fullscreen mode Exit fullscreen mode

public IActionResult PrintPDF()
{
var rdlPath = "\Resources\sales-order-detail.rdl";
FileStream inputStream = new(_hostingEnvironment.WebRootPath +
rdlPath,FileMode.Open,FileAccess.Read);
}

3.To generate the SSRS RDL report as a PDF, pass the file stream object to the Bold Reports Report Writer’s **LoadReport** method and call the **Save** method to save it as a PDF file. The following code does this.
Enter fullscreen mode Exit fullscreen mode

BoldReports.Writer.ReportWriter writer = new();
writer.LoadReport(inputStream);
using MemoryStream memoryStream = new();
writer.Save(memoryStream, BoldReports.Writer.WriterFormat.PDF);
return File(memoryStream.ToArray(), "application/pdf", "example.pdf");


_This will pass the report as a PDF to the client side._

## Initialize Print.js to print the PDFs

To print the PDF files generated from the server side to the client side, you need to use Print.js’s printJS API in your cshtml page.

1. Open the Views/Home/Index.cshtml page.
2. Remove the existing **div** code and add the following code. This will create a button to print the PDF file.
Enter fullscreen mode Exit fullscreen mode

Print SSRS reports

3.Since I already installed the **print-js** package, to make this sample simple, I am copying and pasting the print.js file from the node\_modules/print-js/dist folder to the wwwroot/js folder in my application.
4.Now, I can use the print.js file in my Index.cshtml file and call the **printJS** method to save the server side’s file object as a PDF file on the client side. To do this, in the Index.cshtml page, add a Scripts section and add the print.js location path. Call the server side’s **PrintPDF** method and get the file and pass the file object to the **printJS()** method to save it as a PDF. The following code explains this.
Enter fullscreen mode Exit fullscreen mode

@section Scripts {

<br> document.getElementById(&quot;print-SSRS-reports&quot;).addEventListener(&quot;click&quot;, function () {<br> fetch(&quot;/Home/PrintPDF&quot;).then(response =&gt; response.blob()).then(blob =&gt; {<br> printJS({printable:URL.createObjectURL(blob),type:&quot;pdf&quot;});<br> });<br> });<br>
}


## Add already created reports

1. Create a Resources folder in the wwwroot folder in your application to store the RDL reports.
2. Download the sales-order-detail.rdl file from [here](https://www.syncfusion.com/downloads/support/directtrac/general/ze/sales_order_detail109018224). For more sample reports, refer to the [samples and demos](https://help.boldreports.com/embedded-reporting/aspnet-core-reporting/report-viewer/samples-and-demos/?utm_source=dev&utm_dev=partner_blog&utm_campaign=dev_SSRSReportsPrintJS_cy23) section.
3. Extract the compressed file and paste the sales-order-detail.rdl file to the  **Resources**  folder.

## Run the application to print SSRS reports using PrintJS

Run the application and click the “Print SSRS reports” button. The print page will automatically open, allowing you to print the SSRS report file.

![A computer screen displaying the process of printing an SSRS report as a PDF using PrintJS.](https://www.boldreports.com/wp-content/uploads/2023/05/Run-the-application-compressed.jpg)
_Printing an SSRS Report as a PDF using PrintJS._

## Conclusion

I hope this blog provided sufficient guidance for printing SSRS reports in an ASP.NET Core app with Report Writer and PrintJS. To learn more about the Report Writer for .NET, look through our [documentation](https://help.boldreports.com/embedded-reporting/aspnet-core-reporting/report-writer/export-ssrs-rdl-report/?utm_source=dev&utm_dev=partner_blog&utm_campaign=dev_SSRSReportsPrintJS_cy23). To experience the features live, check out our [demo samples](https://demos.boldreports.com/aspnet-core/ReportWriter/?utm_source=dev&utm_dev=partner_blog&utm_campaign=dev_SSRSReportsPrintJS_cy23).

If you have any questions, please post them in the comments section below. You can also contact us through our [contact page](https://www.boldreports.com/contact/?utm_source=dev&utm_dev=partner_blog&utm_campaign=dev_SSRSReportsPrintJS_cy23), or if you already have an account, you can [log in](https://www.boldreports.com/account/?utm_source=dev&utm_dev=partner_blog&utm_campaign=dev_SSRSReportsPrintJS_cy23) to ask your question.

Bold Reports offers a [15-day free trial](https://www.boldreports.com/pricing/?utm_source=dev&utm_dev=partner_blog&utm_campaign=dev_SSRSReportsPrintJS_cy23) without any credit card information required. We welcome you to start a free trial and experience Bold Reports for yourself. Try it and let us know what you think!

Catch us on [Twitter](https://twitter.com/boldreportsoffl), [Facebook](https://www.facebook.com/boldreportsofficial/), and [LinkedIn](https://www.linkedin.com/showcase/boldreportsofficial) for info about upcoming releases.

The post [Print SSRS Reports in ASP.NET Core with Report Writer and PrintJS](https://dev.to/barbraweke/print-ssrs-reports-in-aspnet-core-with-report-writer-and-printjs-10j0-temp-slug-8418100) appeared first on [Bold Reports](https://www.boldreports.com).
Enter fullscreen mode Exit fullscreen mode

Top comments (0)