WkHtmlToXSharp - System.AccessViolationException

I use the WkHtmlToXSharp wrapper library in my project to create a PDF file from HTML.

I used this library many times on different computers, and suddenly I came across the following problem:

System.AccessViolationException: Attempted to read or write protected memory. This often indicates that another memory is corrupted. in WkHtmlToXSharp.WkHtmlToPdfConverter.wkhtmltopdf_convert (IntPtr converter) in WkHtmlToXSharp.WkHtmlToPdfConverter.Convert (String inputHtml) in WkHtmlToConTonTonTonTonTonFtftTtPtFTPtft Threading.DelegateQueue.EndInvoke (result of IAsyncResult) in Sanford.Threading.DelegateQueue.Invoke (Delegate method, Object [] args) on WkHtmlToXSharp.MultiplexingConverter.Convert ()

This is apparently a common problem with this library (I found some reviews on the Internet about this, but there were no fixes). By the way, in my case this happens somewhat randomly. I have not experienced this problem in other dev machines. I wonder if anyone has a fix. I also wonder if this is a problem with the wrapper library, if with the WkHtmlToPDF library itself.

Any suggestion? I am also open to using another converter if it is free and stable, and, if possible, does not spawn a new process. It should work correctly and stably in all versions of Windows and perform a decent conversion of work (the HTML that needs to be converted is fixed - contains several photos and tables and basic CSS).

+4
source share
1 answer

I would suggest an alternative route: just use wkhtmltopdf.exe directly, creating your own shell. They are not very complicated if you control the input and then know exactly how to update it and how the parameters work. I have never encountered this problem when using wkhtmltopdf directly (on Win7, Win server 2008 r2, Ubuntu and CentOS). However, they perform an autopsy process for each transformation.

As an example, you can check out the Derp class in another answer regarding wkhtmltopdf. Or try something like the unverified code below (your true code will be more complex, it's just a demo / POC).

var pi = new ProcessStartInfo(@"c:\wkhtmltopdf\wkhtmltopdf.exe"); pi.CreateNoWindow = true; pi.UseShellExecute = false; pi.WorkingDirectory = @"c:\wkhtmltopdf\"; pi.Arguments = "http://www.google.com gogl.pdf"; using (var process = Process.Start(pi)) { process.WaitForExit(99999); Debug.WriteLine(process.ExitCode); } 
+3
source

Source: https://habr.com/ru/post/892928/


All Articles