We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

HtmlToPdfResult.Render and Memory leak ?

Hey guys, I have to use your pdf componant to convert some html to PDF.

Everything works great when I convert less of 100 or 200 files in the same action. But in my project I have to convert more of 100 000 and quickly I saw something weird.

for example I have a small application where I just want to convert 250 html in 250 pdf. If I comment the render line, no problem at all, my IIS or a Webserver(from visual) manage all of my memory very well, and I stay between 60MB and 80MB.
But if I uncomment the render line, my memory increase to reach 250MB and never go down even if my application is switch off.
And to get my memory back, I have to kill my IIS process.

For exanple to convert 2000 files I need 1G of memory. so I let you imagine how many I need to convert 300 000 files.

this is the code :

PdfDocument doc = new PdfDocument();
PdfPage page = doc.Pages.Add();
SizeF pageSize = page.GetClientSize();

PdfUnitConvertor convertor = new PdfUnitConvertor();
float width = convertor.ConvertToPixels(page.GetClientSize().Width, PdfGraphicsUnit.Point);
float height = convertor.ConvertToPixels(page.GetClientSize().Height, PdfGraphicsUnit.Point);


html = new HtmlConverter();

HtmlToPdfResult result = html.Convert(pathOrigine, ImageType.Metafile, (int)width, -1, AspectRatio.KeepWidth);
PdfMetafile mf = new PdfMetafile(result.RenderedImage as System.Drawing.Imaging.Metafile);
mf.Quality = 100;

doc.PageSettings.Height = result.RenderedImage.Size.Height;

PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();
format.Break = PdfLayoutBreakType.FitPage;
format.Layout = PdfLayoutType.Paginate;
format.SplitTextLines = true;
format.SplitImages = true;

result.Render(page, format);
//mf.Draw(page, new PointF(0, 0), format);

mf.Dispose();
html.Dispose();

doc.Save(Server.MapPath("") + "/PDF/" + pathDestination);
doc.Close(true);

Thank for your quick answer


2 Replies

GM Geetha M Syncfusion Team March 24, 2009 01:50 PM UTC

Hi Patrice,

Thank you for your interest in Syncfusion products.

I am working on creating a test sample and will get back to you tomorrow.

Regards,
Geetha



GM Geetha M Syncfusion Team March 26, 2009 12:55 PM UTC

Hi Patrice,

Thank you for your patience.

I tried creating the sample. I was able to see that Essential PDF takes less than 50 MB to create 250 files using the render code itself. I used the below code:

PdfDocument doc = new PdfDocument();
PdfPage page = doc.Pages.Add();
SizeF pageSize = page.GetClientSize();

PdfMetafile mf;
HtmlToPdfResult result;

PdfUnitConvertor convertor = new PdfUnitConvertor();
float width = convertor.ConvertToPixels(page.GetClientSize().Width, PdfGraphicsUnit.Point);
float height = convertor.ConvertToPixels(page.GetClientSize().Height, PdfGraphicsUnit.Point);

PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();
format.Break = PdfLayoutBreakType.FitPage;
format.Layout = PdfLayoutType.Paginate;
format.SplitTextLines = true;
format.SplitImages = true;

// get each file or check for the count here
foreach (string file in files)
{
doc = new PdfDocument();
page = doc.Pages.Add();

infile = new FileInfo(file);
html = new HtmlConverter();

result = html.Convert(pathOrigine, ImageType.Metafile, (int)width, -1, AspectRatio.KeepWidth);

mf = new PdfMetafile(result.RenderedImage as System.Drawing.Imaging.Metafile);
mf.Quality = 100;

doc.PageSettings.Height = result.RenderedImage.Size.Height;

result.Render(page, format);
//mf.Draw(page, new PointF(0, 0), format);

mf.Dispose();
html.Dispose();

doc.Save(Server.MapPath("") + "/PDF/" + pathDestination);
doc.Close(true);
}

I would suggest you to use the above code or a similar one, because, whenever you create a new page, the ClientSize is not going to be changed. Also, the format can be defined outside the loop as both width and format are not going to change as you are creating a new PDF for every HTML file. Also, it would be helpful if you send us your HTML file so that I could recreate the problem and find the cause of the same.

Please let me know if you have any difficulties.

Regards,
Geetha


Loader.
Live Chat Icon For mobile
Up arrow icon