NReco.PdfGenerator错误:AuthenticationRequiredError

时间:2017-02-22 02:02:01

标签: c# nreco

我在我的网站项目中使用了“NReco.PdfGenerator.dll”(Visual Studio 2012,c#), 它可以从互联网网址(如http://google.com.tw)中导出 但是当我将url更改为内部url时(公司内部系统) 我收到此错误消息:

“无法生成PDF:由于网络错误退出代码1:AuthenticationRequiredError(退出代码:1)”

这是我的代码:

new NReco.PdfGenerator.HtmlToPdfConverter()。GeneratePdfFromFile(“http://xxx.xxx.xxxx”,null,AppDomain.CurrentDomain.BaseDirectory +“test.pdf”);

有谁可以帮助这个问题??? 非常感谢你

1 个答案:

答案 0 :(得分:2)

当指定的URL返回HTTP代码401(未授权)时,wkhtmltopdf(内部PdfGenerator在单独的进程中执行)返回AuthenticationRequiredError。

在大多数情况下,这意味着只有经过身份验证的用户才能访问此网页;在大多数Web应用程序中,身份验证令牌通过cookie或HTTP标头传递。

您可以使用特殊的wkhtmltopdf选项传递任一cookie,例如:

var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
htmlToPdf.CustomWkHtmlArgs = " --cookie <name> <value>";

额外HTTP标头的选项:

htmlToPdf.CustomWkHtmlArgs = " --custom-header <name> <value> ";

请注意,无法呈现需要Windows身份验证的网址;在这种情况下,应使用替代身份验证机制来通过wkhmtltopdf访问这些页面。