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;
}