Converting html to pdf using URL in ASPOSE.net

Hi Aspose Team,

I want to generate a pdf file using any given url. I am using AngularJS. How do I achieve it??

For example: If I provide https://www.northwesternmutual.com/live-life-differently as my input url,
then Aspose should generate a pdf file with all content present on this page including proper styles.

Currently, if I try converting it, I am getting partially generated file with Loading ...text. I have attached a sample file which I have gendered through Aspose library.

Please provide any workable solution to convert the html page (via url) to pdf.

Thanks in Advance.

Umesh

Hi Umesh,


Thank you for contacting support. We have generated a PDF from the given URL with the latest version 17.5 of Aspose.Pdf for .NET API and it looks fine. We have attached an output PDF to this reply. Please refer to this code example: Convert Web Page to PDF

Thanks for the response.

From your reply, I have learnt that you are using version 17.5 and I was using version 11.4. So I download the latest version from the website which I got version 17.4 at the max. I referenced Aspose.Pdf.dll (17.4) to my project (please refer attachment “Aspose pdf 17.4.0.PNG”) and try to do as suggested here :

Unfortunately, I am not getting proper output. Please refer attachment “output_landsend.pdf”

Also, it took around 10 minutes to generate this output.

Moreover, it behaves totally weird when I am trying to generate pdf from my webpage which is built using AngularJS. It generates only partial output as shown in attachment “fund-porrfolio.pdf.”

Do you suggest, should I take different approach when for AngularJS websites?
I will appreciate if you can provide me the working example (code). Please respond as soon as possible as I am stuck here.

I am using following code :

string dataDir = @“C:\Temp”;
string webPageUrl = “https://www.landsend.com/”;
byte[] content;
//Read the contents of the page using an HttpWebRequest object.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webPageUrl);
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
request.ProtocolVersion = HttpVersion.Version10;
request.ServicePoint.ConnectionLimit = 1;
request.KeepAlive = false;
request.Accept = “/”;
WebProxy proxy = new WebProxy(“Proxy address here”, true);
//To access external website. need to use proxy
proxy.Credentials = new NetworkCredential("<>", “<>”, “<>”);
request.Proxy = proxy;
request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
using (WebResponse webResponse = request.GetResponse())
{
//Pass the content to a StreamReader object
using (Stream stream = webResponse.GetResponseStream())
{
using (MemoryStream ms = new MemoryStream())
{
stream.CopyTo(ms);
content = ms.ToArray();
}
}
}
//Instantiate the HtmlLoadOptions object while passing the web page URL.
HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions(webPageUrl);
htmlLoadOptions.PageInfo.Margin.Bottom = 10;
htmlLoadOptions.PageInfo.Margin.Top = 20;
//Create an instance of MemoryStream.
//Initialize a Document object while passing the stream object.
Document doc = new Document(new MemoryStream(content), htmlLoadOptions);
doc.Save(dataDir + “output.pdf”);

Hi Umesh,


Thank you for the inquiry. The latest version 17.5 of Aspose.Pdf for .NET API is available in the download section. Furthermore, you are missing UTF-8 encoding as narrated in the help topic.
umesh.mehta:
using (WebResponse webResponse = request.GetResponse())
{
//Pass the content to a StreamReader object
using (Stream stream = webResponse.GetResponseStream())
{
using (MemoryStream ms = new MemoryStream())
{
stream.CopyTo(ms);
content = ms.ToArray();
}
}
}
Please modify these lines of code as below and pass the stream object to the constructor of Document class:
[.NET, C#]
<span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// Get the response.<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
HttpWebResponse response = (HttpWebResponse)request.GetResponse();<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// Get the stream containing content returned by the server.<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
Stream dataStream = response.GetResponseStream();<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// Open the stream using a StreamReader for easy access.<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
StreamReader reader = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> StreamReader(dataStream);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// Read the content.<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>string<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> responseFromServer = reader.ReadToEnd();<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
reader.Close();<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
dataStream.Close();<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
response.Close();<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>MemoryStream stream = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> MemoryStream(System.Text.Encoding.UTF8.GetBytes(responseFromServer));

If this does not help, then please create a small project application and make sure that it reproduces the problem in your environment, and then share a project Zip with us. We will investigate and share our findings with you.