Free Support Forum - aspose.com

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.