Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Apr 16, 2020 2:11 pm

We are experiencing an issue with PDF generation in our .net core api that generates PDF's for download from a REACT website. When we do this we notice that after a few "PFD export" requests are made server memory usage for that api is >8GB

The code for this is ...
private Stream HtmlToPdf(string html, PageOrientation orientation, SizeF pageSize)
Code: Select all
        {
            var stream = new MemoryStream();
            Document document = new Document();
            document.LoadHTML(new StringReader(html), XHTMLValidationType.None);
            document.LastSection.PageSetup.PageSize = pageSize.IsEmpty ? PageSize.A4 : pageSize;
            document.LastSection.PageSetup.Orientation = orientation;
            document.SaveToStream(stream, Spire.Doc.FileFormat.PDF);
            stream.Seek(0, SeekOrigin.Begin);
            return stream;
        }


Example Behavior of memory use after one request
Call - https://localhost:44308/enrollment/cust ... export/pdf
Startup 115 MB
Before method LoadHtml - 293 MB
After method LoadHtml - 1.8 GB
After SaveToStream - 2.7 GB


We have tried the newest hotfix version 6.4.4

This is a production issue so our second option is switching to another PDF generator that is actually working great with only 3 lines of code being altered. We would like to resolve this with Spire if we can.

xsinc
 
Posts: 9
Joined: Fri Nov 04, 2016 6:03 pm

Fri Apr 17, 2020 5:51 am

Hello,

Thanks for your inquiry.
Regarding the "another PDF generator" you mentioned, are you referring to the plugin conversion method of our Spire.PDF? And using this method will not cause the issue of heavy memory usage, right?
Besides, I tested your code with the latest Spire.Doc Pack(hot fix) Version:8.4.2 but didn't reproduce your issue. To help us investigate your issue more accurately and quickly, please provide the following information. Thanks in advance.
1) Your input html file or html string.
2) Your server environment information, such as such as OS information (E.g. Windows Server 2012, 64bit) and Region setting (E.g. China, Chinese).

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri Apr 17, 2020 12:22 pm

Thanks for responding. The other generator is a competitor's product so I'll leave the name out, but we have not tried your plugin conversion method of Spire.PDF. Is that something you'd like us to try? Where can we find that? I'm sourcing your answers now and will reply as soon as I have them. Thank you again for your reply!

xsinc
 
Posts: 9
Joined: Fri Nov 04, 2016 6:03 pm

Fri Apr 17, 2020 12:41 pm

1A) Windows Server 2019 Datacenter 64bit English
1B) development machine is Win 10 Pro 64 Eng Core i7-4770 OS build 18363.752
2) HTML File was attached, but is now removed. I would like to send it to you directly as there is real data in it and I am uncomfortable posting it.

xsinc
 
Posts: 9
Joined: Fri Nov 04, 2016 6:03 pm

Mon Apr 20, 2020 2:27 am

Hello,

Sorry for the late reply as weekend.
Our Spire.PDF provides a plugin method to convert HTML to PDF, please refer to this tutorial: Convert HTML to PDF with New Plugin. You can try it on your side.
As for your input html file, you could send it to us( [email protected]) via email. And we promise to keep your document confidential and we will not use it for any other purpose.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Mon Apr 20, 2020 11:56 am

I emailed the file earlier this morning for you to review. We have tried the plugin and although the performance was slightly better it was less then ideal. I have someone on the team getting more specific metrics for you on the difference in performance.

xsinc
 
Posts: 9
Joined: Fri Nov 04, 2016 6:03 pm

Tue Apr 21, 2020 11:45 am

Hello,

Thanks for your sharing.
When testing your file, I didn’t encounter the “OutOfMemoryException” error. The used memory before conversion was 2.0GB, and during the conversion process, the maximum used memory is 3.6GB, the total increased is not as large as yours. Meanwhile, I noticed that your html file contains a lot of content, and the generated PDF has a total of 227 pages. I discussed with our Dev team and learned that the total 1.6GB memory usage is normal. I sent you my testing project via email, you could run it on your side and then tell us your testing result. If your project is different from mine, please provide a sample runnable project for further investigation.
Besides, there is another PS conversion method. Generally, its performance is a little better. Below is the corresponding code, you can also try it. Looking forward to your further feedback.

Code: Select all
    private static Stream HtmlToPdf(string html, PageOrientation orientation, SizeF pageSize)
    {
        var stream = new MemoryStream();
        Document document = new Document();
        document.LoadHTML(new StringReader(html), XHTMLValidationType.None);
        document.LastSection.PageSetup.PageSize = pageSize.IsEmpty ? PageSize.A4 : pageSize;
        document.LastSection.PageSetup.Orientation = orientation;
        ToPdfParameterList tpl = new ToPdfParameterList
        {
            UsePSCoversion = true
        };
        document.SaveToStream(stream, tpl);
        stream.Seek(0, SeekOrigin.Begin);
        return stream;
    }


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Tue Apr 21, 2020 1:04 pm

Thanks Rachel, I will see if we can get you a sample project setup like ours. however I did not receive an email from you nor did it get pulled in by our spam service. would you be able to post the project here or in a common location where I can download it?

I am concerned that the memory usage we have seen also grows based on the number of users also trying to export. if we use you 1.9GB example multiplied but the number of users we are hitting 8GB very quickly.

Thanks,
Ray

xsinc
 
Posts: 9
Joined: Fri Nov 04, 2016 6:03 pm

Wed Apr 22, 2020 2:07 am

Hello,

Thanks for your response.
My email may have been blocked due to restrictions on your mailbox. I uploaded my project here, please check.
http://www.e-iceblue.com/downloads/demo/21266Demo.zip

Generally, when the number of users increases, the memory usage will increase accordingly. If many html files that need to be converted contain a lot of content, I'm afraid that the memory usage may increase quickly.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.PDF