How to convert HTML to PDF using web API in .NET?
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, text, fonts, and the layout of the original HTML document or webpage.
Using this library, you can convert the HTML to PDF using the Web API.
Steps to convert the HTML to PDF using the Web API programmatically:
- Create a new ASP.NET Web application project.
- Install the Syncfusion.HtmlToPdfConverter.AspNet.MVC5 NuGet package as a reference to your .NET Framework applications from NuGet.org.
- Add a button to convert the HTML to PDF in index.cshtml.
<div class="jumbotron"> <h1>ASP.NET</h1> <input type="submit" value="Generate PDF" class="Button" id="btn1" onclick="HtmlToPDF()" /> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> $('#btn1').click(function HtmlToPDF () { $.ajax({ url: 'http://localhost:62944/api/values?url=http://www.google.com', type: "GET", success: function (data) { var filename = "HtmlToPDF.pdf"; if (window.navigator && window.navigator.msSaveOrOpenBlob) { // IE and Edge version var byteCharacters = atob(data); var byteNumbers = new Array(byteCharacters.length); for (var i = 0; i < byteCharacters.length; i++) { byteNumbers[i] = byteCharacters.charCodeAt(i); } var byteArray = new Uint8Array(byteNumbers); var blob = new Blob([byteArray], { type: 'application/pdf' }); window.navigator.msSaveOrOpenBlob(blob, filename); } else if (typeof window.chrome !== 'undefined') { // Chrome version var pdfAsDataUri = "data:application/pdf;base64, " + data; var a = document.createElement('a'); a.href = pdfAsDataUri; a.type = 'application/pdf'; a.download = filename; a.click(); } else { // Firefox version window.open("data:application/pdf;base64, " + data); } } }); }); </script> </div>
- Include the following namespaces in the ValuesController.cs file.
C#
using Syncfusion.HtmlConverter; using Syncfusion.Pdf; using System.IO; using System.Web.Hosting;
- Include the following code snippet in ValuesController.cs to convert HTML to PDF document.
public string Get(string url) { byte[] data = HTMLtoPDF(url); return Convert.ToBase64String(data); } public byte[] HTMLtoPDF(string url) { //Initialize the HTML to PDF converter HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(); //Convert URL to PDF document PdfDocument document = htmlConverter.Convert(url); //Creates memory stream MemoryStream stream = new MemoryStream(); //Save and close the PDF document document.Save(stream); document.Close(true); return stream.ToArray(); }
A complete working sample can be downloaded from HTML-to-PDF-WebAPI.zip.
By executing the program, you will get the PDF file 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.
Click here to explore the rich set of Syncfusion Essential® PDF features.
An online sample link to Convert Html To PDF document.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without trail message.
Note:
A new version of Essential Studio® for ASP.NET is available. Versions prior to the release of Essential Studio® 2014, Volume 2 will now be referred to as a classic versions. The new ASP.NET suite is powered by Essential Studio for JavaScript providing client-side rendering of HTML 5-JavaScript controls, offering better performance, and better support for touch interactivity. The new version includes all the features of the old version, so migration is easy.
The Classic controls can be used in existing projects; however, if you are starting a new project, we recommend using the latest version of Essential Studio® for ASP.NET. Although Syncfusion® will continue to support all Classic Versions, we are happy to assist you in migrating to the newest edition.
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!