问题描述
我在项目中使用WkHtmlToXSharp包装器库从HTML生成PDF文件.
I'm using the WkHtmlToXSharp wrapper library in my project to generate PDF file from HTML.
我在不同的PC上多次使用此库,突然,我遇到了以下问题:
I was using this library a lot of times in different PCs and, suddenly, I came across the following problem:
System.AccessViolationException:尝试读取或写入受保护的内存.这通常表明其他内存已损坏. 在WkHtmlToXSharp.WkHtmlToPdfConverter.wkhtmltopdf_convert(IntPtr转换器) 在WkHtmlToXSharp.WkHtmlToPdfConverter. Convert(字符串inputHtml) 在WkHtmlToXSharp.WkHtmlToPdfConverter.Convert() 在WkHtmlToXSharp.MultiplexingConverter.b _8() ---内部异常堆栈跟踪的结尾--- 在Sanford.Threading.DelegateQueue.EndInvoke(IAsyncResult结果) 在Sanford.Threading.DelegateQueue.Invoke(Delegate方法,Object [] args) 在WkHtmlToXSharp.MultiplexingConverter.Convert()
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at WkHtmlToXSharp.WkHtmlToPdfConverter.wkhtmltopdf_convert(IntPtr converter) at WkHtmlToXSharp.WkHtmlToPdfConverter.Convert(String inputHtml) at WkHtmlToXSharp.WkHtmlToPdfConverter.Convert() at WkHtmlToXSharp.MultiplexingConverter.b_8() --- End of inner exception stack trace --- at Sanford.Threading.DelegateQueue.EndInvoke(IAsyncResult result) at Sanford.Threading.DelegateQueue.Invoke(Delegate method, Object[] args) at WkHtmlToXSharp.MultiplexingConverter.Convert()
这似乎是该库的一个常见问题(我在网络上发现了一些有关它的反馈-但是未提供修复程序).顺便说一句,就我而言,它是随机发生的.我没有在其他开发机器上遇到此问题.我想知道是否有人对此进行了修复.我还想知道包装器库是否有问题,WkHtmlToPDF库本身是否有问题.
This seems to be a common problem with this library (I've found some feedback on the web about it - however no fix was provided). BTW, in my case it happens somewhat randomly. I was not experiencing this problem in other dev machines. I wonder if somebody has a fix for it. I also wonder if this is a problem with the wrapper library, if with the WkHtmlToPDF library itself.
有什么建议吗?我也愿意使用另一个转换器,只要它是免费且稳定的,并且在可能的情况下,也不会产生新的进程即可.它必须在所有Windows版本中都能正常且稳定地工作,并且要进行良好的转换(要转换的HTML是固定的-包含一些图片和表格以及基本的CSS).
Any suggestion? I'm also open to use another converter, as long as it is free and stable and, if possible, without spawning a new process. It must work properly and stable in all Windows versions and do a decent job converting (the HTML to be converted is fixed - contains a few pics and tables and basic CSS).
推荐答案
我建议使用另一种方法:直接使用wkhtmltopdf.exe,构建自己的包装器.如果您可以控制输入,然后确切知道如何更新输入以及选项如何工作,它们并不是很复杂.当直接使用wkhtmltopdf(在Win7,Win Server 2008 R2,Ubuntu和CentOS上)直接使用wkhtmltopdf时,我从未遇到过该问题.不过,它们会为每次转化生成程序.
I would suggest an alternate route: simply use wkhtmltopdf.exe directly, building your own wrapper. They are not very complicated if you have control of the input and then you know exactly how to update it and how the options work. I've never encountered with that problem when using wkhtmltopdf directly (on Win7, Win server 2008 r2, Ubuntu and CentOS). They do spawn process for every conversion though.
例如,请在我的另一个答案中查看 Derp类关于wkhtmltopdf.或尝试使用以下未经测试的代码(您的真实代码会更复杂,这只是一个演示/POC).
For an example, check out the Derp class in another answer of mine regarding wkhtmltopdf. Or try something like the untested code below (your true code will be more complicated, this is 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);
}
这篇关于WkHtmlToXSharp-System.AccessViolationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!