We are going to learn Rotativa.AspNetCore tool to use to generate PDF file from View.
Its very simple to create pdf using Rotativa.AspNetCore tool.
Prerequisite
1. Visual Studio 2017 with ASP.NET CORE 2.0
2. Rotativa.AspNetCore 1.0.6
Let's start with step by step.
1. Creating Asp.Net Core MVC application.
2. During the Asp.Net Core MVC project creation.
3. Once Asp.Net Core MVC application configuration done then we are ready to install Rotativa.AspNetCore to the project.
4. Install Rotativa.AspNetCore nuget package
5. After installing NuGet package Rotativa.AspNetCore. Let's do some configuration on startup.cs class.
- Add this setting to a startup.cs class inside the configure method to set a relative path to the wkhtmltopdf.exe file to access.
6. Time to add few exes under the "wwwroot\Rotativa" folder. Going to add following files to the "Rotativa" folder.
- wkhtmltopdf.exe
- wkhtmltoimage.exe
Once done with file adding. Let's move the final step.
7. We have Home Controller, so going to utilize that controller and use it.
An Index Action will be like this.
We are passing customer object into view. so we are getting an output pdf with the customer list.
You can do more configuration and get various output.
Download source code from my Github
Its very simple to create pdf using Rotativa.AspNetCore tool.
Prerequisite
1. Visual Studio 2017 with ASP.NET CORE 2.0
2. Rotativa.AspNetCore 1.0.6
Let's start with step by step.
1. Creating Asp.Net Core MVC application.
2. During the Asp.Net Core MVC project creation.
3. Once Asp.Net Core MVC application configuration done then we are ready to install Rotativa.AspNetCore to the project.
4. Install Rotativa.AspNetCore nuget package
Install-Package Rotativa.AspNetCore -Version 1.0.6
5. After installing NuGet package Rotativa.AspNetCore. Let's do some configuration on startup.cs class.
- Add this setting to a startup.cs class inside the configure method to set a relative path to the wkhtmltopdf.exe file to access.
RotativaConfiguration.Setup(env);
6. Time to add few exes under the "wwwroot\Rotativa" folder. Going to add following files to the "Rotativa" folder.
- wkhtmltopdf.exe
- wkhtmltoimage.exe
Once done with file adding. Let's move the final step.
7. We have Home Controller, so going to utilize that controller and use it.
An Index Action will be like this.
public IActionResult Index() { var customers = GetCustomers(); return new ViewAsPdf("Index", customers); } private ListGetCustomers() { return new List () { new Customer(){ Name="Kalpesh", City="Ahmedabad"}, new Customer(){ Name="Vijay", City="Surat"}, new Customer(){ Name="John", City="New York"}, new Customer(){ Name="Smith", City="Parth"}, }; }
We are passing customer object into view. so we are getting an output pdf with the customer list.
You can do more configuration and get various output.
Download source code from my Github
0 comments:
Post a Comment