Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download PDF automatically using js?

My scenario is that PDF file download automatically, then user fills it and when click on submit button in PDF it connect to java servlet and save it in DB.

  1. User click on Button
  2. JavaScript code run and PDF file download automatically
  3. open file using JavaScript automatically
  4. user fills & press submit
  5. after submit servlet code run and save data in db

In my Application just the 2nd point is missing. Please provide code how to interact with extension using JavaScript to download file automatically. I just want to download the file.

like image 804
Ehsaan Israr Avatar asked Jan 09 '16 08:01

Ehsaan Israr


People also ask

How do I make a PDF download automatically?

On the right, go to the Content section, and click on Additional content settings. Click on PDF documents. On the next page, turn on (enable) the Download PDF files instead of automatically opening them in Chrome option. You are done.

How do you automatically download a file in JavaScript?

Inside the DownloadFile JavaScript function, the URL of the File is passed as parameter to the GET call of the JavaScript XmlHttpRequest call. Then inside the onload event handler, the received Byte Array (Binary Data) is converted to BLOB object and the File is downloaded in Browser. //Set the File URL.

How can I download a PDF from a URL using JavaScript?

download = 'file. pdf'; link. dispatchEvent(new MouseEvent('click')); } var fileURL = "link/to/pdf"; var fileName = "test. pdf"; download(fileURL,fileName);

How do I save a PDF in JavaScript?

To save HTML page as PDF using JavaScript, we can use the html2pdf library. const element = document. getElementById("element-to-print"); const opt = { margin: 1, filename: "myfile.


3 Answers

Use the download attribute.

var link = document.createElement('a'); link.href = url; link.download = 'file.pdf'; link.dispatchEvent(new MouseEvent('click')); 
like image 54
minj Avatar answered Oct 01 '22 17:10

minj


It is also possible to open the pdf link in a new window and let the browser handle the rest:

window.open(pdfUrl, '_blank');

or:

window.open(pdfUrl);
like image 43
Andreas Avatar answered Oct 01 '22 19:10

Andreas


/* Helper function */
function download_file(fileURL, fileName) {
// for non-IE
if (!window.ActiveXObject) {
    var save = document.createElement('a');
    save.href = fileURL;
    save.target = '_blank';
    var filename = fileURL.substring(fileURL.lastIndexOf('/')+1);
    save.download = fileName || filename;
       if ( navigator.userAgent.toLowerCase().match(/(ipad|iphone|safari)/) && navigator.userAgent.search("Chrome") < 0) {
            document.location = save.href; 
// window event not working here
        }else{
            var evt = new MouseEvent('click', {
                'view': window,
                'bubbles': true,
                'cancelable': false
            });
            save.dispatchEvent(evt);
            (window.URL || window.webkitURL).revokeObjectURL(save.href);
        }   
}

// for IE < 11
else if ( !! window.ActiveXObject && document.execCommand)     {
    var _window = window.open(fileURL, '_blank');
    _window.document.close();
    _window.document.execCommand('SaveAs', true, fileName || fileURL)
    _window.close();
}
}

How to use?

download_file(fileURL, fileName); //call function

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

like image 45
Suresh Suthar Avatar answered Oct 01 '22 18:10

Suresh Suthar