Jump to content
Larry Ullman's Book Forums

Creating Pdf From Php

Recommended Posts

I apologise in advance if I'm committing a faux pas posting here, I couldn't see any forum rules! I read Mr Ullman's PHP for the web book, and it was the best I've read, really clear and easy to follow, it helped me from start to finish on what was (for me) a very big project, but this topic doesn't relate to it. 


I have what I thought would be small problem, but is causing me endless headaches. The users on my project have requested that the final report (that is created using php and mysql) be downloadable as pdf. 


The best I have managed is a fuzzy blurry pdf that's hugely bloated and illegible and the colours don't render properly! All the tutorials and code I've seen online seem to deal with creating either a 'new' pdf (eg an invoice) or they just render static html, rather than reproducing exactly what's in the browser from php. 


Safari's 'Export as pdf' command does basically exactly what I want, but I can't ask users to make sure they use Safari! if anybody could help me or point me in the right direction I'd be extremely grateful, it's driving me a little bit doolally. 


Thank you very much. 


Link to comment
Share on other sites

Thank you very much for the response, your book was EXTREMELY helpful, and was what got me through the project (my first php experience), I’m looking forward to reading your other books on php and Ruby too!


I used something called 'html2canvas' and some cribbed javascript which created a pdf but the file was huge, and the content didn’t render property at all. I have been able to use ‘html2pdf’ and ‘dompdf’ to create ‘Hello World’-type pdfs, but I can't figure out how to take the dynamically created content and create a report that looks like what appears in the browser.


I know very little about libraries, if you could point me in the right direction and tell me what library or functions I should be concentrating on, or any advice at all, it would be very much appreciated.


Thank you! 

Link to comment
Share on other sites

  • 4 weeks later...

Thanks for the nice words and sorry for the delayed reply! I think what you really want to do is find a server-side library. From this page--


--it looks like https://tcpdf.org/ is the best choice. 


What you'll want to do is have the PHP code create the PDF as a temporary file on the server and then send the file to the browser. This means you can develop the process starting from either end: first learn how to serve PDF files via PHP and then learn how to generate them dynamically, or vice versa.


This is definitely do-able and I've done it myself, but it'll take some trial and error and patience to pull off! Let me know if you have any more questions.

Link to comment
Share on other sites


  • Create New...