Slowness on doc.Save

Hi,
We recently purchased Aspose.Total.
I am trying to convert HTML to PDF (sample html attached, in real scenario it will contain two more images.

To create such html I using HTML agility Pack and based on certain rules I apply different images. That piece of code takes only 30 secs for creating 1000 html like the one attached. My requirement is to have five such divs on one page of the pdf.
I am using following code to convert and ultimately spit the file out to the user:

public async Task<byte[]> CreatePdfFileFromHtmlString(List<string> validHtmlStrings, PageInfo pageInfo = null,int? numberOfStringsPerPage = null, HorizontalAlignment stringAlignment =HorizontalAlignment.Left, bool applyBreakAfterString = false)
            {
                Aspose.Pdf.License license = new License();
                license.SetLicense("Aspose.Total.lic");
                var memory = new MemoryStream();
                 var doc = new Document();
                doc.Pages.Add();
                if (pageInfo != null)
                {
                    doc.PageInfo = pageInfo;
                }
                var stringPerPageCount = 1;
                var pageIndex = 1; // pageindex in Aspose is 1-n based.
                for (int i = 0; i < validHtmlStrings.Count; i++)
                {
                    //Start counting how many strings went on the page:
                    HtmlFragment htmlFragment = new HtmlFragment(validHtmlStrings[i]);
                    if (applyBreakAfterString)
                    {
                        htmlFragment.Margin.Bottom = 2;

                    }
                    doc.Pages[pageIndex].Paragraphs.Add(htmlFragment);
                    doc.Pages[pageIndex].Paragraphs[stringPerPageCount -1].HorizontalAlignment = stringAlignment;

                    //Reset the counter, increase the page index and start the new page if count reached:
                    if (stringPerPageCount == numberOfStringsPerPage)
                    {
                        pageIndex++;
                        stringPerPageCount = 1;
                        doc.Pages.Add();
                        continue;
                    }

                    stringPerPageCount++;
                }
                doc.Save(memory,SaveFormat.Pdf);
                
        return memory.ToArray();
       
    }

In this function when it comes to doc.Save part, it takes a hell lot of time there.
I read some of the similar issues here on the forum and the suggestions like using word which might be faster. I don’t have that option so I am looking for some guidance as to how I can improve performance of this function.

  • Will it help if I keep a blank pdf handy and then merge the pages into that? will that be faster?
  • Is there a faster way to do this with Aspose.Html may be or any other since we have Aspose.Total?

I am using 19.3 version.

Please guideBlackDigits.zip (32.2 KB)

@sanketlunyal2

Thank you for contacting support.

Would you please share how are you calculating consumed time. For example, if you are checking it with Stopwatch class then which part of code exists between Start and Stop method. Moreover, please also mention your environment details and share the part of code that specifies values for parameters while calling CreatePdfFileFromHtmlString method, so that we may replicate the scenario exactly and investigate further to help you out.

I have an MVC project and this is how I am calling the above function:

public async Task<IActionResult> test()
        {
            var generatedHtmlStrings = new List<string>();
            var cd = new System.Net.Mime.ContentDisposition
            {
                FileName = "Labels.pdf",
                Inline = false,
            };
                    
            PageInfo pageSettings = new PageInfo()
            {
                Width = 612,
                Height = 792,
                Margin = new MarginInfo()
                {
                    Left = 0,
                    Right = 0,
                    Top =5,
                    Bottom = 5
                }
                       
            };
            Response.Headers.Add("Content-Disposition",cd.ToString());
            //return File(_printingService.PrintUsingDinkToPDF(generatedHtmlStrings.FirstOrDefault().Value), "application/pdf");
            return File(await _printingService.CreatePdfFileFromHtmlString(generatedHtmlStrings.ToList(), pageSettings, 5, HorizontalAlignment.Center, true), "application/pdf");
        }

You can clone the html i provided and fill the generatedHtmlStrings list upto may be 100 strings and then try. I was testing the various code parts and if i start the stopwatch before and after doc.Save, thats the one that takes most amount of time.
I am testing in my developer machine:
i-7
16gb RAM.

My application is an IIS hosted .Net Core web application.
For 100 random strings like the one attached, time taken by doc.Save is 390479 milliseconds while debugging, which is not acceptable in my requirement. non-debug mode might be faster but I don’t suppose more than a few seconds.
A 20 page document taking more than 6 minutes at Save doesn’t seem acceptable for any requirement.
Is there any existing benchmark for Aspose.pdf v19 ?

@sanketlunyal2

Thank you for elaborating it further.

We have logged an investigation ticket to thoroughly address your performance concerns. A ticket with ID PDFNET-46231 has been logged in our issue management system for further investigation and resolution. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

We are sorry for the inconvenience.

Hi @Farhan.Raza ,
Just wanted to follow up. Is there a fix released for this yet or any work around for that I can use?

Thanks

@sanketlunyal2

Thank you for getting back to us.

Please note that the issue is currently pending as it has been logged under free support model and will be investigated on first come first serve basis. Therefore, it may take few more months to resolve. As soon as we have some definite updates or ETA regarding ticket resolution, we will let you know.

However, we also offer Paid Support, where issues are used to be investigated with higher priority. In case your reported issue is a blocker, you may please consider subscribing for Paid Support. For further information, please visit Paid Support FAQs.