HTML to PDF slow rendering using Aspose.HTML (C#)

I have downloaded the evaluation version of Aspose.Html to see whether it’s suitable. When I render an HTML file to a PDF it takes 2.8 seconds for a 2 page PDF. This is pretty slow, particularly considering the throughput that it would be used for.

Is this typical of this component or is the evaluation version purposely slowed down?

@mhoganjones

Thank you for contacting support.

We would like to share with you that all necessary resources are allocated and Document Object Model (DOM) of Aspose.HTML API is loaded into memory, which consumes some time. Please try to convert same HTML file to a PDF document multiple times in same application, and you will notice lesser time being consumed during conversion. Moreover, there is not any performance related limitation in evaluation mode of the product.

In case slower performance persist in your environment, please share a narrowed down sample application along with source and generated files while mentioning your observations about performance, so that we may investigate to help you out.

I have notice the same situation. Rendering a html to pdf takes around 2 seconds and new HtmlDocuments tooks around another 2 seconds. Before I was using the TuesPechkin Lib and the creation time of pdf was much more faster. Is there a way to make it faster or do I something wrong?
I’m using the latest version of aspose.html and this is my code. Here you can find my html sample generated as pdf AsposeHtmlSample.zip (38.3 KB)

using (MemoryStream memoryStream = new MemoryStream())
        {
            var options = new PdfRenderingOptions
            {
                PageSetup = {
                    AnyPage = new Aspose.Html.Drawing.Page
                    {
                        Margin = new Margin(40, 40, 40, 40)                            
                    },
                    AtPagePriority = AtPagePriority.CssPriority,                        
                    AdjustToWidestPage = false,
                }                    
            };
                        

            using (PdfDevice pdfDevice = new PdfDevice(options, memoryStream))
            {                           
                using (var renderer = new HtmlRenderer())
                {                                                
                    string innerDocHtml = XmlToHtml(xmlContent, xslContent, removeMetaTags, language);

                    using (HTMLDocument htmlDocument = new HTMLDocument(innerDocHtml, ""))
                    {                                         
                        renderer.Render(pdfDevice, htmlDocument);
                        //Save memoryStream into output pdf file
                        return memoryStream.ToArray();
                    }
                }
            }
        }

@cpiock

Thank you for contacting support.

The code snippet includes a method call XmlToHtml so please share its definition as well and ensure the code is SSCCE so that we may proceed further.

private static string XmlToHtml(string xmlString, string xslString, bool removeMetaTags, LanguageDto language)
    {
        HtmlAgilityPack.HtmlDocument docHtml = new HtmlAgilityPack.HtmlDocument();
        docHtml.LoadHtml(TransformXMLToHTML(xmlString, xslString, language));
        if(removeMetaTags)
        {
            List<string> xpaths = new List<string>();
            foreach (var node in docHtml.DocumentNode.Descendants()
                            .Where(n => n.Name.ToUpper() == "META")
                            .ToList())
            {
                if (!xpaths.Any(n => n == node.XPath.ToLower()))
                {
                    xpaths.Add(node.XPath.ToLower());
                }
            }

            for (int i = xpaths.Count - 1; i >= 0; i--)
            {
                docHtml.DocumentNode.SelectSingleNode(xpaths[i]).Remove();
            }
        }
        return docHtml.DocumentNode.InnerHtml;
    }

    private static string TransformXMLToHTML(string xmlString, string xslString, LanguageDto language)
    {
        XsltArgumentList args = new XsltArgumentList();
        if (language == LanguageDto.De)
        {
            args.AddParam("lang", "", "de");
            args.AddParam("localeUrl", "", Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), "OficialFormats", "Translations", "Translations.de.xml"));
        }
        else
        {
            args.AddParam("lang", "", "it");
            args.AddParam("localeUrl", "", Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), "OficialFormats", "Translations", "Translations.xml"));
        }

        StringReader stringReaderXML = new StringReader(xmlString);
        StringReader stringReaderXSL = new StringReader(xslString);

        var settings = new XsltSettings
        {
            EnableDocumentFunction = true
        };

        XslCompiledTransform transform = new XslCompiledTransform(false);

        using (XmlReader reader = XmlReader.Create(stringReaderXSL))
        {
            transform.Load(reader, settings, null);
        }
        
        StringWriter results = new StringWriter();
        using (XmlReader reader = XmlReader.Create(stringReaderXML))
        {
            transform.Transform(reader, args, results);
        }

        return results.ToString();
    }

}

Also changed the renderer.Render(pdfDevice, htmlDocument); to Aspose.Html.Converters.Converter.ConvertHTML(htmlDocument, saveOptions, streamProvider);. It is a little bit faster but not as fast as the old version

@cpiock

Thank you for the details.

We are checking this and will get back to you shortly.

hi @Farhan.Raza any news about this? Now we have changed from Free to Apsose.Total. Time is the same. Thanks

@cpiock

We have logged an investigation ticket as HTMLNET-2372 in our issue tracking system. We will further investigate the scenario in details and keep you posted with the status of ticket resolution. Please spare us little time.

We are sorry for the inconvenience.

1 Like

@asad.ali where i can follow up this ticket in my subscription area i can’t access these ticket informaton

@cpiock

The ticket has been logged in our internal issue management system and you may not be able to access it. However, we will keep posting you on the status of its resolution. We will let you know as soon as we have some definite updates in this regard.

Ok thanks. Are you able to reproduce the problem?

@cpiock,

Yes, we have been able to observe the issue.

1 Like

@Adnan.Ahmad did you have any feedback for me?

@cpiock,

I like to inform that currently we are working on new rendering engine which will significantly speed up rendering, it will be released soon. I request for your patience.

Hi,
are there any updates for this Topic? I’m using Aspose.Html Version 20.9 and have performance issues.
I have a html file with a table with around 1800 rows. It takes around 3 minutes to convert.
Here is my Code:

        var bookHtml = new Aspose.Html.HTMLDocument(inputFile);
        Aspose.Html.Saving.PdfSaveOptions options = new Aspose.Html.Saving.PdfSaveOptions();
        Aspose.Html.Drawing.Page page = new Aspose.Html.Drawing.Page();
        page.Margin.Right.Length = Aspose.Html.Drawing.Unit.FromCentimeters(1.0);
        page.Margin.Left.Length = Aspose.Html.Drawing.Unit.FromCentimeters(1.0);
        page.Margin.Bottom.Length = Aspose.Html.Drawing.Unit.FromCentimeters(1.5);
        page.Margin.Top.Length = Aspose.Html.Drawing.Unit.FromCentimeters(2.0);

        options.PageSetup.AnyPage = page;
        options.PageSetup.AdjustToWidestPage = true;

        Aspose.Html.Converters.Converter.ConvertHTML(bookHtml, options,
            outputFile);

Thank’s for the help.

@FK1

Regretfully, the earlier logged ticket is not yet resolved and still under process. Would you kindly share your sample HTML in .zip format with us. We will test the scenario in our environment and address it accordingly.

[quote=“FK1, post:16, topic:174382”]
Hi there,
wondering if you’ve any updates for this Topic? I’m also using latest Aspose.Html Version 21.5.0.0 and similar performance issues.
I’m creating HTML document (size 1,629KB) in just couple of seconds but when converting that to pdf It takes around 3 minutes.
I’ve tried both options ‘Render HTML to PDF’ and ‘Aspose.Html.Converters’
Code is similar as above.

Option 1:

License licCells = new License();
licCells.SetLicense(“Aspose.Total.lic”);

using (var document = new HTMLDocument(htmlContent, “.”))
{

                    // Save the HTML document to a file 
                    document.Save(Path.Combine(OutputDir, "Test.html"));

                    // Create the instance of the PDF output device and render the document into this device
                    using (var device = new PdfDevice(Path.Combine(OutputDir, "Test.pdf")))
                    {
                        // Render HTML to PDF
                        document.RenderTo(device);
                    }

                }

Option 2:
using (var document = new HTMLDocument(htmlContent, “.”))
{

                    // Save the HTML document to a file 
                    document.Save(Path.Combine(OutputDir, "Test.html"));

                    string pdfOutputFile = Path.Combine(OutputDir, "Test.pdf");
                    var bookHtml = new Aspose.Html.HTMLDocument(Path.Combine(OutputDir, "Scheduler.html"));
                    Aspose.Html.Saving.PdfSaveOptions options = new Aspose.Html.Saving.PdfSaveOptions();
                    Aspose.Html.Drawing.Page page = new Aspose.Html.Drawing.Page();
                    page.Margin.Right.Length = Aspose.Html.Drawing.Unit.FromCentimeters(1.0);
                    page.Margin.Left.Length = Aspose.Html.Drawing.Unit.FromCentimeters(1.0);
                    page.Margin.Bottom.Length = Aspose.Html.Drawing.Unit.FromCentimeters(1.5);
                    page.Margin.Top.Length = Aspose.Html.Drawing.Unit.FromCentimeters(2.0);

                    options.PageSetup.AnyPage = page;
                    options.PageSetup.AdjustToWidestPage = true;

                    Aspose.Html.Converters.Converter.ConvertHTML(bookHtml, options,
                        pdfOutputFile);

                }

@jasdeep.grewal

We would like to share with you that the earlier logged ticket has been resolved in 21.6 version of the API which will be releasing soon in this month. We will post a notification in this thread when the fix is available. In case you still face any issue while using 21.6 version of the API, please feel free to share your sample HTML so that we can further proceed to assist you accordingly.

The issues you have found earlier (filed as HTMLNET-2372) have been fixed in this update. This message was posted using Bugs notification tool by pavel.petrushechkin