今天早上我在 AbcPdf9 上遇到了类似的问题。我添加了代码来测试引擎类型并且 GECKO 工作,然后我将它换回 MSHTML,它仍然工作。所以这是一个暂时的问题。
这是您指定引擎类型的方式:
using (var document = new Doc())
{
document.HtmlOptions.Engine = EngineType.Gecko;
...
...
}
此代码调用将 html 转换为 PDF 的方法,但如有必要,将调用它两次,因为它只会失败一次:
try
{
return GeneratePdfFromHtml(html, width, EngineType.MSHtml);
}
catch (Exception ex)
{
/* detect this known issue, swapping the rendering engine once seems to fix it */
if (ex.Message.ToUpper().Contains("BLANK"))
{
return GeneratePdfFromHtml(html, width, EngineType.Gecko);
}
throw;
}
然后您可以向进行转换的方法添加一个参数:
public byte[] GeneratePdfFromHtml(string html, int width, EngineType engineType)
{
if (string.IsNullOrWhiteSpace(html)) throw new ArgumentNullException("html");
if (width < 100) throw new ArgumentOutOfRangeException("width");
try
{
using (var document = new Doc())
{
document.HtmlOptions.Engine = engineType;
...
...
如果您有建议或不同的解决方案,请发表评论。