2

I used "NReco.PdfGenerator.dll" to my web project (Visual studio 2012, c#), and it can export from internet url (like http://google.com.tw) succsssfully But when I change the url to internal url (out company internal system) and I got this error message:

"Cannot generate PDF: Exit with code 1 due to network error: AuthenticationRequiredError(exit code: 1)"

Here is my code:

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

Could anyone help this problem ??? Thank you so much

Vitaliy Fedorchenko
  • 8,447
  • 3
  • 37
  • 34
Tim
  • 23
  • 1
  • 6

1 Answers1

3

AuthenticationRequiredError is returned by wkhtmltopdf (internally PdfGenerator executes it in the separate process) when specified URL returns HTTP code 401 (Unauthorized).

In most cases this mean that this web page can be accessed only by authenticated users; in most web applications authentication token is passed with cookie or HTTP header.

You can pass either cookie with special wkhtmltopdf options, for example:

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

option for extra HTTP header:

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

Note that it is not possible to render URLs that require windows authentication; in this case alternative auth mechanism should be used for accessing these pages by wkhmtltopdf.

Vitaliy Fedorchenko
  • 8,447
  • 3
  • 37
  • 34