mPDF

A pure PHP library that renders UTF-8 HTML and CSS into PDF documents, with built-in CJK font support, barcodes, and tables of contents.

Library
Composer
vv8.3.1
4,701stars
GNU GPLv2

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
65/100Good
Development Activity36
Maintenance32
Community92
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
74/100Good
Architecture72
Code Quality76
Innovation68
Learning Curve78

mPDF is a PHP library that generates PDF files directly from UTF-8-encoded HTML, built on top of FPDF and HTML2FPDF with substantial enhancements. It supports a wide subset of CSS for layout and styling, along with headers/footers, page numbering, tables of contents, watermarks, form fields, and multiple barcode formats, all without requiring an external rendering engine, browser, or binary dependency.

Created by Ian Back and released under GPL-2.0, mPDF has become one of the most widely used PHP PDF-generation libraries, valued in particular for its strong support of complex scripts and CJK (Chinese/Japanese/Korean) typography that many competing HTML-to-PDF tools handle poorly. It integrates via Composer with setasign/fpdi for merging existing PDFs and supports PSR-3 logging and PSR-7 HTTP message interfaces.

What You Get

  • HTML-to-PDF conversion supporting a substantial subset of CSS2/CSS3 for layout and styling
  • Strong CJK (Chinese/Japanese/Korean) and right-to-left/complex-script typography support
  • Built-in headers, footers, automatic page numbering, and table-of-contents generation
  • Barcode generation (Barcode.php) in several common formats without external dependencies
  • PDF form field support and existing-PDF merging via the bundled setasign/fpdi dependency
  • PSR-3 logging and PSR-7 HTTP message interface support for framework integration

Common Use Cases

  • Generating invoices, receipts, and reports as downloadable PDFs from an application’s existing HTML templates
  • Producing multilingual documents that require correct CJK or right-to-left script rendering
  • Building printable certificates, tickets, or labels that include barcodes alongside styled text
  • Server-side PDF export features in CMS, e-commerce, or ERP-style PHP applications

Under The Hood

Architecture: The Mpdf class (src/Mpdf.php, the largest file in the codebase) orchestrates HTML parsing, CSS resolution via CssManager.php and the Css/ directory, and low-level PDF byte-stream writing inherited from the FPDF lineage; font handling, color space conversion, and asset fetching are split into dedicated Fonts/, Color/, and AssetFetcher.php components so HTML-to-PDF conversion is a pipeline of parse → style-resolve → layout → write rather than one monolithic routine.

Tech Stack: It’s a pure PHP library (655 source files) supporting PHP 5.6 through 8.5, depending on setasign/fpdi for low-level PDF primitives and PDF merging, myclabs/deep-copy for object cloning, and PSR interfaces (psr/log, psr/http-message) for framework-agnostic integration; the ext-gd and ext-mbstring PHP extensions are required for image handling and multibyte string support respectively.

Code Quality: Testing is unusually extensive for a PDF library, with roughly 200 test files under tests/Mpdf/ covering individual features (fonts, tables, forms, barcodes) plus a Snapshots/ directory for visual/output regression testing and an Issues/ directory with regression tests tied to specific historical bug reports. Static analysis is enforced via squizlabs/php_codesniffer, though the maintenance cadence has slowed (release_frequency ~0.04/week, 3 commits/month) relative to its large contributor base.

API Design: A minimal example — new Mpdf(); $mpdf->WriteHTML($html); $mpdf->Output(); — gets a working PDF from existing HTML/CSS with almost no new API to learn, though achieving pixel-perfect layouts requires understanding mPDF’s specific (not full-spec) CSS support and its FPDF-derived method naming conventions, which read as dated compared to more modern PHP libraries.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search