Export data into PDF in ASP.NET MVC

Export data into PDF in ASP.NET MVC

In this article we will discuss how to export PDF in ASP .NET MVC. Here we discuss export into PDF without using any 3rd party libraries. Please go through our previous article Export data into Excel in ASP.NET MVC. If are new to in ASP .NET MVC for step by step access please explore ASP .NET MVC Series.

How to export PDF in ASP.NET MVC

Create the Project in ASP .NET MVC and connect database and fetch the record using Dapper ORM and export the data into PDF.

  • Create a ASP.NET MVC application using Visual Studio.
  • We select as Empty project with targeting to ASP.NET MVC.
  • Create a Controller inside the controllers folder, let’s name as “EmployeeController.cs”
  • The Employee Db contains a employee table that holds the employee information. The script are find below;
Database Script
Employee table script
Employee.cs

Add a employee property class in Models folder.

Employee Property Class
Connection string

Declare the connection string in Web.config like below;

 <connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=corespider;Initial Catalog=EmployeeDb;Persist Security Info=True;User ID=sa;password=**********;Pooling=false;Connect Timeout=300000" providerName="System.Data.SqlClient" />
  </connectionStrings>

To connect with database and fetch the record, her ewe use Dapper ORM.

What is Dapper ?

Dapper is an object-relational mapping product for the Microsoft .NET platform: it provides a framework for mapping an object-oriented domain model to a traditional relational database. Its purpose is to relieve the developer from a significant portion of relational data persistence-related programming tasks.

Add Dapper package in Nuget
export data into PDF in ASP.NET MVC-Dapper
Dapper Package
EmployeeRepository.cs

Add another class in Models folder to retrieve the employee information from database.

EmployeeRepository.cs
Code Explanation
  • Here We create the IDbConnection that hold the connection string information.
  • Using Dapper ORM fetch the employee tables.
  • In real-time application you may use Stored procedure instead of text.
  • Dapper ORM directly return the list of employee.
EmployeeController.cs
Employee information
Code Explanation
  • Here we create object of the employee repository class.
  • In the Index() we fetch the employee list and return to the view.
Index.cshtml
Code Explanation
  • Here we use scaffold technique to create list of employees.
  • Added a button to Export the data into PDF.
  • Export is the action method and Employee is the Controller.

Export Action method to convert PDF

Code Explanation
  • In the export Post method we fetch the employee information.
  • To export data into PDF we use Gridview controls that are in System.Web.UI.WebControls namespace.
  • In Gridview controls hold the employee list.
  • In the string writer and string Reader st the PDF using HTMLWorker.
  • Response content type as PDF and finally append the PDFdoc that export into PDF.

Run the application and see the output

export data into PDF in ASP.NET MVC-export pdf
Export Data

After click the export button it convert the above data into PDF like below;

export data into PDF in ASP.NET MVC-PDF
Export PDF
Export PDF
Export to PDF in ASP NET MVC

Conclusion

This article explained how to export PDF in ASP.NET MVC . In this example we use Dapper ORM to fetch the data from database and using the data we export into PDF using Gridview controls that are available in System.Web.UI.WebControl namespace. Please free to feel if have any suggestion or feedback about this article.

Export data into Excel in ASP.NET MVC
Export-data-into-Excel

In this example we learn how to export the data into MS Excel in ASP.NET MVC without using any 3rd Read more