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?

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #28107
    ayyaiyai
    Participant

    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?

    #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.

    #71521
    ayyaiyai
    Participant

    The FPDF worked great, thank you.

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.