How to make PDF file downloadable in HTML link?

How to make PDF file downloadable in HTML link?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Controlling PDF Behavior in HTML Links

When you wrap a PDF filename in an anchor tag like <a href="myfile.pdf">Download Brochure</a>, the browser relies on heuristics to decide how to handle the request. If your server sends the file with a Content-Type of application/pdf and includes a Content-Disposition: attachment header, modern browsers will trigger a download dialog regardless of whether Adobe Acrobat Reader is installed or configured as the default viewer.

However, if you omit that header or rely solely on the URL extension, the browser may attempt to open the file inline. This behavior stems from how browsers manage "application associations." When a PDF exists locally and is associated with a specific handler (like Adobe Reader), the browser acts as a proxy for that application rather than treating the file as raw binary data intended for storage.

To force a download popup, you must explicitly tell the browser to treat the response as an attachment. The most reliable way to do this is by setting the Content-Disposition HTTP header on your server response. This header overrides the user's default preferences and ensures the resource is saved to disk rather than rendered in a plugin or viewer window.

In PHP, for instance, you would modify your script before sending the headers:

header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="myfile.pdf"'); // Change 'inline' to 'attachment'
// Or simply:
header('Content-Disposition: attachment; filename="myfile.pdf"');
readfile("path/to/myfile.pdf");
exit;

Note the critical difference between inline and attachment. The inline directive allows the browser to display the file if a plugin is available, while attachment forces a download. If you are using a framework like Laravel, you can achieve this directly within your controller or route closure:

public function download()
{
    $file = public_path('documents/myfile.pdf');

    return response($file->getContents(), 200)
        ->header('Content-Type', 'application/pdf')
        ->header('Content-Disposition', 'attachment; filename="myfile.pdf"');
}

This approach works consistently across Chrome, Firefox, and Edge. Even if the user has Adobe Acrobat installed, the browser will respect the attachment directive and open its native file save dialog. If no PDF viewer is installed, the system will fall back to the standard OS download handler, which also prompts the user.

There are scenarios where client-side manipulation might seem like a shortcut. Some developers consider using JavaScript to intercept the click event and programmatically trigger a download:

document.getElementById('download-link').addEventListener('click', function(e) {
    e.preventDefault();
    const link = this;
    fetch(link.href).then(response => response.blob()).then(blob => {
        const url = window.URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = link.getAttribute('href').split('/').pop();
        a.click();
        window.URL.revokeObjectURL(url);
    });
});

While this method forces a download, it adds unnecessary complexity and overhead. It requires creating an XMLHTTPRequest or fetch call, handling binary data conversion, and managing memory cleanup. Furthermore, if the server doesn't send the correct headers initially, the browser might still try to render the blob inline depending on its internal logic regarding content types. Server-side control remains the superior solution because it operates at the protocol level before the browser renders anything.

Another common pitfall involves caching. If a user downloads a PDF and then navigates away, subsequent requests for the same URL might serve a cached version. While this doesn't change the download behavior per se, ensuring your cache headers are set correctly (no-cache or appropriate max-age) prevents stale content from being served if you update the file frequently.

If you are serving these files through a CDN or a cloud storage bucket, ensure that the metadata is configured to prioritize the attachment disposition over inline viewing. Many platforms allow you to set this via bucket policies or object attributes without writing custom code in your application layer.

Ultimately, the decision to force a download is a trade-off between user experience and control. Allowing inline viewing can provide a seamless reading experience for users who want to flip pages without saving, but it risks exposing sensitive data if the browser cache is accessed from other processes. For brochures, invoices, or reports where immediate storage is preferred, the attachment header is the industry-standard solution to guarantee that behavior.

Our network