HTML and CSS to PDF in PHP: A Complete 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 You Might Need to Convert HTML to PDF?

\n

Imagine this scenario: you've created a beautiful invoice on a website, laid it out in HTML, added styles, and everything looks perfect. But the client asks for a PDF. Or you need to generate a report with charts, tables, and complex layout. Manually drawing PDFs is a thing of the past. It's much easier to write HTML+CSS and then programmatically convert it to PDF.

\n

The question "How Can I add HTML And CSS Into PDF" on Stack Overflow has gathered thousands of views — which means this problem is relevant for many developers. In this article, we'll cover three of the most popular ways to do this in PHP, show code examples, and help you choose the best tool for your task.

\n\n

Library Comparison: Dompdf, TCPDF, and mPDF

\n

Before writing code, let's figure out what to choose. There are three main "pillars" in the PHP market for converting HTML to PDF:

\n
    \n
  • Dompdf — a lightweight library that understands CSS 2.1 and partially CSS 3. Ideal for simple documents.
  • \n
  • TCPDF — powerful, stable, but requires more code for complex layouts. Not very CSS-friendly.
  • \n
  • mPDF — the best choice if you need support for complex CSS (flexbox, gradients, transparency). The most "CSS-friendly" library.
  • \n
\n

In this article, we'll focus on mPDF as the most convenient option, but we'll also show examples with Dompdf for comparison.

\n\n

Installing Libraries via Composer

\n

First, let's create a project and install dependencies. Open your terminal and run:

\n
composer require mpdf/mpdf
\n

If you want to try Dompdf:

\n
composer require dompdf/dompdf
\n

Now, create a file generate_pdf.php in the project root and start writing code.

\n\n

Example 1: Simple HTML to PDF with mPDF

\n

Let's start with the most basic example. Suppose we have an HTML invoice template:

\n
<!DOCTYPE html>\n<html>\n<head>\n    <style>\n        body { font-family: Arial, sans-serif; }\n        .invoice { max-width: 800px; margin: 0 auto; }\n        .header { background: #4CAF50; color: white; padding: 20px; }\n        .total { font-size: 24px; font-weight: bold; }\n    </style>\n</head>\n<body>\n    <div class="invoice">\n        <div class="header">Invoice No. 123 dated 03/01/2025</div>\n        <p>Client: Ivan Petrov</p>\n        <table border="1" width="100%">\n            <tr><th>Service</th><th>Price</th></tr>\n            <tr><td>Website Development</td><td>50,000 RUB</td></tr>\n            <tr><td>SEO Promotion</td><td>15,000 RUB</td></tr>\n        </table>\n        <div class="total">Total: 65,000 RUB</div>\n    </div>\n</body>\n</html>
\n

Now the PHP code for generating the PDF:

\n
<?php\nrequire_once __DIR__ . '/vendor/autoload.php';\n\n$html = file_get_contents('invoice.html'); // or just an HTML string\n\n$mpdf = new \\Mpdf\\Mpdf();\n$mpdf->WriteHTML($html);\n$mpdf->Output('invoice.pdf', 'I'); // 'I' — open in browser, 'D' — download\n?>
\n

Run the script — and the PDF is ready! Note: mPDF automatically picked up the CSS from <style>. This is the answer to the question "How Can I add HTML And CSS Into PDF" — simply pass HTML with CSS to WriteHTML().

\n\n

Example 2: Working with External CSS Files

\n

Often styles are stored in separate files. mPDF supports linking external CSS via <link>:

\n
<?php\n$html = '\n<!DOCTYPE html>\n<html>\n<head>\n    <link rel="stylesheet" type="text/css" href="style.css">\n</head>\n<body>\n    <h1>Report for M                

Blogs

Book Recommendations