Convert ASPX Page to PDF

Hello,


What is the best practice to convert a ASPX page along with CSS styles/images to a PDF document?

Can you point to Documentation/Samples regarding the same?

We are using Aspose.PDF 17.3.0.0

Thanks

Hello Raman,

Thanks for your inquiry. You can convert HTML into PDF by using Aspose.Pdf for .NET. However in order to convert webpage into PDF format, you can fetch HTML content of the webpage using WebRequest into stream and perform conversion on that stream. Please check the following code snippet to convert a webpage into PDF format.

System.Net.WebRequest request = System.Net.WebRequest.Create("{Webpage URL}");<o:p></o:p>
// If required by the server, set the credentials.
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
// time out in miliseconds before the request times out
request.Timeout = 5000;
// Get the response.
System.Net.HttpWebResponse response = (System.Net.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("{Base URL}");
options.ExternalResourcesCredentials = System.Net.CredentialCache.DefaultCredentials;
Document pdfDocument = new Document(stream, options);
pdfDocument.Save(dataDir + @"Html2PDF_out.pdf");

For more information, please visit “Convert Web Page to PDF” article in API documentation.

rnidamar:
We are using Aspose.PDF 17.3.0.0

Please note that it is always recommended to use latest version of the API as it contains all fixes and enhancements. You can download Aspose.Pdf for .NET 17.4.0, which is latest version, from our Downloads section. In case of any further assistance, please feel free to contact us.

Best Regards,