Rectangle 27 0

Don't use a button, use a link and set the href attribute to the URL of your PDF file. The browser will handle the file download for you, honoring the user's browser preferences.

<a href="your/file.pdf" />

If you need the link to look like a button, you can style it using CSS. See for example this SO thread.

Edit: AFAIK, you can't reliably initialize a file download from javascript. What you can do is to open a new window/tab with your pdf URL:

window.open("http://domain.com/document.pdf",'_blank');

But the user's browser can block the new window from being created. You might want to simply generate a download link:

$('<a>Click here to download PDF</a>').attr('href', filename).appendTo(that.$el);

And have the user click the link to initiate the file download.

@beNerd, you should've probably included that in your original question... anyway, edited the answer. No silver bullet.

This will work, but the responding server will also need to set the Content-Disposition header to "attachment" if you want to force a download: stackoverflow.com/questions/8875949/

@Ben, true, but I think it makes more sense to let the browser make the decision. That's what user in most cases expects, and they can Save link as... if they want to force a download themselves.

javascript - forcing a pdf to download when clicked on button - Stack ...

javascript jquery backbone.js pdf-generation