Home > Code > VB.NET > How to Convert a Web Page Contents to PDF inside .NET Applications

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

by sher azam   on Jan 01, 2015   Category: VB.NET   |  Views: 1526    |  Points: 25   |  Gold 



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.

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")



More about Aspose.Pdf for .NET

- Homepage of Aspose.Pdf for .NET: http://www.aspose.com/.net/pdf-component.aspx

- Read More about Working with Document Conversion: http://www.aspose.com/docs/display/pdfnet/Working+with+Document+Conversion


Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts