DinkToPdf icon indicating copy to clipboard operation
DinkToPdf copied to clipboard

First Call from .Net core 2.1, subsequent request hangs

Open Raghav-Gowda opened this issue 5 years ago • 11 comments

Hello,

I am working on ASP.NET Core v2.1. I am able to generate PDF for the first time, but subsequent request hangs. Please note, I've declare SynchronizedConverter as singleton in Startup.cs

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        services.AddDbContext<OCDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));

        services.AddMvc().AddSessionStateTempDataProvider();
        services.AddSession();

Please suggest how to solve this issue.

Raghav-Gowda avatar Nov 19 '18 16:11 Raghav-Gowda

I am having the same problem. Have you found any solution?

It can easily be reproduced in a console app as well, the second iteration in the loop below, the conversion will hang:

    static void Main(string[] args)
        {
            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode = ColorMode.Color,
                    Orientation = Orientation.Portrait,
                    PaperSize = PaperKind.A4,
                },
                Objects =
                {
                    new ObjectSettings()
                    {
                        PagesCount = true,
                        HtmlContent = @"Lorem ipsum dolor sit amet",
                        WebSettings =
                        {
                            DefaultEncoding = "utf-8"
                        },
                        HeaderSettings =
                        {
                            FontSize = 9, Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812
                        }
                    }
                }
            };

            for (int i = 0; i < 2; i++)
            {
                var converter = new SynchronizedConverter(new PdfTools());
                byte[] pdfData = converter.Convert(doc);
            }

        }

MagnusJohansson avatar Jan 11 '19 17:01 MagnusJohansson

One possible workaround can be to instantiate a new BasicConverter for each request and force the PdfTools object to dispose and unload the libraries after each request in a ASP.NET controller, like this:

[Produces(MediaTypeNames.Application.Pdf)]
public async Task<IActionResult> Index()
{
    var doc = new HtmlToPdfDocument()
    {
        GlobalSettings =
        {
            ColorMode = ColorMode.Color,
            Orientation = Orientation.Portrait,
            PaperSize = PaperKind.A4,
        },
        Objects =
        {
            new ObjectSettings()
            {
                PagesCount = true,
                HtmlContent = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In consectetur mauris eget ultrices  iaculis.",
                WebSettings = { DefaultEncoding = "utf-8" },
                HeaderSettings = { FontSize = 9, Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812 }
            }
        }
    };
            
    using (var tools = new PdfTools())
    {
        var converter = new BasicConverter(tools);
        byte[] pdfData = converter.Convert(doc);
        return File(pdfData, MediaTypeNames.Application.Pdf);
    }
}

MagnusJohansson avatar Jan 11 '19 19:01 MagnusJohansson

I spoke too soon. This is not a valid work around because;

  • If using singleton instances of PdfTools and a Converter, DinkToPdf hangs on subsequent calls
  • If instantiating a new PdfTools for every request, the first creation is ok, but any subsequent conversions looses the HTML generation #44 , only plain text is rendered .

What a mess this is.

MagnusJohansson avatar Jan 13 '19 15:01 MagnusJohansson

It seems to work for me after switching to the newest version of wkhtmltopdf (see also this PR) https://github.com/rdvojmoc/DinkToPdf/pull/59

mze9412 avatar Feb 06 '19 09:02 mze9412

I upgraded to 0.12.5 of the 64-bit Windows library and it still hangs on the second request, so PR #59 didn't help me. However, I realized I was creating multiple instances of SynchronizedConverter, once I changed to a singleton then it actually works fine.

HakanL avatar Apr 22 '19 16:04 HakanL

I upgraded to 0.12.5 of the 64-bit Windows library and it still hangs on the second request, so PR #59 didn't help me. However, I realized I was creating multiple instances of SynchronizedConverter, once I changed to a singleton then it actually works fine.

This fixed the issue for me. Thanks!

caldus85 avatar Sep 11 '19 19:09 caldus85

If someone still has this issue, can try to remove this 'var converter = new SynchronizedConverter(new PdfTools());' and just inject 'IConverter' in your service. I try and this approach is working, after all, we already register the converter, we don`t need to create an instance with 'new' keyword.

Alexander-Lazarov88 avatar Dec 11 '19 13:12 Alexander-Lazarov88

I confirm too that changing to singleton instead of multiple instances works flawlessly. Thank you!

asimon91 avatar Feb 19 '20 07:02 asimon91

When i call in the integration tests, in second test that calls convert i'm with same issue even follow the instructions and using singleton injection. Any tips?

renatolopes avatar Aug 21 '21 16:08 renatolopes

When i call in the integration tests, in second test that calls convert i'm with same issue even follow the instructions and using singleton injection. Any tips?

I would try version 0.12.5 above if you are not already. Then double check that you're calling services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));. Also note Alexander-Lazarov88's comment above about possibly using IConverter instead. Beyond that I don't have any other ideas, sorry.

caldus85 avatar Aug 21 '21 16:08 caldus85

When i call in the integration tests, in second test that calls convert i'm with same issue even follow the instructions and using singleton injection. Any tips?

Any luck with this? I experience the same issue.

zbex avatar Dec 20 '22 16:12 zbex