rdvojmoc / DinkToPdf

C# .NET Core wrapper for wkhtmltopdf library that uses Webkit engine to convert HTML pages to PDF.
MIT License
1.08k stars 415 forks source link

First Call from .Net core 2 Web API works, next one hangs #21

Closed ghost closed 6 years ago

ghost commented 6 years ago

I'm using this on Windows 10 with a .Net core 2 web api.

The first call works fine, but subsequent calls hang. Any ideas?

private byte[] GeneratePDFFromHtml(string html)
        {
            var converter = new SynchronizedConverter(new PdfTools());

            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode = ColorMode.Color,
                    Orientation = Orientation.Landscape,
                    PaperSize = PaperKind.A4Plus
                },
                Objects =
                {
                    new ObjectSettings()
                    {
                        PagesCount = true,
                        HtmlContent = html,
                        WebSettings =
                        {
                            DefaultEncoding = "utf-8"
                        },
                        HeaderSettings =
                        {
                            FontSize = 9,
                            Right = "Page [page] of [toPage]",
                            Line = true,
                            Spacing = 2.812
                        }
                    }
                }
            };

            var bytes = converter.Convert(doc);

            return bytes;
        }
rdvojmoc commented 6 years ago

You must declare SynchronizedConverter as singleton. Example with DI.

ghost commented 6 years ago

Thanks for the quick reply! Did the DI thing and it's working now, thx :)

You might want to reply to maztt on this page: https://stackoverflow.com/questions/39364687/export-html-to-pdf-in-asp-net-core

I don't have enough reputation.

pritampashtev2 commented 4 years ago

this solution didn't worked for me. I have declared singleton but still it stops at line var bytes = converter.Convert(doc); Please suggest.

Alexander-Lazarov88 commented 4 years ago

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.

hubert17 commented 4 years ago
// startup.cs
services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));

// service class
private readonly IConverter _converter;

public PdfService(IConverter converter)
{
    _converter = converter;
}

^ That fixed my problem. Thanks guys!