Performance issue with HtmlRenderer Render method


#1

Certi.zip (9.2 KB)

Stream inputstream = new MemoryStream(htmlContent);
// File name for resultant PDF file
// Create PdfRendering Options object
Aspose.Html.Rendering.Pdf.PdfRenderingOptions pdf_options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions();

        // The PageSetup also provides different properties i.e. FirstPage, LastPage, LeftPage, RightPage and they are used to setup (PageSize, Margin) for every page. 
        // In most cases, usage of setup any page is enough, but in some complicated cases, you may need to fine tune page settings. It can be done either by CSS styles or by using rendering options.
        // the size for drawing is in pixels
        var width = Aspose.Html.Drawing.Unit.FromMillimeters(297);
        var height = Aspose.Html.Drawing.Unit.FromMillimeters(420);
        var pageSizeA3 = new Aspose.Html.Drawing.Size(width, height);

        pdf_options.PageSetup.AnyPage = new Aspose.Html.Drawing.Page(pageSizeA3);
        pdf_options.PageSetup.AdjustToWidestPage = true;

        Stream outputStream = new MemoryStream();

        // Instantiate PdfDevice object while passing PdfRenderingOptions and resultant file path as arguments
        using (Aspose.Html.Rendering.Pdf.PdfDevice pdf_device = new Aspose.Html.Rendering.Pdf.PdfDevice(pdf_options, outputStream))
        // Create HtmlRenderer object
        using (Aspose.Html.Rendering.HtmlRenderer renderer = new Aspose.Html.Rendering.HtmlRenderer())
        // Create HtmlDocument instance while passing path of already created HTML file
        using (Aspose.Html.HTMLDocument html_document = new Aspose.Html.HTMLDocument(inputstream, string.Empty))
        {
            // Render the output using HtmlRenderer
            renderer.Render(pdf_device, html_document);
        }

        Stream alteredoutputStream = ApplyDocumentProperties(outputStream, IsCertificate);

        byte[] buff = null;
        BinaryReader br = new BinaryReader(alteredoutputStream);
        alteredoutputStream.Position = 0;
        long numBytes = alteredoutputStream.Length;

        buff = br.ReadBytes((int)numBytes);

        return buff;<a class="attachment" href="/uploads/discourse_instance3/27676">Certi.zip</a> (9.2 KB)

We are using the Aspose.HTML (18.9.0) and Aspose.PDF (19.1.0) and facing the performance issue with html to pdf conversation.

Please find attached html file in zip and sample code. renderer.Render(pdf_device, html_document); this line taking the 10-14 secs for html to pdf convert.

Thank You in Advance for help!


#2

@cyginfo

Thank you for contacting support.

Please try using below code snippet in your environment and then share your kind feedback with us. It is consuming around 7-8 seconds for rendering the document with Aspose.HTML for .NET 19.6.

// Source HTML document  
HTMLDocument htmlDocument = new HTMLDocument(inputFile);
// Initialize PdfSaveOptions 
Aspose.Html.Saving.PdfSaveOptions options = new Aspose.Html.Saving.PdfSaveOptions
{
    JpegQuality = 100
};
// Output file path 
string outputPDF = dataDir + "HTMLtoPDF_Converter.pdf";

var stopwatch = new Stopwatch();
stopwatch.Start();
// Convert HTML to PDF
Aspose.Html.Converters.Converter.ConvertHTML(htmlDocument, options, outputPDF);
stopwatch.Stop();
Console.WriteLine("Time Consumed: " + stopwatch.Elapsed.Seconds + " seconds");

We hope this will be helpful. Please feel free to contact us if you need any further assistance.