Preets
Preets

Reputation: 117

Automatically print a webpage to pdf

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

Answers (3)

Florian Margaine
Florian Margaine

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

mpen
mpen

Reputation: 282805

Yes... provided that your users have a few things installed.

  1. They need a PDF print driver, such as PrimoPDF
  2. If you want to automatically print to this driver, they need to have a plugin installed, such as JS Print Setup (for Firefox) or MeadCo ScriptX for IE

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

Thein Hla Maw
Thein Hla Maw

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

Related Questions