简体   繁体   中英

Download a pdf (not open in tab) in Javascript

I have a landing page on which there is a form which is filled by user and then it goes to a thank you page (which is another page). On thank you page I simply want to prompt the user for saving a pdf as the thank you page loads. Please note I want the file to be saved/downloaded and not opened on the same tab or another tab in the browser. I have tried multiple solutions but they all seem to open the pdf in the browser itself and not download it. Some of the methods I have tried are meta tag download, page redirect download, iframe download but they all open the pdf in browser itself and not download it. I just want a simple solution for downloading the file and not opening it in the browser. Also the pdf should download automatically without clicking any link or button.

The document.ready event of the thank you page should trigger a local script that runs a server script to download the PDF. Be sure to set the headers as described above. If you try to link directly to the PDF, it'll try to load in the browser.

Alternatively, you could create the PDF in a directory that is already set to only download files by setting the .htaccess file for that directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM