Master PDF Generation in PHP with mPDF: Installation, Features, and Code Samples

This guide introduces the mPDF PHP library, outlines its key features, system requirements, installation steps, and provides detailed code examples for basic usage, HTML file rendering, handling Chinese characters, font settings, and template-based PDF creation.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Master PDF Generation in PHP with mPDF: Installation, Features, and Code Samples

Overview

mPDF is a powerful PHP library that converts UTF-8 encoded HTML into PDF files. Developed by Lan Back and released under GNU GPL v2, it builds on FPDF and HTML2FPDF and is widely used for PDF generation in PHP projects.

Features

Advanced CSS support : most CSS properties are handled.

Images and HTML content : embed images and HTML directly.

Multi‑column layout : create newspaper‑style columns.

Form creation : generate and process PDF forms.

Watermarks and backgrounds : add page watermarks and background images.

System Requirements

PHP 5.6–7.2 for mPDF 7.0; PHP 7.3+ also supported.

Required extensions: mbstring and gd.

Optional extensions for advanced features: zlib, bcmath, xml, etc.

Installation

composer require mpdf/mpdf

Basic Usage

<?php
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('<h1>Hello world!</h1>');
$mpdf->Output();

This outputs a PDF with Content‑Type application/pdf.

Using an HTML file

<?php
declare(strict_types=1);
use Mpdf\Mpdf;
use Mpdf\Output\Destination;
require_once __DIR__ . '/../vendor/autoload.php';
$html = file_get_contents('./pdf01.html');
$mpdf = new Mpdf();
$mpdf->WriteHTML($html);
$mpdf->Output('./pdf01.pdf', Destination::FILE);

Handling Chinese characters and symbols

$mpdf = new Mpdf([
    'autoScriptToLang' => true,
    'autoLangToFont'   => true,
    'useSubstitutions' => true,
]);

After enabling these options the output renders correctly.

Setting font and page size

$mpdf = new Mpdf([
    'autoScriptToLang' => true,
    'autoLangToFont'   => true,
    'useSubstitutions' => true,
    'mode'             => 'UTF-8',
    'format'           => 'A4',
    'default_font_size'=> 16,
    'default_font'     => '宋体',
]);

Using a PHP template

<?php
declare(strict_types=1);
use Mpdf\Mpdf;
use Mpdf\Output\Destination;
require_once __DIR__ . '/../vendor/autoload.php';
$template = file_get_contents('./pdf03.html');
$search  = ['{%username%}', '{%age%}', '{%address%}'];
$replace = ['Tinywan', date('Y-m-d H:i:s'), '浙江省杭州市西湖区梦想小镇'];
$html = str_replace($search, $replace, $template);
$mpdf = new Mpdf([
    'autoScriptToLang' => true,
    'autoLangToFont'   => true,
    'useSubstitutions' => true,
    'format'           => 'A4',
    'default_font_size'=> 14,
    'default_font'     => '宋体',
]);
$mpdf->WriteHTML($html);
$mpdf->Output('./pdf03.pdf', Destination::FILE);

Conclusion

mPDF offers a comprehensive set of features for PHP developers needing PDF output. While its CSS support may lag behind newer tools, its stability and rich functionality make it a solid choice for server‑side PDF generation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPCode ExamplesLibrarypdf-generationmPDF
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.