Best of PHP scripts for generating PDF files

There are several situations where a dynamic PDF file is a great solution to offer digital documents right in your web application . Think about order documents in your eCommerce application, coupons for your marketing campaign or just the PDF version from your web page. There are plenty of situations were a dynamic PDF document is a great way to solve problems, too. Using PHP it’s possible generate those PDF files. As a developer, you can use several free PHP classes and there is also the PDFlib library which is a commercial product. This list is a showcase of the most popular PHP classes and libraries.PDF format

Generate PDF files using PHP

The following classes are written to generate PDF files “from the scratch” by using native PHP functions:


The FPDF class is written for PHP4 and was the first free PHP script for creating PDF files “on the fly”. I’ve used this class 8 years ago to generate PDF documents in several eCommerce applications. The project website offers great documentation and also a lot of useful code examples. The latest version is from June 2011 and I hope the next version is written for PHP5. The original version doesn’t support UTF-8, but since a while there is class extension named tFPDF which supports UTF-8.

Codecanyon PHP 2 PDF Scripts


Like many other PDF scripts is the TCPDF project based on FPDF, too. The TCPDF class was in the beginning a rewrite for PHP5 with the support for UTF-8. Now is this project a great PDF generator with great image functions including support for the CMYK color mode.
The missing support for UTF-8 in FPDF was the reason that I use the TCPDF class for all my projects now. Even for projects where I have used the FPDF class, it was very easy to make the move to this project. The documentation for the TCPDF project is not so good as for the FPDF class, but there are a lot of well-documented examples.


The Zend framework has also PDF generation class, which has less functions than FPDF or the other classes. Like many other classes inside the Zend Framework is the documentation very limited for this class as well. If you’re already using the Zend Framework for your project and you need some basic PDF function, you should go for this dynamic PDF solution.


The PDFlib is the commercial PDF solution for PHP. If installed (compiled), you can use the PDFlib functions right in your PHP code without the need to include a class file or library. This might work better and is often faster, but this product has also a “great” price. I’ve worked with PDFlib in one of my projects in the past and PDFlib is a great PDF generation script. If you look for the PDF documentation on the PHP website, the information is outdated and isn’t updated for the current version anymore. At the moment, that your project becomes bigger and creating PDF files is an important part within your project, you should think about this PDF library for PHP.

Import PDF files and add content using PHP

The classes above are tools for creating PDF files using PHP code, for example to generate PDF invoice documents. Using these tools you need to create any element for your PDF document. The following classes will help you to use existing PDF files to create a new dynamic PDF file.


FPDI is a great class extension for the FPDF and the TCPDF class script. It’s easy: The PHP code opens a file, keeps it in the web server’s memory and you can add text and images. The following example will show how it works.

Note, this is just an example on how to use the FPDI class. Since the object is written for PHP5 you need to use the TCPDF class or you need to change the code first for PHP4.

PDI (a PDFlib product)

There is also a (commercial) PDF import function name PDI for the PDFlib library. According to their website, it’s required to buy this extension or class together with PDFlib as a bundle. I never used this PDF import class, but I think it’s important to mention this script, too.

HTML to PDF conversion with PHP

There are a lot of requests for HTML to PDF conversion. This feature is useful if you like to generate a PDF version from your website dynamically. The following two projects are the most important scripts (if you talk about available information and frequent updates).


mPDF is based on FPDF and HTML2FPDF, with a number of enhancements. The script is able to process utf-8 encoded HTML code and is able to use CSS stylesheets for the generated PDF layout.


dompdf is an HTML to PDF converter which is able to read/parse CSS 2.1 compliant HTML layouts. The script is able to render styles from external stylesheets and the style attributes from individual HTML elements. The PDF rendering function is currently provided either by PDFLib or the R&OS CPDF class written by Wayne Munro.

These are a lot of tools for doing some PDF job, if I forgot an important PHP class or library please mention them in a comment.

28 thoughts on “Best of PHP scripts for generating PDF files

  1. You should give PopPHP a whirl – it has a Pdf component. You can embed fonts, import pages, order them, etc.

    • Hello Nick,
      thanks for sharing, I see a lot of functions for the PDF creation (also an import function).
      Are there any code examples or resources (tutorials/blogs) about the framework?

      Edit: I need to look better examples are included in the file I’ve downloaded :)

  2. Personaly i use wkhtmltopdf (and sometime wkhtmltoimage)
    based on webkit, this GNU project provide a simple binary (.exe, .dmg and linux binaries are available) who convert a html page (with javascript and css, i never test flash or html5’s video) into a pdf (or an image).

    The render is exactly identical as a safari or chrome browser.

    • Thanks for sharing Jeremy, there are many html2pdf conversion tools. I mentioned only those two because they are more or less PHP scripts.

      EDIT: I searched Google for “wkhtmltoimage” and found also resources on how to create remote screenshots using the webkit, interesting…

  3. I’m using wkhtmltopdf at work on a couple of projects, and I’d choose it over anything on your list, anytime of the day – simply because it renders much nicer pdf’s and does it much simpler (not a critique of your list, btw, just a grumble I have with PDF libraries in php). Also, because it’s a standalone binary that easily converts html, we just use it as a service: generate whatever you want for pdf as html, then save it to pdf with wkhtmltopdf.

    • Hi Peter,
      There is differnet function for PHP PDF libs: You build a PDF from the scratch or you add data to existing ones.
      HTML2PDF conversion is about existing HTML pages. If I need a PDF invoice, it makes no sense to create a HTML invoice first.
      Or do you do this? :)

  4. I use highcharts (a javascript library) to generate graphs on my web site.
    Thanks to wkhtmltoimage, I simply convert this graphs into png, to provide a daily email reports for our customers.
    It’s realy powerfull.

    • @Jeremy, Recently I created PDF files using TCPDF using the image API from Google Maps, that was nice one too :) But you’re right that are special usage cases.

  5. html + css provide a lot of way to customize your layout. The language is designed to render contents.

    even if you create a intermediate html page to generate the pdf, it will always be faster than counting pixels of your text who may be contains line breaks. And in both way you should use a template system it could be a html file (with wkhtmltopdf) or list of objects manipulation (with pure php scripts)



    $this->Cell($width, $height, $string, 0, 0, “”);

    • Hi Jeremy,
      If I create PDF files with TCPDF, I have a function to create a single field 😉
      So you say you create a HTML file with CSS stylesheet first if the page need to be processed only (f.e. if the PDF invoice is created in the background).

      even if you create a intermediate html page to generate the pdf, it will always be faster than counting pixels of your text who may be contains line breaks.

      Using TCPDF you can render HTML too, so the linebreak is not a problem. How about page breaks how do you do this?

      Against what everyone says here, I think using PHP scripts to create PDFs it’s possible to work with millimeters or inches, something you can’t do in HTML, how do you take care about this? Do you work with percentages?

  6. Hey Olaf, yes, I’m currently working on some tutorials and example files (as well as a better website.) Probably the best thing at this time is to take a look at the example under the documentation here: (which is basically similar to the examples included that you downloaded.)

    I’m going to try and get better examples up soon with the new website. Thanks!

  7. Oh one more thing – I don’t know if you’d be interested in this part as well. But along with the Pdf component, there’s an Image component and a Graphing component, which allows you to make graphs in just about any format – image, pdf of svg. Thanks, Nick.

    • Thanks Nick for this links and the information.
      I’m sure the people using PHP libs for PDF creation like to try the POPPHP framework 😉

  8. Olaf,

    Yes we done invoices etc using wkhtmltopdf, it is considerably easier to maintain than huge amounts of php code.

    More recently I’ve been looking at openrpt, an xtuple project that you can design pdf layouts in a wysiwyg tool, along with the associated database queues. Then in you just call the executable and it generates a elegant pdf.

    You can even let the designers lose on it 😉

    • Hi Alan,
      Thanks for your comment (the next guy from the HTML2PDF corner, hehe I’m just joking).
      I have this question for you because you mentioned the creation of invoices in your comment. What if you need to open an almost ready PDF file which is made by some other company?
      One of my projects was a catalog for the real estate market, 80% of the final PDF file was static while the script need only to fill a few sections with some property information.
      Sure you can create HTML code for these pages too, but do you get one single file if you need to edit only page 2 and 5 (from 8 pages)?

  9. All the php to pdf librarys are great to work with only if you are not intending on creating active forms.
    I have been trying to create an active form which can be saved offlline then submited but so far most library’s won’t work in the pdf reader.

    • Hi Vic, that’s a good one :)

      You can use FDF for this:

      You have to know that FDF is an very “old” script. PDF form fields are an feature from Adobe and is available in the full version of Adobe Acrobat. It’s possible that these “old” PDF forms created by FDF are not compatible with the current Adobe version. Give it a try an share your experience (if you like).

  10. Olaf,
    Yes modifying an existing pdf its not really where Wkhtml excels,

    I did write a svg to pdf convertor a long time ago that enabled the designers to deliver a pdf, e converted out to svg and added template style tags. Still being used today doing 1000 invoices a month. If I was to do it again, openrpt would be my first choice


    • Thanks Alan for that information.
      I’m sure many people reading these comments will think that wkhtmltopdf is the solution for anything :)
      One thing I like to know is, how do you “fill” a whole page using wkhtmltopdf? I would like see my A4 page with a footer and 15mm padding to the page borders.
      HTML is still measured in mm or inches, and is there a way multiple pages (page 2 from an invoice for example)

  11. Wkhtmltopdf allows you to specify secondary html pages for headers and footers, its tricky to get right, but does work. I think it can also add page numbers
    For sizing, you have to trail and error the output. But usually if you get the look ok on the Web page, the print out is pretty similar

    Its a relatively fast , maintainable and reasonably reliable solution, but not design prefect composted with other ways to to it.

  12. Thanks Lucasz, I’m sure it’s a great tool for users using the Zend Framework for their projects.

  13. Never Tried PrinceXML ? we use it in our company… best solution for us, it convert xhtml to pdf perfectly….

    • Hi Samuele,
      I think PrinceXML was never mentioned because it’s not free, you need to pay a lot of money if you like use it for a commercial application.
      But thanks for mentioning.

  14. For me the best tools are wkhtmltopdf and pahtomJs – both generate pdf from html and are based on WebKit. The most important thing is that they are generating it very quickly (40 pages for 4-5 seconds – the above libs will do it for hour or more if at all)

    • Hello Todor,

      thanks for the comment. Wow 10 pages in a second is a lot, buy how do you do that with paging (for invoices with more than one page)?

  15. Hi Olaf,
    With wkhtmltopdf you have to use this option when generating the pdf:
    -H, Add a default header, with the name of the page to the left, and the page number to the right, this is short for: –header-left='[webpage]’ –header-right='[page]/[toPage]’ –top 2cm –header-line

    The full documentation is here:

Comments are closed.