Convert Figma logo to code with AI

mpdf logompdf

PHP library generating PDF files from UTF-8 encoded HTML

4,385
1,064
4,385
326

Top Related Projects

4,186

Official clone of PHP library to generate PDF documents and barcodes

10,495

HTML to PDF converter for PHP

7,241

A pure PHP library for reading and writing word processing documents

A DOMPDF Wrapper for Laravel

A slim PHP wrapper around wkhtmltopdf with an easy to use and clean OOP interface

Quick Overview

mPDF is a PHP library for generating PDF files from HTML with CSS styling. It supports a wide range of HTML and CSS features, including tables, images, and custom fonts. mPDF is particularly useful for creating complex, multi-page documents with rich formatting.

Pros

  • Extensive HTML and CSS support, allowing for highly customizable PDF output
  • Built-in support for various languages and scripts, including right-to-left text
  • Ability to generate headers, footers, and page numbers automatically
  • Active development and community support

Cons

  • Can be resource-intensive for large documents or complex layouts
  • Learning curve may be steep for users new to PDF generation
  • Some advanced CSS features may not be fully supported
  • Performance may be slower compared to other PDF generation libraries

Code Examples

  1. Basic PDF generation:
<?php
require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('<h1>Hello World</h1>');
$mpdf->Output('document.pdf', 'F');
  1. Adding custom styles:
<?php
$mpdf = new \Mpdf\Mpdf();
$stylesheet = file_get_contents('styles.css');
$mpdf->WriteHTML($stylesheet, \Mpdf\HTMLParserMode::HEADER_CSS);
$mpdf->WriteHTML('<div class="custom-style">Styled content</div>');
$mpdf->Output();
  1. Creating a table of contents:
<?php
$mpdf = new \Mpdf\Mpdf(['mode' => 'utf-8', 'format' => 'A4']);
$mpdf->WriteHTML('<h1>Chapter 1</h1>', \Mpdf\HTMLParserMode::HTML_BODY);
$mpdf->TOC_Entry('Chapter 1', 1);
$mpdf->WriteHTML('<h2>Section 1.1</h2>', \Mpdf\HTMLParserMode::HTML_BODY);
$mpdf->TOC_Entry('Section 1.1', 2);
$mpdf->AddPage();
$mpdf->WriteHTML('<tocpagebreak />');
$mpdf->Output();

Getting Started

To start using mPDF, follow these steps:

  1. Install mPDF using Composer:

    composer require mpdf/mpdf
    
  2. Create a new PHP file and add the following code:

    <?php
    require_once __DIR__ . '/vendor/autoload.php';
    
    $mpdf = new \Mpdf\Mpdf();
    $mpdf->WriteHTML('<h1>My First PDF</h1>');
    $mpdf->Output('my_first_pdf.pdf', 'F');
    
  3. Run the PHP file to generate your first PDF.

For more advanced usage and configuration options, refer to the official mPDF documentation.

Competitor Comparisons

4,186

Official clone of PHP library to generate PDF documents and barcodes

Pros of TCPDF

  • More extensive documentation and examples
  • Better support for complex layouts and advanced PDF features
  • Wider range of supported image formats

Cons of TCPDF

  • Steeper learning curve due to more complex API
  • Slower performance, especially for large documents
  • Less active development and community support

Code Comparison

TCPDF:

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 12);
$pdf->Cell(0, 10, 'Hello World!', 0, 1, 'C');
$pdf->Output('example.pdf', 'F');

mPDF:

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('<h1>Hello World!</h1>');
$mpdf->Output('example.pdf', 'F');

TCPDF offers more granular control over PDF creation, while mPDF provides a simpler API for basic tasks. TCPDF requires more code for the same output, but offers greater flexibility for complex layouts. mPDF's HTML-based approach is more intuitive for web developers, making it easier to generate PDFs from existing HTML content.

10,495

HTML to PDF converter for PHP

Pros of dompdf

  • Simpler setup and usage, especially for basic PDF generation
  • Better support for CSS3 and HTML5 features
  • Lighter weight and potentially faster for simple documents

Cons of dompdf

  • Less robust handling of complex layouts and large documents
  • Limited support for advanced PDF features like forms and digital signatures
  • May struggle with certain non-Latin character sets and right-to-left languages

Code Comparison

dompdf example:

require_once 'dompdf/autoload.inc.php';
$dompdf = new Dompdf();
$dompdf->loadHtml('<h1>Hello, World!</h1>');
$dompdf->render();
$dompdf->stream("document.pdf");

mpdf example:

require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('<h1>Hello, World!</h1>');
$mpdf->Output("document.pdf", "D");

Both libraries offer straightforward ways to generate PDFs from HTML, but mpdf generally provides more advanced features and better handling of complex documents at the cost of a steeper learning curve and potentially slower performance for simple tasks.

7,241

A pure PHP library for reading and writing word processing documents

Pros of PHPWord

  • Supports creation and editing of Word documents (.docx, .doc, .odt)
  • Offers more advanced formatting options for text, tables, and images
  • Provides better integration with Microsoft Office suite

Cons of PHPWord

  • Larger library size and potentially higher memory usage
  • Steeper learning curve due to more complex API
  • Limited PDF generation capabilities compared to MPDF

Code Comparison

MPDF:

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('<h1>Hello World</h1>');
$mpdf->Output('document.pdf', 'F');

PHPWord:

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addText('Hello World', ['size' => 16, 'bold' => true]);
$writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$writer->save('document.docx');

MPDF is focused on HTML-to-PDF conversion, making it simpler for PDF generation from HTML content. PHPWord offers more flexibility in creating and manipulating Word documents programmatically but requires more code for basic tasks. MPDF is generally easier to use for PDF creation, while PHPWord excels in Word document manipulation and advanced formatting options.

A DOMPDF Wrapper for Laravel

Pros of laravel-dompdf

  • Seamless integration with Laravel framework
  • Utilizes the popular DomPDF library, which has good HTML/CSS support
  • Lightweight and easy to set up in Laravel projects

Cons of laravel-dompdf

  • Limited support for complex layouts and advanced CSS features
  • May have performance issues with large documents or heavy styling
  • Less actively maintained compared to mpdf

Code Comparison

laravel-dompdf:

use Barryvdh\DomPDF\Facade\Pdf;

$pdf = Pdf::loadView('pdf.invoice', $data);
return $pdf->download('invoice.pdf');

mpdf:

$mpdf = new \Mpdf\Mpdf();
$html = view('pdf.invoice', $data)->render();
$mpdf->WriteHTML($html);
$mpdf->Output('invoice.pdf', 'D');

Both libraries offer straightforward ways to generate PDFs from HTML, but laravel-dompdf provides a more Laravel-specific implementation with its facade. mpdf, on the other hand, offers a more flexible approach that can be easily integrated into various PHP projects.

While laravel-dompdf excels in Laravel environments, mpdf generally provides better support for complex layouts and advanced features. The choice between the two depends on the specific project requirements, with laravel-dompdf being ideal for simpler Laravel-based PDF generation tasks, and mpdf offering more robust capabilities for complex PDF needs across different PHP applications.

A slim PHP wrapper around wkhtmltopdf with an easy to use and clean OOP interface

Pros of phpwkhtmltopdf

  • Utilizes wkhtmltopdf, which offers better HTML and CSS rendering
  • Supports JavaScript execution in PDF generation
  • Generally faster for complex layouts and large documents

Cons of phpwkhtmltopdf

  • Requires external wkhtmltopdf binary installation
  • Limited font embedding options compared to mpdf
  • May have compatibility issues with some server environments

Code Comparison

mpdf:

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML($html);
$mpdf->Output('document.pdf', 'D');

phpwkhtmltopdf:

$pdf = new Pdf($html);
if (!$pdf->send()) {
    $error = $pdf->getError();
}

Both libraries offer straightforward PDF generation from HTML, but phpwkhtmltopdf relies on an external binary for rendering. mpdf is a pure PHP solution, which can be advantageous in certain environments. phpwkhtmltopdf excels in rendering complex layouts and supporting JavaScript, while mpdf provides better control over font embedding and doesn't require additional software installation. The choice between the two depends on specific project requirements, server constraints, and desired output quality.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

mPDF is a PHP library which generates PDF files from UTF-8 encoded HTML.

It is based on FPDF and HTML2FPDF (see CREDITS), with a number of enhancements. mPDF was written by Ian Back and is released under the GNU GPL v2 licence.

Latest Stable Version Total Downloads License

⚠ If you are viewing this file on mPDF GitHub repository homepage or on Packagist, please note that the default repository branch is development which can differ from the last stable release.

Requirements

PHP versions and extensions

  • PHP >=5.6 <7.3.0 is supported for mPDF >= 7.0
  • PHP 7.3 is supported since mPDF v7.1.7
  • PHP 7.4 is supported since mPDF v8.0.4
  • PHP 8.0 is supported since mPDF v8.0.10
  • PHP 8.1 is supported as of mPDF v8.0.13
  • PHP 8.2 is supported as of mPDF v8.1.3
  • PHP 8.3 is supported as of mPDF v8.2.1
  • PHP 8.4 is supported as of mPDF v8.2.5

PHP mbstring and gd extensions have to be loaded.

Additional extensions may be required for some advanced features such as zlib for compression of output and embedded resources such as fonts, bcmath for generating barcodes or xml for character set conversion and SVG handling.

Known server caveats

mPDF has some problems with fetching external HTTP resources with single threaded servers such as php -S. A proper server such as nginx (php-fpm) or Apache is recommended.

Support us

Consider supporting development of mPDF with a donation of any value. Donation button can be found on the main page of the documentation.

Installation

Official installation method is via composer and its packagist package mpdf/mpdf.

$ composer require mpdf/mpdf

Usage

The simplest usage (since version 7.0) of the library would be as follows:

<?php

require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('<h1>Hello world!</h1>');
$mpdf->Output();

This will output the PDF inline to the browser as application/pdf Content-type.

Setup & Configuration

All configuration directives can be set by the $config parameter of the constructor.

It is recommended to set one's own temporary directory via tempDir configuration variable. The directory must have write permissions (mode 775 is recommended) for users using mPDF (typically cli, webserver, fpm).

Warning: mPDF will clean up old temporary files in the temporary directory. Choose a path dedicated to mPDF only.

<?php

$mpdf = new \Mpdf\Mpdf(['tempDir' => __DIR__ . '/tmp']);

By default, the temporary directory will be inside vendor directory and will have write permissions from post_install composer script.

For more information about custom temporary directory see the note on Folder for temporary files in the section on Installation & Setup in the manual.

If you have problems, please read the section on troubleshooting in the manual.

About CSS support and development state

mPDF as a whole is a quite dated software. Nowadays, better alternatives are available, albeit not written in PHP.

Use mPDF if you cannot use non-PHP approach to generate PDF files or if you want to leverage some of the benefits of mPDF over browser approach – color handling, pre-print, barcodes support, headers and footers, page numbering, TOCs, etc. But beware that a HTML/CSS template tailored for mPDF might be necessary.

If you are looking for state of the art CSS support, mirroring existing HTML pages to PDF, use headless Chrome.

mPDF will still be updated to enhance some internal capabilities and to support newer versions of PHP, but better and/or newer CSS support will most likely not be implemented.

Online manual

Online manual is available at https://mpdf.github.io/.

General troubleshooting

For general questions or troubleshooting please use Discussions.

You can also use the mpdf tag at Stack Overflow as the StackOverflow user base is more likely to answer you in a timely manner.

Contributing

Before submitting issues and pull requests please read the CONTRIBUTING.md file.

Unit Testing

Unit testing for mPDF is done using PHPUnit.

To get started, run composer install from the command line while in the mPDF root directory (you'll need composer installed first).

To execute tests, run composer test from the command line while in the mPDF root directory.

Any assistance writing unit tests for mPDF is greatly appreciated. If you'd like to help, please note that any PHP file located in the /tests/ directory will be autoloaded when unit testing.