We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

CSS not working correctly when converting from html to pdf

Hi,
I am trying to convert the HTML string to PDF using aspose pdf. But css is not applied in the generated PDF. Please find the attchment.

aspose files.zip (115.4 KB)

Following is the c# code used
string text = System.IO.File.ReadAllText(@“C:\Users\Dotnet\Desktop\Desktop data\testHtml.html”);
HtmlLoadOptions objLoadOptions = new HtmlLoadOptions();
Document doc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(text)), objLoadOptions);
objLoadOptions.PageInfo.Margin.Bottom = 10;
objLoadOptions.PageInfo.Margin.Top = 20;
string FileName = “Aspose_” + DateTime.Now.ToString(“dd-MM-yyyy”) + “.pdf”;
doc.Save(@“e:” + FileName);

thanks & regards,
Gaurav

@gaurav.thakur90

Thank you for contacting support.

I would like to request you to use the path to your resource files (CSS file, Images etc) in the constructor of HtmlLoadOptions class as in the code below:

   HtmlLoadOptions objLoadOptions = new HtmlLoadOptions("Path");

In case the issue persists, please share necessary resources (CSS file, Images etc) with us so that we may proceed to help you out.

Hi Farhan, Thanks for the response.
Earlier I was converting html to pdf with internal css (Attachment in previous post). In that css was not applied properly.
Now I tried using external css. This time css is not applied at all. Please check where I am going wrong.
Following is the code
string text = System.IO.File.ReadAllText(@“C:\Users\Dotnet\Desktop\aspose\testHtml.html”);
HtmlLoadOptions objLoadOptions = new HtmlLoadOptions(@“C:\Users\Dotnet\Desktop\aspose\testCss.css”);
Document doc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(text)), objLoadOptions);
objLoadOptions.PageInfo.Margin.Bottom = 10;
objLoadOptions.PageInfo.Margin.Top = 20;
string FileName = “Aspose_” +DateTime.Now.ToString(“dd - MM - yyyy”) + “.pdf”;
doc.Save(@“e:” +FileName);

Please find attachment having html and css file.
aspose.zip (1.9 KB)

@gaurav.thakur90

Thank you for sharing requested data.

I would like to share with you that the HTML file shared by you does not open in a browser correctly. Kindly place the HTML file and the CSS file in the same folder and pass the path to that folder, instead of the path to file.

For example, keep the HTML and CSS file in aspose folder and then use below line of code. Resultant PDF will be rendered to PDF exactly as the browser displays that HTML file.

HtmlLoadOptions objLoadOptions = new HtmlLoadOptions(@“C:\Users\Dotnet\Desktop\aspose\”);

I have attached a sample HTML, CSS file, generated output file and the sample code to work with it. Sample.zip

    HtmlLoadOptions options = new HtmlLoadOptions(@"D:\Sample\");
    // Load HTML file
    Document doc = new Document(@"D:\Sample\source.html", options);
    // Save HTML file
    doc.Save(@"D:\Sample\Aspose_18.1.pdf");

I hope this will be helpful. Please feel free to let us know if you need any further assistance.

Hi Farhan, I tried as the steps are given above.
Still css is not properly applied. Please find updated files uploaded. Files also include image files
“AsposeOutput” : pdf generated by aspose
“BrowserOutput” : Html output shown by browser.

Please let me know if any changes required or if there is any alternate method to get desired output.
Sample.zip (358.2 KB)

@gaurav.thakur90

Thank you for elaborating it further.

I have worked with the data shared by you and have been able to reproduce the issue mentioned by you. Following tickets have been logged in our issue management system for further investigation and resolution.

  • PDFNET-44191: Problem with PDF file generated using Aspose.PDF API
  • HTMLNET-965: Problem with PDF file generated using Aspose.HTML API

These issues appear to be file specific issues as other PDF files are generated fine and CSS styling is applied as expected. The issue IDs have been linked with this thread so that you will receive notifications as soon as the issues are resolved.

We are sorry for the inconvenience.

The issues you have found earlier (filed as HTMLNET-965) have been fixed in Aspose.HTML for .NET 18.4. This message was posted using BugNotificationTool by @Farhan.Raza