How to Make a PDF Download Instead of Opening in Browser Using Content-Disposition Header
Learn how to force PDF files to download instead of opening in browser by setting the Content-Disposition header in your web server or PHP script.
306 views
To make a PDF download instead of opening in the browser, set the HTTP header `Content-Disposition` to `attachment`. For example, in PHP: `header('Content-Disposition: attachment; filename="filename.pdf"');`. This instructs the browser to download the file rather than display it.
FAQs & Answers
- What does the Content-Disposition header do? The Content-Disposition header tells the browser how to handle a file, whether to display it inline or prompt the user to download it as an attachment.
- How can I force a PDF to download using PHP? You can force a PDF to download by setting the HTTP header in PHP like this: header('Content-Disposition: attachment; filename="filename.pdf"'); before outputting the file.
- Why does my PDF open in the browser instead of downloading? If the Content-Disposition header is not set to attachment, browsers usually open PDFs inline by default instead of downloading them.