How to Add HTML and CSS to PDF: PHP Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why HTML+CSS to PDF is a Headache (and How to Solve It)



Imagine: you're developing a website in PHP, and the client asks: "Make the 'Download PDF' button export beautiful invoices, reports, or certificates." You think: "Easy! I'll take an HTML template, add some CSS styles, and convert it to PDF." But reality turns out to be more complicated: fonts shift, tables break apart, and the CSS property flexbox works inconsistently.



On Stack Overflow, the question "How can I add HTML and CSS into PDF" has gathered thousands of views. This means the problem is familiar to almost every PHP developer. In this article, we'll cover three popular libraries — Dompdf, TCPDF, and mPDF — and show you how to properly convert HTML with CSS to PDF in PHP.



You will learn:

  • Which library to choose for different tasks
  • How to include CSS styles (external, inline, @import)
  • How to solve common issues: Cyrillic characters, tables, images
  • How to write working code for generating PDF from HTML


Why Can't You Just Save HTML as PDF?



Browsers render HTML+CSS perfectly, but PDF generators are not browsers. They use their own rendering engines that do not support all CSS properties. For example:

  • flexbox and grid — don't work everywhere
  • border-radius — partially supported
  • @media print — may be ignored
  • Fonts (especially Cyrillic) — require embedding


The good news: modern PHP libraries are constantly improving, and with the right approach, you can get a PDF that looks almost like in a browser.



Overview of Libraries for Converting HTML+CSS to PDF in PHP



Let's look at three of the most popular tools. Each has its own strengths.



1. Dompdf

Pros: lightweight, easy to install via Composer, good support for CSS 2.1 and partial CSS3. Great for simple documents — invoices, receipts.

Cons: weak support for complex layouts (flexbox, grid), issues with large amounts of HTML.



2. TCPDF

Pros: stable, powerful, supports Unicode (Cyrillic), works without external libraries. Ideal for complex PDF files with charts, QR codes, multi-page tables.

Cons: complex syntax, CSS styles need to be included manually, code can be bulky.



3. mPDF

Pros: best CSS support (including @media, float, position), excellent Cyrillic support, supports styles from external files. Considered the "sweet spot".

Cons: heavier than Dompdf, can be slow on large documents.



Recommendation: For a quick start, use Dompdf. For serious projects — mPDF. TCPDF — if you need maximum flexibility and are willing to write more code.



Practical Example: Generating PDF from HTML with Dompdf



Let's write some real code. Install Dompdf via Composer:



composer require dompdf/dompdf


Now create a PHP script that converts an HTML template to PDF:



<?phprequire_once 'vendor/autoload.php';

use Dompdf\\Dompdf;use Dompdf\\Options;

// Settings$options = new Options();$options->set('defaultFont', 'DejaVu Sans'); // Cyrillic support$options->set('isRemoteEnabled', true); // Allow external images

$dompdf = new Dompdf($options);

// HTML template$html = '<!DOCTYPE html><html><head> <style> body { font-family: DejaVu Sans, sans-serif; } .invoice { width: 100%; max-width: 80

Blogs

Book Recommendations