How to Convert a Web Page Contents to PDF inside .NET Applications

This technical tip shows how to convert Web page to PDF inside .NET applications. The HtmlLoadOptions option offers the feature to load HTML contents and parse the HTML tags accordingly so that they are rendered respectively inside the resultant PDF. In order to convert Web page contents to PDF format, we can first fetch the HTML page contents using WebRequest instance, create StreamReader object and read page contents. Finally pass the contents to Document object and render the output in PDF format. When converting a web page hosted on a web server to PDF first read the contents of the page using an HttpWebRequest object and pass the content to a StreamReader object. Then create an instance of MemoryStream and instantiate the HtmlLoadOptions object while passing the web page URL. After that, initialize a Document object while passing the stream object. Optionally, set the page orientation to Landscape so that more contents can be accommodated on the page.
//your code here... //Please take a look over the following code snippet for converting Web page to PDF //[C# Code Sample] // Create a request for the URL. WebRequest request = WebRequest.Create("http://www.google.com"); // If required by the server, set the credentials. request.Credentials = CredentialCache.DefaultCredentials; // Get the response. HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // Get the stream containing content returned by the server. Stream dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. string responseFromServer = reader.ReadToEnd(); reader.Close(); dataStream.Close(); response.Close(); MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(responseFromServer)); HtmlLoadOptions options = new HtmlLoadOptions("http://www.google.com/"); // Load HTML file Document pdfDocument = new Document(stream, options); options.PageInfo.IsLandscape = true; // Save output as PDF format pdfDocument.Save(myDir + "HTMLtoPDF_DOM.pdf"); //[VB.NET Code Sample] ' Create a request for the URL. Dim request As System.Net.WebRequest = WebRequest.Create("http://www.google.com") ' If required by the server, set the credentials. request.Credentials = CredentialCache.DefaultCredentials ' Get the response. Dim response As HttpWebResponse = TryCast(request.GetResponse(), HttpWebResponse) ' Get the stream containing content returned by the server. Dim dataStream As Stream = response.GetResponseStream() ' Open the stream using a StreamReader for easy access. Dim reader As StreamReader = New StreamReader(dataStream) ' Read the content. Dim responseFromServer As String = reader.ReadToEnd() reader.Close() dataStream.Close() response.Close() Dim stream As MemoryStream = New MemoryStream(System.Text.Encoding.UTF8.GetBytes(responseFromServer)) Dim options As HtmlLoadOptions = New HtmlLoadOptions("http://www.google.com/") ' Load HTML file Dim pdfDocument As Document = New Document(stream, options) options.PageInfo.IsLandscape = True ' Save output as PDF format pdfDocument.Save("c:/pdftest/HTMLtoPDF_DOM_VB.pdf")

Url: http://www.aspose.com/.net/pdf-component.aspx

Language: C# | User: Sheraz Khan | Created: Jan 1, 2015 | Tags: Convert Web Page to PDF load HTML contents parse HTML tags to PDF convert Web page contents to PDF convert a web page hosted on web server to PDF .NET PDF component