Aspose PDF Net Stalling Issue While Working with HTML

Hello Support,

We are using Aspose.PDF(DotNet) to generate PDFs from image(png), text and HTML. Recently, we upgraded our Aspose version from 10.1 to 24.11.

During this upgrade, we had issues where the HTML to PDF conversion took place, specifically with the function “AddHTMLToParagraph” in the code snippet below. The problem was that the HTML part would be blank. So we resolved this by using another function “AddHTMLToParagraphWithTable”. Then the PDF generation process would start to stall if there are many HTMLs text to be added to one document and disrupt the production workflow.

There is no error raised but the PDF generation process appears to never stop. The memory consumption and CPU utilization were off the charts when the stalling occurs. Our current resolution is to either roll back the version and use “AddHTMLToParagraph” or continue with the latest version and “AddHTMLToParagraphWithTable” but have to restart the application whenever the stalling happens and then it works as expected.

Attached is the code snippet we are currently using for generating the PDF documents. Could you please review it and suggest areas for improvement? The stalling is not easily replicated in a local environment, but intermitently occurs on the server. We are running this on AWS EC2 with Windows Server 2019 and the app is using ASP.NET Framework 4.7.

Your help in resolving this issue would be greatly appreciated.

Besides, why is the header set to bold by default and not able to change to regular in the latest version? Please also take a look at the “AddHeader” function and suggest how we can change it.

protected virtual HtmlFragment GetHTMLFragment(string text)
{
    StringBuilder htmlText = new StringBuilder("<div style=\"font-family: Arial;\">");
    htmlText.Append(text);
    htmlText.Append("</div>");
    HtmlFragment fragment1 = new HtmlFragment(htmlText.ToString());
    fragment1.HorizontalAlignment = HorizontalAlignment.FullJustify;
    return fragment1;
}

protected void AddHTMLToParagraph(StringBuilder qtext, Page sectionFB, int bottomMargin = 5, int leftMargin = 0, int rightMargin = 0, int topMargin = 0)
{
    string text = qtext.ToString();
    if (!string.IsNullOrEmpty(text))
    {
        var questionFragment = GetHTMLFragment(text);
        questionFragment.IsKeptWithNext = true;
        questionFragment.Margin.Bottom = bottomMargin;
        questionFragment.Margin.Left = leftMargin;
        questionFragment.Margin.Right = rightMargin;
        questionFragment.Margin.Top = topMargin;
        sectionFB.Paragraphs.Add(questionFragment);
    }
}

protected Cell GetHtmlCell(HtmlFragment frag)
{
    Cell cell = new Cell();
    cell.Alignment = HorizontalAlignment.Center;
    cell.VerticalAlignment = VerticalAlignment.Top;
    cell.Paragraphs.Add(frag);
    return cell;
}

protected void AddHTMLToParagraphWithTable(StringBuilder qtext, Page sectionFB, int bottomMargin = 5, int leftMargin = 0, int rightMargin = 0, int topMargin = 0)
{
    string text = qtext.ToString();
    if (!string.IsNullOrEmpty(text))
    {
        Table table = new Table();
        table.IsKeptWithNext = true;
        string colWidth = "100%";
        switch (leftMargin)
        {
            case 20:
            case 30: colWidth = "90%"; break;
            case 40: colWidth = "80%"; break;
            case 60: colWidth = "70%"; break;
            default: break;
        }
        table.ColumnWidths = colWidth;
        table.Alignment = HorizontalAlignment.Left;
        table.Border = new BorderInfo(BorderSide.None);
        table.DefaultCellBorder = new BorderInfo(BorderSide.None);
        table.DefaultCellPadding = new MarginInfo(0f, 0f, 0f, 0f);
        table.DefaultCellTextState.Font = pageFont;
        table.DefaultCellTextState.FontSize = pageFontSize;
        table.Margin.Left = leftMargin;
        table.Margin.Right = rightMargin;
        table.Margin.Top = topMargin;
        table.Margin.Bottom = bottomMargin;
        Row row = table.Rows.Add();
        row.Cells.Add(GetHtmlCell(GetHTMLFragment(text)));
        sectionFB.Paragraphs.Add(table);
    }
}

protected HeaderFooter AddHeader()
{
    HeaderFooter header = new HeaderFooter();
    TextFragment fragment1 = new TextFragment("©" + DateTime.Now.Year + " Company. All rights reserved.");
    fragment1.TextState.Font = pageFont;
    fragment1.TextState.FontSize = pageHeaderFontSize;
    fragment1.TextState.FontStyle = FontStyles.Regular;
    fragment1.HorizontalAlignment = HorizontalAlignment.Left;
    fragment1.Margin.Top = 5;
    fragment1.Margin.Left = -40;

    TextFragment fragment2 = new TextFragment("Company Name");
    fragment2.TextState.Font = pageFont;
    fragment2.TextState.FontSize = pageHeaderFontSize;
    fragment2.TextState.FontStyle = FontStyles.Regular;
    fragment2.HorizontalAlignment = HorizontalAlignment.Center;
    fragment2.Margin.Top = -8;

    header.Paragraphs.Add(fragment1);
    header.Paragraphs.Add(fragment2);
    return header;
}

@permayshwar

Can you please share the main program or method as well in which you are using these methods to generate the PDF document? We will test the scenario in our environment and address it accordingly.

Sure, here is the Solution zip file. Please update the string value of “outputFilePath” in the Program.cs before running. And the stalling issue is not easily replicated by one time build and run since it only occurs from time to time on a cloud server. And the stalling starts when the code reaches “Document.Save()” call.

@permayshwar

And the stalling issue is not easily replicated by one time build and run since it only occurs from time to time on a cloud server

We tested in our environment were not able to replicate the issue. However, we have generated following investigation ticket in our issue management system to further analyze it and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-58890

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

PS: We have removed your .zip attachment from your above reply as it had your license file. Please do not ever share your license file unless we explicitly request for it via secure channel.