1

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.

rinaldy31
  • 117
  • 2
  • 9
  • check it https://stackoverflow.com/questions/2255291/print-the-contents-of-a-div – Ahmed Ginani Jun 05 '17 at 06:48
  • You can have the server create and deliver it _without_ saving it there. So on-the-fly generation. – arkascha Jun 05 '17 at 06:50
  • @AhmedGinani i will check it, thankyou – rinaldy31 Jun 05 '17 at 06:52
  • @arkascha what do u mean? i don't get it haha – rinaldy31 Jun 05 '17 at 06:52
  • Generting a document (pdf or whatever format) on the server side does _not_ mean you have to save it to a file there. Usually such document is generated upon request by a client and transferred immediately to that client _without_ creating a server side file. – arkascha Jun 05 '17 at 07:06
  • @arkascha so can i use `window.open` to generate and save file? – rinaldy31 Jun 05 '17 at 08:17
  • You implement an ajax request on the client side (so in javascript) which hands over all information required to create the document to a server side script (could be php). That script uses some PDF generation library to create the document and streams the content as a response to the request. The client side receives the response and can do with it whatever you like: print it, save it, visualize it. – arkascha Jun 05 '17 at 08:19

1 Answers1

0

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

Hussy Borad
  • 626
  • 5
  • 20