All formats

TCPDF

LanguagePHP 8.2+ (TCPDF 7)
LicenseLGPL-3.0-or-later
Repositorygithub.com/tecnickcom/TCPDF
Documentationtcpdf.org, font setup
Depends ontecnickcom/tc-lib-pdf and its modules (version 7), ext-curl; ext-gd or Imagick for some image formats
Read / writeno / yes

TCPDF is one of the most used PHP PDF generators: it writes text with Unicode TrueType fonts, a subset of HTML and CSS (writeHTML), images, vector graphics, 1D and 2D barcodes (QR, DataMatrix, PDF417), AcroForm fields, bookmarks, encryption and digital signatures, and supports PDF/A.

TCPDF is deprecated and in maintenance-only mode. Since version 7 the TCPDF class is a compatibility facade over tc-lib-pdf, its modern successor: the public methods are the same, but output can paginate slightly differently, legacy PHP font files are no longer supported, and fonts are generated from the tc-lib font package at install time. Start new projects with tc-lib-pdf; keep TCPDF for existing code. It can’t read or modify existing PDFs.

Installation#

Shell
composer require tecnickcom/tcpdf
# TCPDF 7: generate the font files once (needs make, or run the PHP script directly)
make -C vendor/tecnickcom/tc-lib-pdf-font deps fonts

Without the font step, the first page fails with unable to read file: helvetica.json. To run the generator on every composer install, add it to the scripts section of your composer.json as shown in the TCPDF README.

Basic usage: create a PDF with text#

PHP
<?php
require 'vendor/autoload.php';

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator('My app');
$pdf->SetAuthor('Me');
$pdf->SetTitle('Hello');
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

$pdf->AddPage();
$pdf->SetFont('helvetica', 'B', 24);
$pdf->Cell(0, 15, 'Hello, PDF!', 0, 1);

// DejaVu Sans covers most Latin, Greek and Cyrillic characters
$pdf->SetFont('dejavusans', '', 12);
$pdf->Write(0, "Zażółć gęślą jaźń · Привет\n");

$pdf->Output(__DIR__ . '/hello.pdf', 'F');  // 'I' inline to browser, 'D' download, 'S' return string

Units are millimeters by default (PDF_UNIT), the page size is A4.

How to convert HTML to PDF with TCPDF#

PHP
$html = '<h2>Invoice #1001</h2>
<table border="1" cellpadding="4">
  <tr><th>Item</th><th>Qty</th><th>Price</th></tr>
  <tr><td>Widget</td><td align="right">2</td><td align="right">9.99</td></tr>
</table>';
$pdf->writeHTML($html, true, false, true, false, '');

TCPDF supports a limited set of HTML tags and inline CSS. For full CSS layouts, use Dompdf or mPDF.

How to add barcodes and images#

PHP
$style = ['border' => false, 'padding' => 0, 'fgcolor' => [0, 0, 0], 'bgcolor' => false];
$pdf->write2DBarcode('https://example.com', 'QRCODE,M', 20, 20, 40, 40, $style);
$pdf->write1DBarcode('1234567890128', 'EAN13', 20, 70, 80, 20);
$pdf->Image(__DIR__ . '/logo.png', 20, 100, 60);  // x, y, width in mm

How to create a fillable form#

PHP
$pdf->SetFont('helvetica', '', 12);
$pdf->Cell(30, 8, 'Name:');
$pdf->TextField('name', 60, 8);
$pdf->Ln(12);
$pdf->Cell(30, 8, 'Agree:');
$pdf->CheckBox('agree', 5, false);

How to password-protect a PDF#

SetProtection must be called before the first page. Mode 3 is AES-256:

PHP
$pdf = new TCPDF();
$pdf->SetProtection(['print', 'copy'], 'user', 'owner', 3);
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 12);
$pdf->Cell(0, 10, 'Secret');
$pdf->Output(__DIR__ . '/secret.pdf', 'F');

Tips#