ASP.NET MVC - Create PDF files using Rotativa
Create PDF Files in ASP.NET MVC Using Rotativa
In today's digital age, generating PDF files directly from web applications has become essential for many developers. Whether it's for invoices, reports, or any other document, having a reliable way to create PDFs can save time and enhance user experience. In this tutorial, we will explore how to create PDF files in an ASP.NET MVC application using the Rotativa library.
What is Rotativa?
Rotativa is an open-source library that allows you to generate PDF documents from your ASP.NET MVC applications. Built on top of the popular wkhtmltopdf library, Rotativa converts HTML pages to PDF files seamlessly, making it an excellent choice for developers looking to create PDF files from their web applications.
Prerequisites
Before we dive into the implementation, make sure you have the following:
- Visual Studio installed on your machine.
- A basic understanding of ASP.NET MVC.
- A new or existing ASP.NET MVC project.
Step-by-Step Guide to Creating PDF Files
Step 1: Install Rotativa
To begin, you need to install the Rotativa package into your ASP.NET MVC project. Open the NuGet Package Manager Console and run the following command:
Install-Package Rotativa.AspNetCore
This command will download and install the Rotativa library into your project, enabling the PDF generation capabilities.
Step 2: Configure Rotativa
After installation, you must configure Rotativa in your application. Open the Startup.cs file and add the following line in the ConfigureServices method:
services.AddRotativa(options => options.WkhtmltopdfPath = "C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe");
Make sure to replace the path with the actual path where wkhtmltopdf.exe is located on your machine. This executable is necessary for Rotativa to function properly.
Step 3: Create a Controller Action
Next, you need to create an action in your controller that will handle the PDF generation. Here’s an example of a simple action method:
public class ReportController : Controller
{
public IActionResult GeneratePDF()
{
return new ViewAsPdf("PDFView")
{
FileName = "Report.pdf",
PageSize = Rotativa.AspNetCore.Options.Size.A4,
PageMargins = new Margins(5, 5, 5, 5)
};
}
}
In this example, ViewAsPdf is used to specify that we want to generate a PDF from a view called PDFView. You can adjust the FileName, PageSize, and PageMargins as needed.
Step 4: Create the View
You will need to create a view that will be converted into a PDF. In this case, create a new Razor view named PDFView.cshtml under the Views/Report folder:
@model YourNamespace.Models.YourModel
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>PDF Report</title>
</head>
<body>
<h1>Your Report Title</h1>
<p>This is a sample PDF report generated using Rotativa in ASP.NET MVC.</p>
<p>Model Data: @Model.Property</p>
</body>
</html>
Replace YourNamespace.Models.YourModel with the appropriate namespace and model you are using. The model data can be displayed dynamically in the PDF.
Step 5: Create a Route to Access the PDF
To allow users to access the PDF generation, you will need to set up a route. Add the following line to your Startup.cs file within the Configure method:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
This will provide the necessary routing for your application.
Step 6: Test the PDF Generation
To test your PDF generation, run your application and navigate to the URL associated with the GeneratePDF action, typically:
http://localhost:5000/Report/GeneratePDF
If everything is set up correctly, the browser should prompt you to download the generated PDF file.
Conclusion
Creating PDF files in ASP.NET MVC using Rotativa is a straightforward and efficient process. With just a few steps, you can generate high-quality PDFs that can enhance the functionality of your web applications. Whether you’re generating reports, invoices, or any other type of document, Rotativa provides a robust solution for PDF generation.
Feel free to explore further customization options with Rotativa, such as headers, footers, and more advanced styling. Happy coding!
Connect with SkillBakery Studios
Explore more tutorials, tools, and resources:
Posted by SkillBakery Studios


No comments:
Post a Comment