How to Download PDF File on Button Click using JavaScript ?

Sometimes, a web page may contain PDF files that can be downloaded by the users for further use. Allowing users to download the PDF files can be accomplished using JavaScript . The below methods can be used to accomplish this task.

Table of Content

Using html2pdf.js library

Example: The below example shows the implementation of the above-explained approach.

Output:

Using pdfmake Library

Example: The below example shows the implementation of the above-explained approach.

Output:

Output : Using pdfmake Library

Using window.print() method

Example: The below example shows the implementation of the above-explained approach.

Output:

Like Article -->

Please Login to comment.

Similar Reads

How to Download Image on Button Click in JavaScript?

Downloading an image on a button click in JavaScript involves creating a mechanism to trigger the download action when a user clicks a button. Below are the approaches to download image on button click in JavaScript: Table of Content Using Anchor Tag with Download AttributeUsing Fetch APIUsing Anchor Tag with Download AttributeThe "download" attrib

3 min read How to Download Canvas as Image on Button Click in ChartJS?

In Chart.js, the charts that are generated can be converted and downloaded as images for easy sharing and reporting. By using built-in methods like toBase64Image() and the HTML canvas toBlob() method, we can enable users to download charts in formats such as PNG. These techniques provide a simple way to capture the visual data from charts and offer

4 min read How to set radio button checked by button click in AngularJS ?

In this article, we will see how to set the radio button checked by button click in AngularJS, along with knowing its implementation through the examples. Approach: The approach is to use the ng-checked directive to check the radio button in the DOM. In the first example, a single radio button is checked by the button and In the second example, mul

2 min read How to attach a click and double-click event to an element in jQuery ?

In this article, we will see how to attach a click and double-click event to an element in jQuery. To attach click and double-click events, click and dbclick events are used. These events are attached with the bind() method. This method is used to attach one or more event handlers for selected elements and this method specifies a function to run wh

1 min read How to change the href value of a tag after click on button using JavaScript ?

JavaScript is a high-level, interpreted, dynamically typed, and client-side scripting language. HTML is used to create static web pages. JavaScript enables interactive web pages when used along with HTML and CSS. Document Object Manipulation (DOM) is a programming interface for HTML and XML documents. DOM acts as an interface between JavaScript and

4 min read How to change a button text on click using localStorage in Javascript ?

In this article, we will learn how to change a button text using Javascript localStorage when we click on the button achieved by adding HTML onclick event listener. Approach: HTML DOM Window localStorage allows us to store key-value pairs in our browser using an object. The name of the key should be unique and key-value pairs always be stored in st

3 min read How to trigger a file download when clicking an HTML button or JavaScript?

Triggering file downloads in JavaScript refers to initiating the download of files directly from a webpage when a user clicks a button or link. This can be achieved using HTML5's download attribute or custom JavaScript functions, allowing dynamic and user-friendly file-downloading experiences. To trigger a file download on a button click we will us

4 min read How to download PDF file in ReactJS ?

To download pdf in React JS there are methods given in JavaScript and HTML DOM. They provide a convenient way to download files and store them in local computers. Here we will use locally stored pdf files in the project folder to use as a sample pdf downloaded by the browser. These are the approaches to download a PDF in React JS projects. Table of

3 min read How to Add an Input Field on Button Click in JavaScript ?

JavaScript allows us to dynamically add an input field upon button click. We can use the below approaches for adding an input field on Button Click in JavaScript. Table of Content Using DOM ManipulationUsing Template Literal and InnerHTMLUsing DOM ManipulationDOM manipulation is used to dynamically create and append input fields upon clicking the "

2 min read How to click a button to animate the paragraph element using jQuery ?

In this article, we will click a button to animate the paragraph element using jQuery. To animate the paragraph elements, we use animate() method. The animate() method is used to change the state of the element with CSS style. This method can also be used to change the CSS property to create the animated effect for the selected element. Syntax: $(s

1 min read How to add more values to array on button click using PHP ?

In this article, we will learn to add elements to an existing array with the click of a button using PHP. PHP is a server-side language, and it only responds to requests (GET, POST, PUT, PATCH, and DELETE). The button click action happens as a part of the client-side to directly call a PHP function. We need an intermediary language to perform this

3 min read How to use hide() method on button click using jQuery ?

jQuery has a lot of handy methods to get the work done easily. In this article, we will discuss one of them which is the hide() method. We can use this method for various purposes on our webpage and get an efficient result. The very first step will be creating an HTML file and link the jQuery library file via CDN. jQuery CDN Link: <script src="h

2 min read How to hide block of HTML code on a button click using jQuery ?

In this article, we will learn how to hide a block of HTML code with a button click. We can do this by using the jQuery inbuilt hide() method. Let's briefly learn the functionality of this method. hide(): In CSS, we have a property display:none which basically hides the element. This hide() method in jQuery also hides the selected element. Syntax:

2 min read How to click a button on webpage using Selenium?

This article is all about how to click any button using Selenium on a webpage and many more concepts related to the same which are discussed below. Table of Content What is Selenium?How to click on a button using SeleniumConclusionFrequently Asked Questions on How to click a button on webpage using selenium?What is Selenium?Selenium is a widely use

2 min read How to hide/show an image on button click using jQuery ?

In this article, we will see how we can hide or show any particular image in jQuery when a button gets clicked. This is quite easy to do with some lines of jQuery code. Before we jump to the topic, let's know which methods of jQuery will be used for this. So there is a method called show() and another one is hide(), these two methods of jQuery can

3 min read How to convert a file to zip file and download it using Node.js ?

The Zip files are a common way of storing compressed files and folders. In this article, I'll demonstrate how to convert the file to zip format by using the adm-zip module (NPM PACKAGE). Uses of ADM-ZIP compress the original file and change them to zip format.update/delete the existing files(.zip format).Installation of ADM-ZIP: Step 1: Install the

3 min read How to programmatically fire a click event for a file input element in JavaScript ?

In this article, we will learn how to programmatically fire click events on the input file element. ApproachWhenever you want to perform a click event programmatically, at your specific condition, just use the JavaScript in-built click() function by DOM object. Example: document.getElementById('your_input_type_file_element_id').click();Example 1: W

2 min read How to load notification alert on top right corner without click of button in bootstrap ?

Bootstrap Toast component provides an easy way to mimic the functionality of push notifications. Bootstrap Toast is like an alert box that can be displayed to the user whenever an event occurs. This component has been built with CSS flexbox which makes it easy to position and align. It is not necessary to show the toast on a button click. It can be

2 min read How to set alert message on button click in jQuery ?

In this article, we will learn how to create an alert message in jQuery when a button gets clicked. For this, we will use the jQuery click() method. jQuery CDN Link: We have linked the jQuery in our file using the CDN link. <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" c

1 min read How to add input fields dynamically on button click in AngularJS ?

The task is to add an input field on the page when the user clicks on the button using AngularJs. Steps: The required component for the operation is created (add-inputComponent). In that component, html file (add-input.component.html) required html is written. In that HTML, the main div for input fields are there and button is created. There is a (

2 min read How to set checkbox checked on button click in AngularJS?

In this article, we will see how to set checkboxes checked with the click of a button in AngularJS. Approach: The approach is to use the ng-checked directive to check the checkbox in the DOM. In the first example, a single checkbox is checked by the button and In the second example, multiple checkboxes are checked by the button. The ng-model direct

2 min read How to hide an HTML element via a button click in AngularJS ?

In this article, we will see how to use a toggle button feature to hide and display an element by button click in Angular. The toggle button is a user interface control that may be useful for situations when the user wants to switch between 2 states or conditions. For instance, in our smartphone, we usually turn off/on the button to wireless connec

2 min read How to Create Button Dynamically with Click Event in Angular ?

The task is to create a button dynamically with a click event using AngularJS, i.e., whenever someone clicks on the button then a new button gets created. The easiest way to create a button in AngularJS is to use the ng-repeat directive. We can easily hook up a repeat logic inside a button click event by using the ng-click Directive. Syntax: <el

2 min read How to add a pressed effect on button click in CSS?

In this tutorial, we are going to learn how to add a pressed effect on a button using CSS. This effect is a part of modern UI design and is used on many websites. This effect allows the user to experience an interaction with the button element as compared to the normal behavior. We’ll take advantage of the active pseudo class. This class is added t

2 min read How to Change Button Text on Click in ReactJS ?

In this article, we will learn about diverse methods for altering button text dynamically upon clicking on the button. React, a widely embraced JavaScript library for crafting user interfaces provides several approaches to seamlessly achieve this functionality. The key lies in manipulating the component's state and adjusting its rendering logic to

3 min read Create Dynamic Table with Auto Increment Serial Number on Button Click Event

We will see how we can auto increment the serial number of a table in html using JavaScript. We will build a button clicking on that will add a row to a table with an increasing number of rows. It will show the dynamic behavior, showing that we will use JavaScript and jQuery. These are the following methods: Table of Content Using JavaScriptUsing J

3 min read How to Change the Color of Button on Click ?

Sometimes, on a web page, when a user clicks a button it gets difficult to understand whether the button is clicked or not. In such cases, you can change the background color of the button to indicate that the button is clicked. The below methods can be used to change background color of a button on click. Table of Content Using :active pseudo sele

2 min read How to create a Ripple Effect on Click the Button ?

The ripple effect is a part of the modern design trend. You have seen it on many websites specially on Google's material design language. It gives a button-pressing effect. We can make a ripple effect by adding and animating a child element to the button. We can also position it according to the position of the cursor on the button using JavaScript

3 min read How to navigate on path by button click in react router ?

Navigation in React JS is done by implementing the routing between components using react-router-dom. Prerequisites:NPM & Node.jsReact JSReact-Router-DomApproach:In React, to navigate on the path by clicking a button we will be using the useHistory hook from react-router-dom^5 (useNavigate in v6). We will set the routes for specific components

3 min read How to trigger HTML button after hitting enter button in textbox using JavaScript ?

In this article, we are given an HTML document containing a text area and the task is to trigger the button when the user hit Enter button. We can do it by using the "keyup", "keydown", or "keypress" event listener on the textbox depending on the need. When this event is triggered, we check if the key pressed is ENTER or not. If the key pressed is

4 min read Article Tags :