Welcome Guest Search | Active Topics | Sign In | Register

EO.pdf: Showing a loading spinner while generating the pdf Options
Gotcha A/S
Posted: Tuesday, December 3, 2019 4:02:59 AM
Rank: Newbie
Groups: Member

Joined: 2/19/2018
Posts: 7
Hi

I have an ActionLink in my view that triggers my RenderAsPDF action. This works fine, but generating the pdf takes a while, so I'd like to show a spinner while the pdf is building.

As far as I know, I have to call my controller using ajax instead, but it seems like eo.PDF does not allow that looking on the error: "Can not convert the output to PDF during an AJAX request".

I searched the forum and google for a guide on how to generate the pdf using ajax, but nothing shows up.

Can anyone help with a way to either register when the pdf is done building when clicking my ActionLink, or a way to generate the pdf using ajax?

Thanks!
eo_support
Posted: Tuesday, December 3, 2019 12:13:16 PM
Rank: Administration
Groups: Administration

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

You do not necessary need an AJAX call in order to show a spinner. You can simply attach an onclick event handler to your ActionLink and inside that handler you can display your spinner. The code would be something like this:

Code: HTML/ASPX
<%= Html.ActionLink(ActionName, ControllerName, null, new { onclick="displaySpinner();" }) %>


Here displaySpinner is a JavaScript function you would supply. For example, the simplest way to do it is to have a spinner image that is initially hidden and then display it in your function:

Code: HTML/ASPX
<img id="spinner" style="display:none" src="spinnger.gif" />


Code: JavaScript
function displaySpinner()
{
    //Clear "display:none" so it becomes visible
    document.getElementById("spinner").style.display = "";
}


Obviously you can enhance this part to have all kind of fancy stuff. You can search online and you should be able to find plenty of solutions/ideas on how to make the spinner more attractive and user friendly.

Hope this helps.

Thanks!
Gotcha A/S
Posted: Wednesday, December 4, 2019 4:30:06 PM
Rank: Newbie
Groups: Member

Joined: 2/19/2018
Posts: 7
Hi,

Thanks for the reply.

Showing the spinning is not the issue, but I need a callback in order to know when to remove it again.

I need Ajax for that, right?

Which leads me back to the initial questons: Is it possible to render the pdf with an Ajax request?

Thanks!
eo_support
Posted: Wednesday, December 4, 2019 4:45:51 PM
Rank: Administration
Groups: Administration

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

The short answer is no. You can not render a PDF file (or any other file for that matter) through AJAX. A single HTTP request can only give a single HTTP response. That response can either carry the result PDF file to the browser, or carry your AJAX result (often in JSON or XML format) to the browser. It can not carry both.

To achieve what you want to achieve, you MUST use more than one requests/responses. There are multiple ways to do so and you can pick whichever way that works best for you. The two most common methods are:

1. Use an iframe to start a secondary request. This way you can still use whatever methods (AJAX or plain JavaScript) in the main frame and use the iframe request to trigger the conversion and the corresponding response to carry the result back;

2. Use client side redirection. For example, you can use the request in the main frame to trigger the conversion, but instead of using the response to carry the result PDF file down, you can save the PDF file to a temp file and only carry down the temp file path (or just an internal ID for security reason). On the client side you can then use JavaScript to both stop the spinner and then start a second request to fetch the actual PDF file down to the browser.

As you can see, whichever method you use, the key is you need to make more than one round trip to the server.

Hope this make senses to you.

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.