Introduction
There are times when having users download a PDF is the better option. This is often the case with fillable forms or documents meant to be saved locally. You can do this using the HTML download attribute or by setting server-side rules to treat PDFs as attachments.
There are a few ways to trigger file downloads. This article focuses on the HTML download
attribute and changes to server response headers.
Download attribute
In HTML, you can ask the browser to download a link's destination content using the download attribute
in an anchor element.
<a href="files/download.pdf" download>Download my PDF document</a>
The attribute usually takes precedence over server configuration, though browser support and CORS policies may affect this.
Server response and headers
If the HTML attribute doesn't work or if you need to apply a change globally, the server can be configured to treat PDF files as generic data streams and prompt the browser to download them.
The Windows web.config
and Apache .htaccess
configuration files contain instructions, rules, and other variables that apply to the server. The files are typically found in the root home folder and propagate down, applying to every folder and subfolder. Configuration files can also be added to specific folders if they should only affect particular files.
Always back up your configuration files before making any changes. Incorrect edits can prevent your site from loading properly.
MIME types are used to tell the browser how it should identify and interpret a file. MIME stands for Multipurpose Internet Mail Extensions, but in web development, it refers to the content type used in HTTP headers. The default MIME type application/pdf
is overridden with application/octet-stream
. This instructs the browser to treat the file as a generic binary stream instead of trying to render it.
Response headers tell the browser how to handle a document. They can instruct it to display the file in the browser or prompt the user to download it as an attachment.
These rules apply only if the server has permission to modify headers and MIME types. Some shared hosting environments may restrict these changes.
Apache (.htaccess)
<FilesMatch "\.(pdf)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
IIS (web.config)
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Set Content Type and Disposition for PDF">
<match url=".*\.pdf$" />
<action type="Rewrite" url="{R:0}" />
<serverVariables>
<set name="RESPONSE_CONTENT_TYPE" value="application/octet-stream" />
<set name="RESPONSE_CONTENT_DISPOSITION" value="attachment" />
</serverVariables>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Note that this requires the server to be configured to allow variables. If it's not, you can add the following to allow variables to RESPONSE_CONTENT_TYPE
and RESPONSE_CONTENT_DISPOSITION
. You'll need your website's IIS name, which can be found in the management console.
<location path="IIS website name">
<system.webServer>
<rewrite>
<allowedServerVariables>
<add name="RESPONSE_CONTENT_TYPE" />
<add name="RESPONSE_CONTENT_DISPOSITION" />
</allowedServerVariables>
</rewrite>
</system.webServer>
</location>
Takeaways
- Use the
download
attribute when you want users to save a PDF from a link. - Set up server-side rules if you need all PDFs to download automatically.
- Override the MIME type using
application/octet-stream
and setContent-Disposition
toattachment
. - Test on different browsers to ensure consistent behavior.