Automatically Download PDFs by jQuery

Have you ever face the problem to download the PDF automatically? Then don’t worry I will explain to you the complete code in this article. While downloading the PDF you have the following option
1) On click download PDF
2) Automatically download PDF

1) On click download PDF

On Click download PDF is so easy you just need to add the download attribute to a elements. If you used, this attribute then the resource it points to be downloaded rather than navigating to it.

<a href="pdffilelink.pdf" download>Download PDF</a>

In some browsers, this code is not supported, even It won’t work in Internet Explorer or Safari. So, the best thing you can open the link in a new tab when it can’t be downloaded by adding target="_blank" .

<a href="pdffilelink.pdf" target="_blank" download>Download PDF</a>

2) Automatically download PDF

In some cases, we need to download the PDF files automatically like on page load or any form response, etc. In this case, the above code is not supported so you need to create the virtual anchor tag and click on this. I will create a function so that you just need to copy and paste it anywhere just pass the PDF URL in this.


function autodownload_pdf(pdflink){
var link = document.createElement('a');
link.href = url;
link.download = pdflink;
link.dispatchEvent(new MouseEvent('click'));
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

*