Aspose.PDF - HTML to PDF Conversion with CSS

Greetings Aspose support and community,

Please see the following code… we are trying to simply get a page to save as a PDF document, however when we try to reference stylesheets it is as if they were never referenced. We get the same result when we do not have any css references. Thanks for your help. An example output is attached.

// The address of the web URL which you need to convert into PDF format
string WebUrl = @"https://sitefinity.dmainc.com/about-us/";

// create a Web Request object to connect to remote URL
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(WebUrl);

// set the Web Request timeout
request.Timeout = 10000; // 10 secs

// Retrieve request info headers
HttpWebResponse localWebResponse = (HttpWebResponse)request.GetResponse();

// Windows default Code Page (Include System.Text namespace in project)
Encoding encoding = Encoding.GetEncoding(1252);

// Read the contents of into StreamReader object
StreamReader localResponseStream = new StreamReader(localWebResponse.GetResponseStream(), encoding);

// Instantiate an object PDF class
Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();

// add the section to PDF document sections collection
Aspose.Pdf.Generator.Section section = pdf.Sections.Add();

//Create text paragraphs containing HTML text
Aspose.Pdf.Generator.Text text = new Aspose.Pdf.Generator.Text(section, localResponseStream.ReadToEnd());

// enable the property to display HTML contents within their own formatting
text.IsHtmlTagSupported = true;

// Add the text object containing HTML contents to PD Sections
section.Paragraphs.Add(text);

pdf.HtmlInfo.ExternalResourcesBasePath = @"https://sitefinity.dmainc.com/css/";

//Save the pdf document
pdf.Save("C:/Test/DirectHTML2pdf.pdf

I had a heck of a time getting stylesheets to work properly. I ended up going with embedded styles which solved my problem. That said, the html to pdf conversion is slooooooowwwwww. So is the conversion of PDF to a byte array. Ugh.

Hi,

Thanks for using our API.

I have tested the scenario and have observed that when trying to convert the web page (https://sitefinity.dmainc.com/about-us/) to PDF format, the resultant PDF is not identical to source page. For the sake of correction, I have logged this problem
as PDFNEWNET-36378 in our issue tracking system. We will further
look into the details of this problem and will keep you updated on the status
of correction. Please be patient and spare us little time. We are sorry for
this inconvenience.

rmorris-1:
I had a heck of a time getting stylesheets to work properly. I ended up going with embedded styles which solved my problem. That said, the html to pdf conversion is slooooooowwwwww. So is the conversion of PDF to a byte array. Ugh.
Hi Robbe,

Thanks for contacting support.

Please share some details regarding the scenario in which you are using this API so that we can test the scenario in our environment and try optimizing the performance. We are sorry for your inconvenience.

Hi,

Thanks for your patience.

We tried further investigating the issue PDFNEWNET-36378 reported earlier but as per our observations, the website https://sitefinity.dmainc.com/about-us/ seem to be down. Can you please double check at your end and share the correct URL or link for which you are facing an issue while transforming it to PDF format.