Hi all

I'm generating some complex tables in PHP and want to offer users the ability to download a PDF of the table after viewing it in the browser as HTML.

I'm using FPDF to generate the PDF itself.

I could include a 'Download as PDF' button on the page that links to another PHP page which generates the table straight to PDF, then opens it in the browser or forces it to download as a file. However, the tables take a long time to generate; the user would have to wait for the HTML table, then wait again if they choose to download as PDF so I'm looking at some other options.

1) Present the user with the option 'PDF or screen' at the start. I'm not keen on this; in many cases the user will want to view the report then decide if they want a PDF or not.

2) Generate the entire table as an HTML string, and simply output it to the browser when it's finished. I can then set a session variable containing the string and point the download button to another PHP page that uses FPDF's table-to-PDF function to parse the string. I haven't tested the table-to-PDF function yet but if it works, it'll certainly help; some of these tables have multiple rowspans and colspans, so generating the PDF manually would be complex.

3) Generate the PDF manually inline; so when I'm outputting a TR or TD or whatever, at the same time, run the $pdf-> commands. When the table is finished, the PDF will be ready too.

I'm most keen on option 3. However, I can't quite see how to trigger the PDF download with a button. When the PDF is complete, the $pdf->Output() command can load the file into the browser, force it as download or save it as a file on the server. If I save it as a file, I could download it with the button but then I have to 'clean up' the saved files periodically.

So my questions are:

  • How would the experts do it? One of the above or some other way?
  • How can I trigger Output() on a click, so I don't have to write a file to the server?

Thank you!

    i'lld generate the pdf on demand if it takes more than a couple of seconds to convert a couple of pages from html to pdf them i would look at the software you are using to do the pdf, we can create 5 pages a second of a complex template on 1 core.

      I don't think the table to pdf function in FPDF can handle rowspans - I'm happy to be told I'm wrong
      (from my own use I'd also say the FPDF table conversion scripts are basic)

        yeha i use the TCPDF library directly, test 4 others but found this was the best for both speed and functionality.

          5 days later

          Thank you all for your input !

          I hadn't seen html2pdf, looks good. I've started using the brilliant DataTables to present my tables now, partly because I wanted to improve the functionality anyway, and partly because it has print, save as CSV, Excel and PDF buttons as plugins, three of which I need to provide anyway.

          dagon, the reason the tables take a long time in this case is due to the SQL processing required in the back end; the PDF part isn't taking any time.

          The DataTables PDF output is pretty basic though, so I might still need to generate my own using the libraries mentioned.

          I'll set this thread as resolved since the suggestions here answer my questions and will also help people looking for PDF ideas.

          Thanks again

            thanks for the link to datatables - could come in handy
            (and shows once more how UI is inexorably moving into js)
            [MPDF is also worth a look as another FPDF based library]

              Write a Reply...