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
372 stars 67 forks source link

System.AccessViolationException #78

Closed saumyeN closed 2 years ago

saumyeN commented 2 years ago

When I was using .NET Core 3.1 this message keeps popping up. Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Even in the initial life cycle of the application. Rarely some times it success and in the second iteration it fails giving out the error.

The code:

var converter = new SynchronizedConverter(new PdfTools()); var doc = new HtmlToPdfDocument() { GlobalSettings = { ColorMode = ColorMode.Color, Orientation = Orientation.Landscape, PaperSize = PaperKind.A3, Margins = {Top = 0, Right = 0, Left = 0, Bottom = 0, Unit = Unit.Centimeters } }, Objects = { new ObjectSettings() { PagesCount = true, HtmlContent = result, WebSettings = { DefaultEncoding = "utf-8" }, FooterSettings = { FontSize = 6, Right = "Page [page] of [toPage]", Spacing = 2.812 }, UseExternalLinks = true, UseLocalLinks = true } } }; var document = converter.Convert(doc);

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

HakanL commented 2 years ago

You should keep the converter static/global in your solution, not instantiate it for each call.

saumyeN commented 2 years ago

Hi Thanks for quick reply. As you said I made following class which solved my problem. Sharing here if anyone finds this helpfull.

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Text;
using WkHtmlToPdfDotNet;

namespace ****
{
    public static class SomeClass
    {
        private static SynchronizedConverter converter = null;
        private static readonly object threadLock = new object();

        public static SynchronizedConverter Instance()
        {

            lock (threadLock)
            {
                if (converter == null)
                    converter = new SynchronizedConverter(new PdfTools());
            }
            return converter;
        }

        public static byte[] GeneratePdf(string htmlResult)
        {
            Instance();

            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings = {
                    ColorMode = ColorMode.Color,
                    Orientation = Orientation.Landscape,
                    PaperSize = PaperKind.A3,
                    Margins = { Top = 0, Right = 0, Left = 0, Unit = Unit.Centimeters }
                },
                Objects = {
                    new ObjectSettings() {
                        PagesCount = true,
                        HtmlContent = htmlResult,
                        WebSettings = { DefaultEncoding = "utf-8" },
                        FooterSettings = { FontSize = 9, Right = "Page [page] of [toPage]", Spacing = 2.812 },
                        UseExternalLinks = true,
                        UseLocalLinks = true
                    }
                }
            };

            byte[] pdf = converter.Convert(doc);
            return pdf;
        }
    }
}

Called as following.

byte[] pdf = SomeClass.GeneratePdf(htmlString);