(html) Download a PDF file instead of opening them in browser when clicked
Initiate PDF download via HTML's download
attribute:
Or leverage HTTP headers for control from the server-side:
Server-magic: Controlling downloads server-side
Fancy some server-side action? Fine-tune your .htaccess
configuration to redirect PDF download requests:
Doing it with a .NET twist? Add these lines to your ASP.NET server config for that PDF funnel:
Always check browser compatibility with the HTML5 download
attribute on caniuse.com, to ensure your preceding work doesn't go down the drain.
The Deep Dive: Advanced uses and common issues
Quite straightforward, right? Let’s unpack more layers:
1. Customizing file names: An identity crisis
Don't want the original file name? Use download
with a value to customize:
2. Playing nice with legacy browsers
Older browsers don't understand HTML5? Prepare a server-side or JavaScript fallback:
3. Mime types: It's all in the label
Make doubly sure that your server is setting the correct Content-Type
(application/pdf
):
And the Content-Disposition can't go AWOL. If it does, your download
attribute might get ignored:
4. Cross-browser eccentricities
Every browser has a personality. Test across various platforms for a consistent user experience.
Was this article helpful?