Welcome Guest Search | Active Topics | Sign In | Register

HtmlToPdf.ConvertUrl does not fail if the server returns an HTTP error Options
Ash
Posted: Tuesday, September 18, 2018 4:56:50 AM
Rank: Newbie
Groups: Member

Joined: 9/18/2018
Posts: 3
We have a problem where we are using EO.Pdf to auto-generate some documents. Occasionally, the URL that we are passing to ConvertUrl() will throw an error, and return Response Code 500 (Internal Server Error).

EO.Pdf does not handle these errors correctly and continues to simply generate a PDF of the error text returned by the server, which is not desired behaviour.

Is it possible to have ConvertUrl throw an exception or otherwise catch these HTTP errors so that our application can take the appropriate remedial action, rather than blindly creating a PDF from the error text?

Thanks
eo_support
Posted: Tuesday, September 18, 2018 10:53:08 AM
Rank: Administration
Groups: Administration

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

You will need to use HtmlToPdfSession object to have this level of control. See here for sample code on how to use this object:

https://www.essentialobjects.com/doc/eo.pdf.htmltopdfsession.runwebviewcallback_overload_1.aspx

You will need to make the following modifications based on the sample code:
1. The sample code uses webView.LoadRequestAndWait to load a Request. You will need to modify that to something like this:

Code: C#
//Load the Url into the WebView
NavigationTask task = webView.LoadUrl(your_url);

//Wait for the load to finish
task.WaitOne();


2. Add code to check the NavigationTask object's HttpStatusCode value and act accordingly. This value is the http status code (such as 500) returned by your server. Note this is not the same as ErrorCode. ErrorCode indicates a real error (such as server not reachable), where as HttpStatusCode indicates the web server has responded properly, even though it may not be the result you wanted.

If you want further controls, you may want to familiar yourself with the WebView class:

https://www.essentialobjects.com/doc/eo.webbrowser.webview.aspx

This is the core class of our WebBrowser product and is what EO.Pdf uses internally to load and render web pages.

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

Thanks!

Ash
Posted: Wednesday, September 19, 2018 9:12:00 AM
Rank: Newbie
Groups: Member

Joined: 9/18/2018
Posts: 3
Thank you for your response, I have got it working.

I do feel that this should be default behaviour though, I don't imagine that it is often that people would want to create a PDF of an unexpected error page.
eo_support
Posted: Wednesday, September 19, 2018 10:31:00 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,082
Thank you very much for your feedback. We will look into this and see what we can do.
Ash
Posted: Wednesday, September 26, 2018 9:20:08 AM
Rank: Newbie
Groups: Member

Joined: 9/18/2018
Posts: 3
As a followup to this, is there a way to add data to the request body? I can't seem to find an overload or method on WebView that will allow me to do this, other than LoadRequest() which takes a request, but only allows me to add form data (we need to post Json to the URL).

eo_support
Posted: Wednesday, September 26, 2018 9:34:16 AM
Rank: Administration
Groups: Administration

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

Have you tried something like this:

Code: C#
request.PostData.Add(new PostData(your_binary_post_data));

You will need to covert your JSON into a byte array first and also need to set the request's ContentType and ContentEncoding properly.

Thanks!
eo_support
Posted: Thursday, October 11, 2018 3:21:22 PM
Rank: Administration
Groups: Administration

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

This is just to let you know that we have posted a new build that added this property:

https://www.essentialobjects.com/doc/eo.pdf.htmltopdfoptions.throwonhttperror.aspx

To maintain backwards compatibility, this property is false by default. To achieve the behavior you wanted, you would need to set this property to true.

You can download the new build from our download page. Please take a look and let us know how it goes.

Thanks!


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.