Yarin
Yarin

Reputation: 183909

Implementing "Download as PDF" on a PHP web page

What are the range of options available for implementing a "Download as PDF" feature on a PHP-based web page? Do I need to implement a server-side PHP-based PDF generator using a third party library, or are external web services available that can accomplish this for me?

Upvotes: 6

Views: 3990

Answers (4)

Stephen R
Stephen R

Reputation: 3917

Mpdf is an excellent library for this. You can use custom fonts, control page breaks and all manner of formatting. Different methods of accepting input. It's flexible and solid (though it does not yet handle brand new methods such as CSS Flexbox).

https://mpdf.github.io/

Upvotes: 0

thirtydot
thirtydot

Reputation: 228242

If you are able to install programs on your web server: http://code.google.com/p/wkhtmltopdf/

It's very easy to generate high quality PDFs. For example:

wkhtmltopdf http://www.google.com/ google.pdf

It will do exactly what you expect.

Upvotes: 1

zanlok
zanlok

Reputation: 1630

Use TCPDF over fpdf, because:

  • a) it supports unicode
  • b) it's still being actively maintained
  • c) is based on a more modern OO architecture.

By way of trivia, TCPDF was based upon fpdf many years ago, but was almost entirely remastered since. It still comes with source code and is highly adviseable.

Example usage is another question, and a practive of extending the base class, overriding methods, etc. Don't worry, the download contains lots of samples (10 or more projects) from simple to advanced. I am experienced in PHP, but even taking that into account, some years back, I was able to ship our first prototype of localized pdf reports in all of one day.

class MyPDF extends TCPDF {
   // meaningful code
}

By the way, there's even a TCPDF tag here on StackOverflow :)

Upvotes: 2

Cam
Cam

Reputation: 15244

Note: I'm assuming you want to know how to generate PDF files dynamically with PHP

I recommend fpdf. The site seems to be down right now, but you can probably download it elsewhere.

To answer your question directly, your best option is indeed to use a third-party library. Of course a PDF file is nothing but a normal file that follows the specifications laid out for 'PDF files', so you could always roll your own.

But that's a waste of time, given what's already available freely for pdf generation. So you should find a library/class that can do it for you (again, I recommend fpdf but I'm sure there's many other libraries out there that can do this too, so look around before you pick one).

Edit: I should add that it's been a while since I've used fpdf, and zanlok seems to have indicated that it's no longer maintained, so maybe look for another library. But definitely look for one; don't roll your own!

Upvotes: 2

Related Questions