javascriptjquerypdf

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.


Solution

  • Use the download attribute.

    var link = document.createElement('a');
    link.href = url;
    link.download = 'file.pdf';
    link.dispatchEvent(new MouseEvent('click'));