Tutorials

Download PDF File From URL Using jQuery AJAX Method

In this tutorial, you will learn how to download a PDF file from a URL using jQuery and AJAX. We will also use some HTML5 and core JavaScript code to build the application.

The complete source code of the PDF downloader developed using jQuery and AJAX is given below.

To get started, simply create a new HTML file index.html and copy/paste the below-mentioned code into it.

index.html

<button type="button" id="download_pdf_file">Download PDF File!</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$('#download_pdf_file').on('click', function () {
    $.ajax({
        url: 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/172905/test.pdf',
        method: 'GET',
        xhrFields: {
            responseType: 'blob'
        },
        success: function (data) {
            var a = document.createElement('a');
            var url = window.URL.createObjectURL(data);
            a.href = url;
            a.download = 'my_file.pdf';
            document.body.append(a);
            a.click();
            a.remove();
            window.URL.revokeObjectURL(url);
        }
    });
});
</script>

Code Explanation

  1. At first, we will create a button using HTML. The user will click this button to download the file.
  2. Load the jQuery using CDN.
  3. Attach an onClick event to the “Download PDF File!” button. This method will be executed whenever the button is pressed.
  4. Now make an AJAX call to the desired PDF file using the built-in ajax() method of jQuery.

Run the Project

In order to see our code in action, go ahead and open the index.html file in your favorite web browser.

The output will look something like this screenshot.

Here, you need to click the “Download PDF File!” button.

When you click this button, the PDF file will automatically start downloading on your PC.

Furqan

Well. I've been working for the past three years as a web designer and developer. I have successfully created websites for small to medium sized companies as part of my freelance career. During that time I've also completed my bachelor's in Information Technology.

Recent Posts

MiniMax-M1 vs GPT-4o vs Claude 3 Opus vs LLaMA 3 Benchmarks

MiniMax-M1 is a new open-weight large language model (456 B parameters, ~46 B active) built with hybrid…

June 22, 2025

How to Use Husky with npm to Manage Git Hooks

Managing Git hooks manually can quickly become tedious and error-prone—especially in fast-moving JavaScript or Node.js…

June 22, 2025

How to Use Lefthook with npm to Manage Git Hooks

Git hooks help teams enforce code quality by automating checks at key stages like commits…

June 22, 2025

Lefthook vs Husky: Which Git Hooks Tool is Better? [2025]

Choosing the right Git hooks manager directly impacts code quality, developer experience, and CI/CD performance.…

June 22, 2025

Llama 3.1 vs GPT-4 Benchmarks

We evaluated the performance of Llama 3.1 vs GPT-4 models on over 150 benchmark datasets…

July 24, 2024

Transforming Manufacturing with Industrial IoT Solutions and Machine Learning

The manufacturing industry is undergoing a significant transformation with the advent of Industrial IoT Solutions.…

July 6, 2024