Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
bevc

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. 

 

Share this post


Link to post
Share on other sites

Hello! Thanks for the nice words on the book. I really appreciate it. I've done many sites over the years that generate good PDFs, although it's been a while. What library or functions are you trying to use?

  • Upvote 1

Share this post


Link to post
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! 

Share this post


Link to post
Share on other sites

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

http://ourcodeworld.com/articles/read/226/top-5-best-open-source-pdf-generation-libraries-for-php

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...