0

I'm working on an "Online Medical report download" website, i have a button called "Download" on page, when click it will redirect to 'report.php' page, end of the body I'm using window.print() method of JavaScript for printing.

its working fine for me, but if I have to download a large file, then print preview takes time to load the page.

I want to show a loading GIF image or Some message if there is a waiting time till file load for printing. so my users can get interaction.

I have tried to search a lot but not found any specific solution for this.

Kamalesh M. Talaviya
  • 1,422
  • 1
  • 12
  • 26
RAK
  • 109
  • 7
  • is the file you serve is fetched through some Ajax call ? – Aravind Anil Dec 26 '19 at 10:23
  • You can use the approach mentioned in https://stackoverflow.com/questions/18325025/how-to-detect-window-print-finish . Wherein you can show your loading icon before print event and hide it after print event. – Aravind Anil Dec 26 '19 at 10:25
  • You could capture the onload event for all the images used, and show progress at same time. – Keith Dec 26 '19 at 11:20
  • @AravindAnil no it's not ajax call, on download button click I'm redirecting to second page, end of body I'm calling window.print method – RAK Dec 26 '19 at 11:46

2 Answers2

0

You can try using onafterprint and onbeforeprint event to do this

window.onbeforeprint = function(){
    $('loader').show();
}
window.onafterprint = function() {
    $('loader').hide();
}

Ref : https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onafterprint

Aravind Anil
  • 153
  • 1
  • 9
0

If the page you want to print is dependent on an API call then you might handle the situation with a loader, if it doesn't then you might have to fake it with a setTimeOut function and time which you need to mention in the callback function should be equal to thee time taken by your page to load.

Vish
  • 142
  • 1
  • 8
  • I know about this function but problem is How i will know the time taken by my page to load so I can pass to the function. – RAK Dec 26 '19 at 15:02