HTML to PDF - CSS Performance Recommendations

Hi Aspose Team!

Posting our question here as the support for our 11.9 OEM license has expired, and would like to know the answer to this question before renewing…

We have the need to produce some pretty large PDF documents that we are currently producing using HTML and styling with CSS. Given some experimentation, there seems like a direct correlation in the performance the library and the amount of CSS provided. I ran a few different tests on a 80 page PDF:

  1. HTML to PDF - With CSS table styles - ~30 secs

test.pdf (512 KB)

  1. HTML to PDF - With CSS table styles and embedded base64 images - ~30 secs

test-with-base64-images.pdf (2 MB)

  1. HTML to PDF - No CSS - ~10 secs

test-with-no-css.pdf (2 MB)

  1. HTML to PDF - Extra CSS - ~250 secs

Loaded FontAwesome all.css even though no elements used any classes - no file attached.

  1. HTML to PDF - CSS applied inline on HTML elements - ~30 secs

File not included, not the best test since copying all the styles onto the elements would have been a lot of work, but I think I did enough to get a gauge that the performance was similar to having the styles in CSS.

All the files included here: test.zip (1.4 MB)

I totally understand we are on an older version, so I am curious what our best plan moving forward is. I’m not expecting performance miracles rendering a 1500 page PDF, but making sure we are not using the API in an unwise way. There obviously is a direct correlation to the amount of CSS in the HTML and render time - even if the CSS is not used. Many moons ago when we were initially working on PDF performance we did see a performance hike rending the CSS inline rather than referencing an external CSS file.

We have experimented primarily with two different methods - Stream + Flatten and Page + HtmlFragment. We saw slightly better performance (maybe 15%) using HtmlFragment but the code below seemed to produce the best results for our markup.

var html = RenderView(viewName, model);
var buffer = Encoding.UTF8.GetBytes(html);
var instream = new MemoryStream(buffer);
var options = new HtmlLoadOptions();
options.PageInfo.Width = Aspose.Pdf.Generator.PageSize.LetterWidth;
options.PageInfo.Height = Aspose.Pdf.Generator.PageSize.LetterHeight;
options.PageInfo.Margin.Top = 35;
options.PageInfo.Margin.Bottom = 40;
options.PageInfo.Margin.Right = 35;
options.PageInfo.Margin.Left = 35;

var doc = new Document(instream, options);

doc.Flatten();

var outstream = new MemoryStream();
doc.Save(outstream);

Here are my questions:

  1. Is there a better use of the 11.9 library that would render the CSS faster?

  2. What is the best pattern to use in the newest version?

  3. Since the trial limits page length - is there a way I can test the newest library version for extra large documents?

I’m not looking for performance miracles, but instead advice of how to best to HTML to PDF conversions as going bare metal with the API and drawing PDFs is not a real option.

Thanks in advance team - I look forward to your response.

-Max

@sycorr

Thank you for contacting support.

You are using quite old and outdated version of the API where several feature and performance enhancements have been incorporated in latest version of the API. You may test Aspose.PDF for .NET 19.4 in its full capacity by applying a free 30-days temporary license. Once you complete evaluation process, you may purchase the license to keep using API features. In case you face any issue, please feel free to let us know.

You may compare the results with latest version in same environment and then share your kind feedback with us. Moreover, we have modified your code snippet in order to test the same with latest version.

var buffer = Encoding.UTF8.GetBytes(html);
var instream = new MemoryStream(buffer);
var options = new HtmlLoadOptions();
options.PageInfo.Width = Aspose.Pdf.PageSize.PageLetter.Width;
options.PageInfo.Height = Aspose.Pdf.PageSize.PageLetter.Height;
options.PageInfo.Margin.Top = 35;
options.PageInfo.Margin.Bottom = 40;
options.PageInfo.Margin.Right = 35;
options.PageInfo.Margin.Left = 35;
var doc = new Document(instream, options);
doc.Flatten();
var outstream = new MemoryStream();
doc.Save(outstream);

Furthermore, you may use any of the approach for conversion as there is not particular recommended approach. All of these options use same underlying rendering engine for all conversions.

Hello again Farhan and Aspose Team!

I am sorry to report that using 19.4 actually produces these PDF tests slower; however, in 11.9 all of my 8 CPUs were pegged while 19.4 appears to attempt to be more machine friendly with using all cores at 50% utilization.

The zip file originally attached above contains the full HTML files for all the different tests - I would greatly appreciate someone taking a harder look at these files and see what is going on because ~60 secs to render 80 pages of tabular data needs some drastic improvement given wkhtmltopdf based libraries can get this done in less than ~7 secs (9x).

Thanks in advance,
-Max

@sycorr

Thank you for more details.

We have logged a ticket with ID PDFNET-46353 in our issue management system for further investigations. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

We are sorry for the inconvenience.