Install mPDF with Composer
Written for mPDF 8.2 on PHP 8.1+. Updated 2 November 2026.
mPDF is a PHP library that turns HTML and CSS into PDF files. It is installed with Composer, PHP’s package manager. The old ZIP downloads from the mPDF 5 and 6 days are obsolete and won’t run on current PHP.
Requirements
- PHP 7.4 or newer is the practical baseline for a new project. mPDF 8.2 still installs on older versions, but those no longer get security fixes.
- The mbstring and gd extensions are required. gd handles images.
- zlib is strongly recommended: it compresses embedded fonts and images.
- bcmath is needed only for some barcode types, and xml for SVG images.
Check what you have with php -m.
Install
From your project folder:
composer require mpdf/mpdf
This adds mPDF and its dependencies to vendor/. The package page on Packagist shows the latest release.
Your first PDF
<?php
require __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf([
'tempDir' => sys_get_temp_dir() . '/mpdf',
]);
$mpdf->WriteHTML('<h1>Hello from mPDF</h1><p>This paragraph is real, selectable text.</p>');
$mpdf->Output('hello.pdf', \Mpdf\Output\Destination::INLINE);
Open the script in a browser and the PDF appears in the browser’s viewer.
Set a writable temp directory
mPDF caches font data and processed images on disk. By default it writes inside vendor/mpdf/mpdf/tmp, which often isn’t writable on servers, and it’s wiped on every deploy. Point tempDir at a folder the web server can write to, as above, and the “temporary files directory is not writable” error goes away.
Coming from mPDF 5 or 6?
Old code looks like include 'mpdf.php'; $mpdf = new mPDF('utf-8', 'A4');. In mPDF 7 and 8:
- The class is namespaced:
new \Mpdf\Mpdf([...]). - It takes one array of settings instead of positional arguments.
- It is loaded through Composer’s autoloader.
The configuration guide maps the old arguments to the new keys.
Next steps
- Configuration options: page size, margins and fonts.
- Output: download, save or return the PDF as a string.
- Troubleshooting, if something doesn’t work.