Reputation: 117
While using browsers like Chrome and Firefox, there is an option to print the page to PDF (that is saving the webpage as a PDF file).
<a href="JavaScript:window.print();">Print this page</a>
This code opens up the print page from where the user should manually choose print to PDF. Can this be done automatically? If so please help.
Upvotes: 3
Views: 11418
Reputation: 60717
There is not much available to print to PDF in Javascript.
The most supported option is indeed to generate it server-side using a library such as tcpdf (as shown by Thein Hla Maw).
I don't think Mark's solution is the correct one, since it requires the users to have some software installed.
For pure javascript PDF generation, there is this.
NB : thanks SO :-)
Upvotes: 2
Reputation: 282805
Yes... provided that your users have a few things installed.
Then, using the API of said plugin, you can choose which driver to print to. You can even skip the print dialog.
A better option, however, would be the generate the PDF server-side, stream it to them, and then use JavaScript inside the PDF to automatically open the print dialog as soon as they view it. This way they don't need any plugins.
Upvotes: 0
Reputation: 683
You can not print page to pdf with Javascript. You have to use server side script to print page to pdf. In PHP, you can use 3rd party PDF library like http://www.tcpdf.org .
Upvotes: 2