Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First Call from .Net core 2.1, subsequent request hangs #62

Open
Raghav-Gowda opened this issue Nov 19, 2018 · 11 comments
Open

First Call from .Net core 2.1, subsequent request hangs #62

Raghav-Gowda opened this issue Nov 19, 2018 · 11 comments

Comments

@Raghav-Gowda
Copy link

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.

@MagnusJohansson
Copy link

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
Copy link

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
Copy link

MagnusJohansson commented Jan 13, 2019

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 Subsequent PdfTools instances yield wrong PDF output #44 , only plain text is rendered .

What a mess this is.

@mze9412
Copy link

mze9412 commented Feb 6, 2019

It seems to work for me after switching to the newest version of wkhtmltopdf (see also this PR)
#59

@HakanL
Copy link

HakanL commented Apr 22, 2019

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.

@caldus85
Copy link

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!

@Alexander-Lazarov88
Copy link

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.

@asimon91
Copy link

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

@renatolopes
Copy link

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?

@caldus85
Copy link

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.

@zbex
Copy link

zbex commented Dec 20, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants