9

I need to download pdf's from one of our online resources.There is no built in function to batch download.The only way to do it is to navigate to each pdf file, click to open, then click download. There are several thousand files and this would take a very long time to do. I got around this in the past using javascript. I gathered all the links to the pdfs, put them in a csv, and had the code loop through each link, download, and move onto the next link.

Unfortunately, I have lost that code and my efforts to recreate it have been unsuccessful.

I have tried everything in this article: How to download PDF automatically using js?

I have tried the code from this article (which I'm pretty sure is what I did before): https://www.convertplug.com/plus/docs/download-pdf-file-forcefully-instead-opening-browser-using-js/

This is what I think should work...per the second article I referenced above

function download_file(fileURL, fileName) {
var link = document.createElement('a');
link.href = fileURL;
link.download = 'file.pdf';
link.dispatchEvent(new MouseEvent('click'));
}
var fileURL = "link/to/pdf";
var fileName = "test.pdf";
download(fileURL,fileName);

The code above is just to test download one file from a hardcoded URL. If it worked as intended, when the page is loaded, it should download the pdf from the provided url. Instead, it doesn't do anything on load or refresh. Any suggestions?

KMCSACK
  • 121
  • 1
  • 1
  • 3
  • is it possible that the browser is not allowing your app to download multiple files? https://android.stackexchange.com/questions/200312/how-to-allow-downloading-multiple-files-again-in-chrome – Barak May 03 '19 at 05:58
  • 3
    Try download_file not download – Smart Manoj May 03 '19 at 06:05

2 Answers2

12

Please check https://stackoverflow.com/a/18983688/6923146

<a href="http://www.africau.edu/images/default/sample.pdf" download="sample.PDF">click me</a>

Another one https://stackoverflow.com/a/45905238/6923146

function download(url, filename) {
fetch(url).then(function(t) {
    return t.blob().then((b)=>{
        var a = document.createElement("a");
        a.href = URL.createObjectURL(b);
        a.setAttribute("download", filename);
        a.click();
    }
    );
});
}

download("https://get.geojs.io/v1/ip/geo.json","geoip.json")
download("data:text/html,Hello Developer!", "HelloDeveloper.txt");

I hope it helpfull

https://www.convertplug.com/plus/docs/download-pdf-file-forcefully-instead-opening-browser-using-js/

  • 1
    Thanks for the Info! It must have something to do with my browser. Both code snippets above just open the file at the URL, not download. – KMCSACK May 03 '19 at 17:18
  • I just tried it again in Firefox and IE, but same results... so not a browser issue? – KMCSACK May 03 '19 at 17:24
  • I am getting following error. `t.blob is not a function` If I remove the function parenthesis after the blob then the downloaded file is corrupted / empty. – Sardar Faisal Nov 30 '22 at 17:40
6
  1. You must add link element to DOM

function download_file(fileURL, fileName) {
  var link = document.createElement('a');
  link.href = fileURL;
  link.download = fileName;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}

var fileURL = "https://cdn.sstatic.net/clc/img/jobs/bg-remote-header-sm.png";
var fileName = "test.pdf";
download_file(fileURL, fileName); // fix function name
  1. Link must be in same origin

    The download attribute on anchor was ignored because its href URL has a different security origin.

Medet Tleukabiluly
  • 11,662
  • 3
  • 34
  • 69
  • What does that mean? I was able to get Hardik's code to work but when I add my own url, it does nothing. The code you provided only brings me to the file, but does not download. – KMCSACK May 03 '19 at 17:39
  • Ok, I just researched what "origin" means. How can I make this work if my script lives on the localhost? – KMCSACK May 03 '19 at 18:05
  • Origin means you cannot download file from other url, only in current url the script is running – Medet Tleukabiluly May 04 '19 at 16:36
  • I have done this before, I just lost the code. Is it possible that my online resource added a security feature to prevent what I am doing? Like I said I made this work in the past. – KMCSACK May 05 '19 at 17:29