Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript whats the best way to convert a page to pdf with javascript? Re: whats the best way to convert a page to pdf with javascript?

#71355

You aren’t going to be able to convert a page to pdf with Javascript because it’s a client-side language (i.e. it runs on the user’s web browser). If the users have a pdf generator (e.g. Adobe Acrobat Pro) installed then they will be able to save the page as a pdf using that, but that’s of no help to you.

Since you are generating the page using PHP, I suggest you look at a pdf generator library such as FPDF or DOMPDF. These allow you to generate a pdf file using PHP in much the same way that you generate HTML.

One way to implement this would be to add a query string to the url to specify the output format. E.g:
HTML – http://example.com/articles/article.php
PDF – http://example.com/articles/article.php?output=pdf

This way you merely need <a href="?output=pdf">View as PDF</a> on your page to download the pdf.