What it is
mPDF is an open source PHP library that converts HTML and CSS into PDF documents. You lay out a page with the markup you already know, and mPDF renders it to a PDF file, with headers, footers, page numbers, tables, images and fonts. It is installed with Composer like most modern PHP libraries.
It is aimed at documents rather than web pages: invoices, reports, statements and forms that need to print cleanly on a fixed page size. It supports Unicode and right-to-left text, and runs inside an ordinary PHP application with no separate service. Page breaks, repeated table headers and watermarks are handled for you.
How I use it
- Rental Pilot's PHP API uses mPDF to generate invoices, timesheets and rental agreement PDFs.
- Yard paperwork prints as PDFs, and the phone app can view PDFs without leaving the app.
- Runs inside the CodeIgniter API on the EC2 server, next to the data it prints.
mPDF is the right tool when a PHP application needs to produce printable documents from its own data, and the layout is easier to express in HTML than by drawing on a canvas. I reach for it because Rental Pilot's paperwork has to look the same every time it prints, and it can be generated right where the data lives.
Questions
Can my software generate its own invoices as PDFs?
Yes. Rental Pilot's API builds invoices, timesheets and rental agreements as PDFs with mPDF, straight from the data in the system.
Why generate PDFs from HTML?
HTML and CSS are a familiar way to lay out tables and text, so documents are quicker to build and change. mPDF then turns that layout into a fixed page that prints the same every time.