简体   繁体   中英

How to create a download file and not save it in server with javascript

i have problem about how to create button which will downloading PDF file for one div.

i tried window.print() like this :

$(document).ready(function(){

        $('#printBtn').on('click',function(){
            window.print();
        });

    });

and save it as PDF file. i don't know other ways than that and i don't want to save it on server first.

You try direct download link with using a elements attribute download

<a href="/images/myw3schoolsimage.jpg" download>

---- OR ----

<button id="print" onclick="print('uname');" >Print</button>
<div id="uname">
<label>Name</label>
<label>Spider</label>
</div>

<button id="print" onclick="printContent('uname');" >Print</button>

    function print(e){var divToPrint=document.getElementById('e');

    var newWin=window.open('','Print-Window');

    newWin.document.open();

    newWin.document.write('<html><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>');

    newWin.document.close();

    setTimeout(function(){
        newWin.close();
    },10);
}

Demo Here

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