Generating PDF Files in PHP Using TCPDF: A Step-by-Step Guide

Generating PDF Files in PHP Using TCPDF: A Step-by-Step Guide

Introduction

Generating PDF files dynamically is a common requirement in invoices, reports, receipts, and certificates. PHP offers multiple libraries for this, and TCPDF is one of the most powerful, open-source PDF generation libraries available.

With TCPDF, you can:

Create PDFs dynamically from PHP code
Add text, images, tables, and custom formatting
Generate invoices, reports, and receipts easily
Save, download, or email generated PDFs

This guide will walk you through installing TCPDF, creating a basic PDF, and customizing it with images, tables, and styles.

1. Installing TCPDF in PHP

TCPDF can be installed manually or via Composer.

Install via Composer (Recommended)

composer require tecnickcom/tcpdf

After installation, include TCPDF in your PHP file:

require_once 'vendor/autoload.php';

Manual Installation

  1. Download TCPDF from GitHub.
  2. Extract it and place the tcpdf folder in your project directory.
  3. Include TCPDF in your PHP script:
require_once 'tcpdf/tcpdf.php';

Now, TCPDF is ready for use!

2. Creating a Basic PDF File in PHP

Let's start by generating a basic PDF with text.

Example: Creating a Simple PDF

require_once 'vendor/autoload.php';

$pdf = new TCPDF();
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 12);
$pdf->Cell(0, 10, 'Hello, this is a generated PDF!', 0, 1, 'C');
$pdf->Output('generated.pdf', 'I');

Explanation:

AddPage() – Creates a new blank page.
SetFont('helvetica', '', 12) – Sets font style and size.
Cell(0, 10, 'Text', 0, 1, 'C') – Adds text (centered).
Output('generated.pdf', 'I') – Displays the PDF in the browser.

🔹 Want to download the PDF instead? Change 'I' to 'D':

$pdf->Output('download.pdf', 'D'); // Forces download

3. Adding Images to a PDF in PHP

You can add logos, product images, or profile pictures inside PDFs.

Example: Adding an Image

$pdf = new TCPDF();
$pdf->AddPage();

// Add Image
$pdf->Image('logo.png', 15, 10, 40, 20, 'PNG');

// Add Text
$pdf->SetY(40); // Move text below image
$pdf->SetFont('helvetica', '', 14);
$pdf->Cell(0, 10, 'Company Invoice', 0, 1, 'C');

$pdf->Output('image_pdf.pdf', 'I');

Explanation:

Image('logo.png', x, y, width, height, 'PNG') – Places an image.
SetY(40) – Moves text below the image.

🔹 Supports multiple formats: JPEG, PNG, GIF, SVG.

4. Creating a Table in PDF (For Reports & Invoices)

Tables help organize invoice details, reports, and structured data.

Example: Adding a Table to a PDF

$pdf = new TCPDF();
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 12);

// Table Header
$pdf->Cell(50, 10, 'Product', 1, 0, 'C');
$pdf->Cell(50, 10, 'Quantity', 1, 0, 'C');
$pdf->Cell(50, 10, 'Price ($)', 1, 1, 'C');

// Table Rows
$pdf->Cell(50, 10, 'Laptop', 1, 0, 'C');
$pdf->Cell(50, 10, '1', 1, 0, 'C');
$pdf->Cell(50, 10, '1200', 1, 1, 'C');

$pdf->Cell(50, 10, 'Phone', 1, 0, 'C');
$pdf->Cell(50, 10, '2', 1, 0, 'C');
$pdf->Cell(50, 10, '800', 1, 1, 'C');

$pdf->Output('table_pdf.pdf', 'I');

Explanation:

Creates a table using Cell() with border and alignment.
First row is the header, remaining rows contain data.

🔹 Modify column widths to fit your content.

5. Adding Page Numbers and Headers

For multi-page PDFs, add page numbers and headers automatically.

Example: Custom Header & Footer in TCPDF

class CustomPDF extends TCPDF {
    // Header
    public function Header() {
        $this->SetFont('helvetica', 'B', 12);
        $this->Cell(0, 10, 'Company Report', 0, 1, 'C');
    }

    // Footer
    public function Footer() {
        $this->SetY(-15);
        $this->SetFont('helvetica', 'I', 10);
        $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().' of '.$this->getAliasNbPages(), 0, 0, 'C');
    }
}

$pdf = new CustomPDF();
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 12);
$pdf->Cell(0, 10, 'Report content goes here...', 0, 1);
$pdf->Output('header_footer.pdf', 'I');

Header() method adds a header to every page.
Footer() method adds page numbers dynamically.

🔹 Now, all pages will have headers and footers automatically!

6. Saving and Emailing a PDF File

Example: Save PDF to Server

$pdf->Output('invoices/invoice.pdf', 'F'); // Saves the file

Example: Email PDF as Attachment

$mail = new PHPMailer(true);
$mail->addAttachment('invoices/invoice.pdf');
$mail->send();

PDFs can be saved, emailed, or stored for later access.

Best Practices for PHP PDF Generation with TCPDF

Use SetFont() wisely – Avoid excessive font styles to reduce file size.
Optimize images before adding – Large images increase PDF size.
Use Output('D') for downloads – Prevent unnecessary storage.
Always set headers for multi-page documents – Ensures consistency.

Conclusion

With TCPDF, you can generate dynamic, customizable PDFs in PHP for invoices, reports, or documentation.

Create PDFs dynamically from PHP code
Add images, tables, headers, and footers
Save, email, or download PDF files

By following these best practices and examples, you can build efficient PDF generation features for any PHP application. 🚀

Leave a Reply