The PDF Viewer library allows you to download a PDF document rendered in the viewer while refreshing the window or clicking the close window using the onbeforeunload
event.
Step 1: Follow the steps provided in the link to create a simple PDF Viewer sample.
Step 2: Add the following code sample to download a PDF document while refreshing or closing the window.
//The event triggers while closing or refreshing the window.
window.onbeforeunload = function(e){
var message = "Do you want to close the page?"
e = e || window.event;
//For IE and Firefox.
if (e) {
e.returnValue = message;
//Method to download the document.
viewer.download();
}
}