Welcome Guest Search | Active Topics | Sign In | Register

EO.Pdf.HtmlToPdf.ConvertUrl Exception: 401:The requested resource requires user authentication Options
JohnL
Posted: Wednesday, August 24, 2011 4:35:27 PM
Rank: Newbie
Groups: Member

Joined: 8/24/2011
Posts: 3
Url converts fine on my development machine. When the site is moved to a test server, the 401 error occurs. Site on test server is using ASP.NET Impersonation and Windows Authentication.

Code to convert the url is:

PdfDocument doc = new PdfDocument();
try
{
// Give the pdf 0.25 inch margins.
HtmlToPdf.Options.OutputArea = new RectangleF(0.25f, 0.25f, 8.0f, 10.5f);
HtmlToPdf.ConvertUrl(_printUrl, doc); // Exception occurs here.
}
catch (Exception ex)
{
//Output the exception message to the PDF file in case the conversion fails.
doc = new PdfDocument();
string error = string.Format("<p>Exception:</p><pre>{0}</pre>", HttpUtility.HtmlEncode(ex.ToString()));
HtmlToPdf.ConvertHtml(error, doc);
}

Thanks.

JohnL
eo_support
Posted: Wednesday, August 24, 2011 4:50:02 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

That can be normal. The user under which the converter runs on your development machine may have sufficient rights, while the user under which the converter runs on the server may not.

The easiest way to work around this issue is to use ASPXToPDF instead of ConvertUrl. ASPXToPDF is a free control that is built on top of the EO.Pdf. The key difference between ASPXToPDF and ConvertUrl is, ASPXToPDF lives "inside" the page, where ConvertUrl pulls the page from "outside". Because the control is already "inside" the page, it should not hit any security issue.

You can find more information about ASPXToPDF here:

http://doc.essentialobjects.com/library/4/web/aspxtopdf.aspx

Hope this helps. Please feel free to let us know if you have any more questions.

Thanks!
JohnL
Posted: Thursday, August 25, 2011 3:19:07 PM
Rank: Newbie
Groups: Member

Joined: 8/24/2011
Posts: 3
Actually, I wound up using the .NET WebClient and setting it to use default credentials to get around the 401 issue and then ConvertHtml instead of ConvertUrl:

WebClient wc = new WebClient();
wc.UseDefaultCredentials = true;
string resultStr = wc.DownloadString(_printUrl);
// Set the base url.
string[] words = Request.Url.AbsoluteUri.Replace("Downloader", "~Downloader").Split('~');
_baseUrl = words[0];
HtmlToPdf.Options.BaseUrl = _baseUrl;
// Give the pdf 0.25 inch margins.
HtmlToPdf.Options.OutputArea = new RectangleF(0.25f, 0.25f, 8.0f, 10.5f);
//HtmlToPdf.ConvertUrl(_printUrl, doc);
HtmlToPdf.ConvertHtml(resultStr, doc);

However, my issue now is that while this produces the .pdf I'm looking for locally, when I move it to the test server, the _baseUrl is not recognized and I lose my images and stylesheet styling.
eo_support
Posted: Thursday, August 25, 2011 3:23:08 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

There is no reason BaseUrl would not work. It might still be permission issue because the converter will still hit 401 when it tries to pull the images. So you might want to give the images anonymous access.

Thanks
JohnL
Posted: Thursday, August 25, 2011 4:12:28 PM
Rank: Newbie
Groups: Member

Joined: 8/24/2011
Posts: 3
I enabled anonymous authentication on the css and image folders and that did the trick.

Thanks for your help.

For a future release, it'd be really nice if HtmlToPdf.ConvertUrl() was overloaded to allow the System.Net.CredentialCache.DefaultCredentials to be used.
eo_support
Posted: Thursday, August 25, 2011 5:34:23 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Glad to hear that it's working for you. Your suggestion makes perfect sense. Thank you very much for your feedback!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.