Grow your CSS skills. Land your dream job.

whats the best way to convert a page to pdf with javascript?

  • # February 23, 2010 at 9:45 am

    If I want to allow users to create a pdf from a php results page, what’s the easiest way to do this?

    I found this while googling http://www.printfriendly.com/, but it’s down or something because when I add their button to my pages the convert page just stays blank. But how are they doing that? Is there away to add my own javascript for this function?

    # February 23, 2010 at 10:48 am

    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.

    # February 24, 2010 at 4:15 pm

    The FPDF worked great, thank you.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".