简体   繁体   中英

JavaScript download pdf file while printing

I am trying to download a PDF file while printing using JavaScript. I tried afterprint event but it is hitting on both close and print button click. Is there any way to download PDF file while clicking on print button of print preview screen and to not download file on clicking close button.

window.addEventListener("afterprint", function (event) {
    console.log("This event hit after clicking on both print and close button");
});

You can wrap print button with <a> tag and use download attribute instead of writing event listener.

For Example:

 <a href="specify file path" download>print</a>

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