How to convert chart to PDF using C# in ASP.NET MVC?
The Syncfusion® HTML to PDF converter is a .NET PDF library that converts 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 the Chart to PDF document in C#.
Steps to convert the MVC view to PDF programmatically:
- Create a new ASP.NET MVC application project.
- Install the Syncfusion.HtmlToPdfConverter.AspNet.Mvc5 NuGet package as a reference to your MVC application from NuGet.org.
- Add new button in index.cshtml for converting chart to PDF document.
CSHTML
@{ Html.BeginForm("ConvertToPDF", "Home", FormMethod.Post); { <h2><input type="submit" value="Convert To PDF"></h2> } }
- Include the following namespaces in HomeController.cs file.
C#
using Syncfusion.HtmlConverter; using Syncfusion.Pdf; using System.IO;
- Include the following code snippet in the HomeController.cs to convert the chart to PDF document.
C#
//Initialize HTML to PDF converter HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(); //Initialize the Blink converter settings. BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings(); //Set additional delay; units in milliseconds; blinkConverterSettings.AdditionalDelay = 5000; //Assign Blink settings to HTML converter. htmlConverter.ConverterSettings = blinkConverterSettings; //Convert URL to PDF PdfDocument document = htmlConverter.Convert(Server.MapPath("~/App_Data/ChartToPDF.html")); MemoryStream stream = new MemoryStream(); //Save and close the PDF document document.Save(stream); document.Close(true); return File(stream.ToArray(), "application/pdf", "Output.pdf");
You can download the working sample from ChartToPDFSample.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.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion® license key in your application to use the components without trail message.
Conclusion
I hope you enjoyed learning about how to convert chart to PDF using C# in ASP.NET MVC Library.
You can refer to our ASP.NET MVC PDF feature tour page to know about its other groundbreaking feature representations 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 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!