Persistent High CPU Usage after HTML to PDF Conversion

Problem Description

After generating a few documents, the CPU usage remains persistently high (~30%), despite no other routines running in the application. To investigate the cause, we used dotTrace and captured memory dumps to identify the routine responsible for the CPU consumption. The data indicates that the Aspose Library thread is the source of the persistent High CPU usage.

Our expected behavior is that the CPU usage will return to normal after the conversion is completed.

Library Name: Aspose.PDF.Drawing - Version: 24.2 (Also tested with version 24.10)

Code Block

public static byte[] ConvertHtmlToPdfWithPageSize(string html, float leftMargin, float rightMargin,
    float topMargin, float bottomMargin, float footerHeight, float pageWidth, float pageHeight, bool paginator, string pageFooter,
    HorizontalPlacement pageNumberAlignment, string fileName)
{
    byte[] results = new byte[] { };
    pageWidth = new Unit(UnitMetric.Inch, pageWidth).ValueInPoint;
    pageHeight = new Unit(UnitMetric.Inch, pageHeight).ValueInPoint;
    leftMargin = new Unit(UnitMetric.Inch, leftMargin).ValueInPoint;
    topMargin = new Unit(UnitMetric.Inch, topMargin).ValueInPoint;
    rightMargin = new Unit(UnitMetric.Inch, rightMargin).ValueInPoint;
    bottomMargin = new Unit(UnitMetric.Inch, bottomMargin).ValueInPoint;
    footerHeight = new Unit(UnitMetric.Inch, footerHeight).ValueInPoint;
    
    DocumentManagementMethods.SetLicensePdf();
    
    using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(html ?? "")))
    { 
        using (Document document = new Document(stream, new HtmlLoadOptions() 
               {
                 PageInfo = new PageInfo()
                 {
                     Width = pageWidth,
                     Height = pageHeight,
                     Margin = new MarginInfo(leftMargin, bottomMargin, rightMargin, topMargin)
                 }
             }))
        {
            document.Flatten();
            // Add footer on all pages
            foreach (Page page in document.Pages)
            {
                using (page)
                {
                    AddFooterToPage(page, document, pageFooter, paginator, pageNumberAlignment, pageHeight, leftMargin, rightMargin, footerHeight);
                }
            }
            document.SetTitle(fileName);
            using (MemoryStream ms = new MemoryStream())
            {
                document.Save(ms, SaveFormat.Pdf);
                results = ms.GetBuffer();
            }
        }
    }
    
    //Force garbage collecting.
    GC.Collect();
    return results;
}

private static void AddFooterToPage(Page page, Document document, string pageFooter, bool paginator, HorizontalPlacement pageNumberAlignment, float pageHeight, float leftMargin, float rightMargin, float bottomMargin)
        {
            string footerContent = string.Empty;
            if (string.IsNullOrEmpty(pageFooter) == false)
            {
                footerContent = pageFooter.Replace("{page_number}", (page.Number).ToString()).Replace("{total_pages}", document?.Pages.Count.ToString());
            }
            else if (paginator)
            {
                footerContent = $"{page.Number} of {document?.Pages.Count}";
            }

            // Page Number in Footer
            TextStamp textStamp = new TextStamp(footerContent)
            {
                BottomMargin = bottomMargin,
                HorizontalAlignment = TranslateHorizontalAlignment(pageNumberAlignment),
                VerticalAlignment = VerticalAlignment.Bottom,
                LeftMargin = leftMargin,
                RightMargin = rightMargin,
            };
            page.AddStamp(textStamp);
        }

HTML that caused the problem

<!DOCTYPE html>

<html>

<head>

<title>

</title>
<style>
body {

font-family: "Inter";

}
table { width: 75%; margin-left: auto; margin-right: auto;   border-style: inset;

  border-width: 7px;}

th {text-align: center;  border-bottom: 1px solid;}

tr {text-align: center; border-right: 1px solid;}

td {text-align: center;}

h1 {text-align:center; margin: 25px;

font-size: 45px; font-weight: 600; color: #2633f7;
}

tr:nth-child(odd) {background-color: #f2f2f2;}

td:nth-child(odd) {border-right: 1px solid;}

th:nth-child(odd) {border-right: 1px solid;}

</style>
</head>
<body>
<h1>Table of Contents</h1>
<br></br>
<br></br>

<table>
<tr>

<th>#</th>

<th>Description</th>

</tr>
<tr>
<td>1</td>
<td>Physician Summary</td>
</tr><tr>
<td>2</td>
<td>Patient Summary</td>
</tr><tr>
<td>3</td>
<td>Next Best Actions</td>
</tr><tr>
<td>4</td>
<td>Notes</td>
</tr><tr>
<td>5</td>
<td>SDoH Needs & Referrals</td>
</tr><tr>
<td>6</td>
<td>Assessments</td>
</tr>
</table>
</body>
</html>

Memory Dump Threads

## Child-SP         RetAddr           Call Site
00 000000a66b67d939 00007ffeeb20464c ntdll!NtWaitForMultipleObjects+0x14
01 000000a66b67d939 00007ffed954765b KERNELBASE!WaitForMultipleObjectsEx+0xec
02 000000a66b67d939 00007ffed95474a5 coreclr+0xb765b
03 000000a66b67dbe0 00007ffed95473c0 coreclr+0xb74a5
04 000000a66b67dbe0 00007ffe829de375 coreclr+0xb73c0
05 000000a66b67dbe0 00007ffe8aff3fd6 System_Private_CoreLib!System.Threading.WaitHandle.WaitOneNoCheck+0xb5
06 000000a66b67dc90 00007ffe8ad1a21a Aspose_PDF!#=zj3emEQNJeSKvd7jTxmK7Ywe6xdKW.#=z22bSdakdwY5ApVrRPw==.#=zDEnMHg4=+0x216
07 000000a66b67dd20 00007ffe8b74c8cf Aspose_PDF!#=zj3emEQNJeSKvd7jTxmK7Ywe6xdKW.#=zDmZGC7KAx8Mt+0x17a
08 0000000000000000 00007ffe8ad26d98 Aspose_PDF!#=zNFKoF2c8Su1yzcM$ONKAutedstDZ.#=zNsFTUws=+0x10f
09 000000a66b67de10 00007ffe8b170a8d Aspose_PDF!#=zo4qXxLCil7SODppCorrJQwY=.#=zNsFTUws=+0x3f8
0a 000000a66b67de90 00007ffe8a252e7b Aspose_PDF!#=zuGcZPRO$RkrZOIZGkqiy3HU=.#=zNsFTUws=+0x2ad
0b 000000a66b67e070 00007ffe8b6fb139 Aspose_PDF!#=zZanBRRI9ZONTUhG82ZBmXHc=.Dispose+0xbb
0c 000000a66b67e070 00007ffe8b6fa00d Aspose_PDF!#=zJbgHBVnVBjCAnPT9TEIk6wgLs8at.#=zPFdqZBzmOQI9+0x1029
0d 000000a66b67e200 00007ffe8b6f9fb8 Aspose_PDF!#=zJbgHBVnVBjCAnPT9TEIk6wgLs8at.#=z6hOx_hU=+0x2d
0e 000000a66b67e200 00007ffe8b6f833d Aspose_PDF!#=zJbgHBVnVBjCAnPT9TEIk6wgLs8at.#=z6hOx_hU=+0x18
0f 000000a66b67e200 00007ffe8b6f6f28 Aspose_PDF!Aspose.Pdf.Document.#=zvUb20T0=+0x12dd
10 000000a66b67e260 00007ffe8b6f5524 Aspose_PDF!Aspose.Pdf.Document..ctor+0x1e8
11 000000a66b67e540 00007ffe8b6f4801 DecisionsFramework!DecisionsFramework.Utilities.Data.PDFUtility.ConvertHtmlToPdfWithPageSize+0x624
## Child-SP         RetAddr           Call Site
00 0000000000000000 00007ffe8a3e11de Aspose_PDF!#=zNFKoF2c8Su1yzcM$ONKAutedstDZ.#=zu01xCio=+0x1a5
01 000000a66c2bf7a0 00007ffe8b70a535 Aspose_PDF!#=zj3emEQNJeSKvd7jTxmK7Ywe6xdKW.#=zGbFLmyk=+0x39e
02 000000a66c2bf840 00007ffe7ffb8462 Aspose_PDF!#=zsmu3parI47WJN2mymRi$bm4K8pGnNhltkQ==.#=ztwdUF5P0Lzroz4u5d5ua6kE=+0x35
03 000000a66c2bf840 00007ffed95ebd43 System_Private_CoreLib!System.Threading.ExecutionContext.RunInternal+0xb2
04 000000a66c2bf870 00007ffed94bebac coreclr!coreclr_set_error_writer+0x2ee3
05 0000000000000004 00007ffed95d7ee3 coreclr+0x2ebac
06 0000000000000000 00007ffed9544aed coreclr!coreclr_execute_assembly+0x295d3
07 0000000000000000 00007ffed9544a03 coreclr+0xb4aed
08 0000000000000000 00007ffed9544b9e coreclr+0xb4a03
09 0000000000000000 00007ffeeca94cb0 coreclr+0xb4b9e
0a 0000000000000000 00007ffeed65ecdb kernel32!BaseThreadInitThunk+0x10
0b 0000000000000000 0000000000000000 ntdll!RtlUserThreadStart+0x2b

image.png (75.6 KB)
image.jpg (205.6 KB)

@jineshudec

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-58737

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.