How to convert form authenticated webpage to PDF using C#
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 authenticated webpage to PDF document in ASP.NET Web Forms by using the advanced Blink rendering engine.
Steps to convert authenticated web page to PDF programmatically:
- Create a new ASP.NET Web application project.
- Select Web Forms option and change authentication to Individual User Accounts.
- Install the Syncfusion.HtmlToPdfConverter.AspNet NuGet package as reference to your .NET Framework applications from NuGet.org.
- Add a new button in the Default.aspx as follows.
<div class="jumbotron"> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Convert authenticated web page to PDF" /> </div> <div> <asp:Label ID="Label1" runat="server" Text="After succussful registration, You can click the above button to convert authenticated web page into the PDF document."> </asp:Label> </div>
- Include the following namespaces in your Default.aspx.cs file.
C#
using Syncfusion.HtmlConverter; using Syncfusion.Pdf;
- Include the following code snippet in the click event of the button in Default.aspx.cs to create the PDF file and download it.
C#
//Set cookie name string cookieName = ".AspNet.ApplicationCookie"; //Get cookie value from HttpRequest object for the requested page string cookieValue = string.Empty; if (Request.Cookies[cookieName] != null) { cookieValue = Request.Cookies[cookieName].Value; } //Initialize HTML converter HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(); //Blink converter settings BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings(); //Add cookies as name and value pair blinkConverterSettings.Cookies.Add(cookieName, cookieValue); //Assign the Blink settings htmlConverter.ConverterSettings = blinkConverterSettings; //Convert URL to PDF PdfDocument document = htmlConverter.Convert("http://localhost:64448/"); //Save the document into the HttpResponse stream document.Save("AuthenticatedWebPage.pdf", HttpContext.Current.Response, Syncfusion.Pdf.HttpReadType.Save); //Close the document document.Close(true); //Open the PDF document System.Diagnostics.Process.Start("AuthenticatedWebPage.pdf");
A complete working sample can be downloaded from AuthenticatedWebPageToPDF.zip.
By executing the program, 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.