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!