Coder Social home page Coder Social logo

Comments (7)

evenlee avatar evenlee commented on July 19, 2024 1

sorry, my fault, I researched my code, seems there is memory leaking.

I changed code from

        var converter = new SynchronizedConverter(new PdfTools());
        var bytes = converter.Convert(doc);
        var provider = new FileExtensionContentTypeProvider();
        var fileName = $"Report_{DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss")}.pdf";
        var contentType = string.Empty;
        if (!provider.TryGetContentType(fileName, out contentType))
        {
            contentType = "application/octet-stream";
        }
        var stream = new MemoryStream(bytes);
        return new FileStreamResult(stream, contentType)
        {
            FileDownloadName = fileName
        };

to

        using (var converter = new SynchronizedConverter(new PdfTools()))
        {
            var bytes = converter.Convert(doc);
            var provider = new FileExtensionContentTypeProvider();
            var fileName = $"Report_{DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss")}.pdf";
            var contentType = string.Empty;
            if (!provider.TryGetContentType(fileName, out contentType))
            {
                contentType = "application/octet-stream";
            }
            var stream = new MemoryStream(bytes);
            return new FileStreamResult(stream, contentType)
            {
                FileDownloadName = fileName
            };
        }

the issue has been fixed.

from wkhtmltopdf-dotnet.

evenlee avatar evenlee commented on July 19, 2024 1

Thanks for your advice @HakanL, I changed it to global, and the cpu load is improved a bit, and works more smoothly.

from wkhtmltopdf-dotnet.

HakanL avatar HakanL commented on July 19, 2024 1

It will be a lot slower if you instantiate the converter in each call. So the recommendation is to have it globally like you posted above. It should be a singleton as you should only have one single instance, the native library is not multi-threaded.

from wkhtmltopdf-dotnet.

HakanL avatar HakanL commented on July 19, 2024

You should have the converter global in your project, you shouldn't instantiate it for each request.

from wkhtmltopdf-dotnet.

wapenshaw avatar wapenshaw commented on July 19, 2024

@HakanL @evenlee Even I am having a similar issue as evenlee, how ever this happens on an azure linux webapp and randonly the CPU usage hits 100 and stays there. Restarting the web app is the only issue .

var converter = new SynchronizedConverter(new PdfTools());
				var doc = new HtmlToPdfDocument
				{
					GlobalSettings =
					{
						ColorMode = ColorMode.Color,
						Orientation = Orientation.Portrait,
						PaperSize = PaperKind.A4,
					},
					Objects =
					{
						new ObjectSettings
						{
							PagesCount = true,
							HtmlContent = outputAsString,
							WebSettings = {DefaultEncoding = "utf-8"},
							HeaderSettings = {FontSize = 9, Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812}
						}
					}
				};
				
					var pdfBytes = converter.Convert(doc);
					var pdfStream = new MemoryStream(pdfBytes);
					var pdfFileResult = new FileStreamResult(pdfStream, "application/pdf");
					var pdfName = $"HMRC_MTD_VAT_RETURN_{periodKey}.pdf";
					pdfFileResult.FileDownloadName = pdfName;

					return pdfFileResult;

This is my code, I will try using the using keyword to properly use the IDisposable instance and update the situation. Meanwhile can you explain what You should have the converter global in your project meant?

from wkhtmltopdf-dotnet.

HakanL avatar HakanL commented on July 19, 2024

Having the converter global means you don't instantiate it on each call, but have one shared instance throughout your application.

from wkhtmltopdf-dotnet.

wapenshaw avatar wapenshaw commented on July 19, 2024

@HakanL Thank you, in my case thats the only place I use the PDF converter, adding the using statement fixed my issue. So thank you for that too! but I understand having global instance, I have implemented something like this in my project.

https://github.com/JakeSiewJK64/SWE20001/blob/e4bd84f7de659340fc7ee85aa1a0034687613868/src/Infrastructure/DependencyInjection.cs#L69

https://github.com/JakeSiewJK64/SWE20001/blob/e4bd84f7de659340fc7ee85aa1a0034687613868/src/Infrastructure/PDF/PDFConverterHelper.cs#L7

services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
services.AddTransient<IPdfGenerator, PdfGenerator>();

and I am using the PdfGenerator across my application now. Do you suggest Injecting the PDF Service as a Transient one or Scoped one?

from wkhtmltopdf-dotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.