How to convert MVC View to PDF using C# in ASP.NET Core?
The Syncfusion® HTML to PDF converter is a .NET library for converting webpages, SVG, MHTML, and HTML to PDF using C#. It is reliable and accurate. The result preserves all graphics, images, texts, fonts, and the layout of the original HTML document or webpage.
Using this library, you can convert MVC view to a PDF document using C# in ASP.NET Core.
Steps to convert the MVC view to PDF document programmatically:
- Create a new ASP.NET Core Web application project.
- In configuration window, name your project and click Next.
- Install Syncfusion.HtmlToPdfConverter.Net.Windows NuGet package as reference to your .NET application from NuGet.org.
- A default controller with name HomeController.cs gets added on creation of ASP.NET Core project. Include the following namespaces in that HomeController.cs file.
C#
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
- Add a new button in index.cshtml as shown below.
C#
Html.BeginForm("ConvertToPDF", "Home", FormMethod.Post);
{
<h2><input type="submit" value="Convert To PDF"></h2>
}
- Include the following code snippet in the HomeController.cs to convert the MVC view to PDF document.
C#
//Getting Index view page as HTML
string html = await ViewToStringAsync(this, "Index", null);
string baseUrl = string.Empty;
//Convert the HTML string to PDF document
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//Convert HTML string to PDF
PdfDocument document = htmlConverter.Convert(html, baseUrl);
//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save and close the PDF document
document.Save(stream);
document.Close(true);
return File(stream.ToArray(), "application/pdf", "ViewAsPdf.pdf");
You can download the working sample from MVC_ViewToPDF.Zip.
By executing the above sample, you will get the PDF document as follows.
Take a moment to peruse the documentation, where you can find converting HTML pages to PDF document along with respective customization options and features.
Refer here to explore the rich set of Syncfusion Essential® PDF features.
An online sample link for Converting HTML to PDF.
Note: Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn how to generate and register the Syncfusion® license key in your application and use the components without a trial message.
Conclusion
I hope you enjoyed learning about how to convert MVC View to PDF using C# in ASP.NET Core.
You can refer to our ASP.NET MVC PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET MVC PDF Example to understand how to create and manipulate data.
For current customers, you can check out our ASP.NET Core Components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!