HakanL / WkHtmlToPdf-DotNet

C# .NET Core wrapper for wkhtmltopdf library that uses Webkit engine to convert HTML pages to PDF.
GNU Lesser General Public License v3.0
366 stars 66 forks source link

System.AccessViolationException' occurred in WkHtmlToPdfDotNet.dll #96

Closed crmmvio closed 1 year ago

crmmvio commented 1 year ago

When I try to generate a pdf twice in a row the following error occurs:

Erro An unhandled exception of type 'System.AccessViolationException' occurred in WkHtmlToPdfDotNet.dll: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

` var htmlContent = await this.RenderViewAsync("PrintCoverSheet", viewModel, false);

        using (var converter = new SynchronizedConverter(new PdfTools()))
        {
            converter.PhaseChanged += Converter_PhaseChanged;
            converter.ProgressChanged += Converter_ProgressChanged;
            converter.Finished += Converter_Finished;
            converter.Warning += Converter_Warning;
            converter.Error += Converter_Error;

            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings = {
                    PaperSize = PaperKind.A4,
                    ColorMode = ColorMode.Color,
                    Orientation = Orientation.Portrait,
                    Margins = new MarginSettings(5,5,5,5) {Unit = Unit.Millimeters}
                },

                Objects = {
                    new ObjectSettings()
                    {
                        WebSettings = { DefaultEncoding = "utf-8" },
                        UseExternalLinks = false,
                        UseLocalLinks = true,
                        IncludeInOutline = true,
                        Page = Url.AbsoluteAction("GeraFolhaRosto", "FolhaRosto", new{id= viewModel.Id})
                    }
                }
            };

            dataPdf = converter.Convert(doc);
        }

        Response.Headers.Add("content-disposition", $"attachment; filename={filename}");
        return File(dataPdf, "application/pdf", filename);

` Erro-WkHtmlToPdfDotNet

HakanL commented 1 year ago

You need to only hold a singleton of the SynchronizedConverter.

crmmvio commented 1 year ago

In the Startup file already has

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

HakanL commented 1 year ago

Then you should use that, instead of creating a new instance: using (var converter = new SynchronizedConverter(new PdfTools()))

crmmvio commented 1 year ago

see above my code is exactly like this

HakanL commented 1 year ago

Yes, that's what's wrong, you should NOT create a new instance, instead you should use the singleton.

crmmvio commented 1 year ago

You would have an example of correct use, this one I got from the git documentation

HakanL commented 1 year ago

We do, it's right here: https://github.com/HakanL/WkHtmlToPdf-DotNet/blob/master/src/TestWebServer/Controllers/ConvertController.cs

crmmvio commented 1 year ago

ok now i understand, use by IOC, tks!!