PHP FPDF Cell Tutorial πŸ“

beginner
11 min

PHP FPDF Cell Tutorial πŸ“

Welcome to our comprehensive guide on using the FPDF cell function in PHP! This tutorial is designed for beginners and intermediates, so let's dive right in.

What is FPDF? 🎯

FPDF is a popular PHP library that allows you to create PDF documents dynamically from PHP scripts. It's a powerful tool for generating invoices, reports, certificates, and other types of documents.

Understanding the FPDF cell Function πŸ“

The cell function is one of the most commonly used functions in FPDF. It allows you to write text with different fonts, colors, and alignments in a cell of a table.

php
// Create a new FPDF document $pdf = new FPDF(); // Add a page $pdf->AddPage(); // Define the text, font, color, and position $text = 'Hello, World!'; $font = 'Arial'; $color = '000'; // RGB: 0-255 $x = 10; $y = 10; // Write the text using the cell function $pdf->SetTextColor($color); $pdf->SetFont($font, '', 14); $pdf->Cell($w = 40, $h = 7, $text, 0, 0, 'L');

πŸ’‘ Pro Tip: The SetTextColor, SetFont, and Cell functions are used together to write text with the specified color and font.

Table Cell Properties πŸ“

The Cell function accepts several arguments that control the properties of the cell and the text inside it:

  1. Width ($w)
  2. Height ($h)
  3. Text ($text)
  4. Border ($border)
  5. Alignment ($align)
  6. Fill ($fill)
  7. Link ($link)

Width and Height

The width and height of the cell are defined by the $w and $h parameters, respectively. The width is measured in units of the current font size.

Text

The text to be written in the cell is defined by the $text parameter.

Border, Alignment, and Fill

The $border, $align, and $fill parameters control the borders, alignment, and fill color of the cell, respectively. The $border parameter accepts a combination of the following values:

  • TL: Top-left border
  • TM: Top-middle border
  • TR: Top-right border
  • BL: Bottom-left border
  • BM: Bottom-middle border
  • BR: Bottom-right border

The $align parameter accepts the following values:

  • L: Left-aligned
  • C: Centered
  • R: Right-aligned

The $fill parameter accepts the following values:

  • F: Filled
  • T: Filled with a pattern (default)

Link

The $link parameter allows you to create clickable links within your PDF. The URL or the destination page should be specified within the $link parameter.

php
// Create a clickable link $pdf->Cell($w = 40, $h = 7, 'Google', $border = 0, $link = 1);

Real-World Example πŸ“

Let's create a simple invoice using the cell function.

php
// Create a new FPDF document $pdf = new FPDF(); // Add a page $pdf->AddPage(); // Set the font and font size $pdf->SetFont('Arial', '', 14); // Write the invoice details $pdf->Cell(40, 7, 'Invoice #: 123456', 0, 0, 'L'); $pdf->Cell(40, 7, 'Date: January 1, 2023', 0, 0, 'L'); $pdf->Cell(70, 7, 'Client Name: John Doe', 0, 0, 'L'); $pdf->Ln(10); // Line break // Write the item details $pdf->Cell(20, 7, 'Item', 1, 0, 'C'); $pdf->Cell(30, 7, 'Description', 1, 0, 'C'); $pdf->Cell(20, 7, 'Quantity', 1, 0, 'C'); $pdf->Cell(30, 7, 'Price', 1, 0, 'C'); $pdf->Cell(30, 7, 'Total', 1, 1, 'C'); // Write the item details $pdf->Cell(20, 7, '1', 0, 0, 'C'); $pdf->Cell(30, 7, 'Product A', 0, 0, 'C'); $pdf->Cell(20, 7, '5', 0, 0, 'C'); $pdf->Cell(30, 7, '$100', 0, 0, 'C'); $pdf->Cell(30, 7, '$500', 0, 1, 'C'); $pdf->Cell(20, 7, '2', 0, 0, 'C'); $pdf->Cell(30, 7, 'Product B', 0, 0, 'C'); $pdf->Cell(20, 7, '3', 0, 0, 'C'); $pdf->Cell(30, 7, '$200', 0, 0, 'C'); $pdf->Cell(30, 7, '$600', 0, 1, 'C'); // Write the total and save the PDF $pdf->Cell(50, 7, 'Total: $1,100', 0, 0, 'R'); $pdf->Output();

Quiz 🎯

Quick Quiz
Question 1 of 1

What is the purpose of the `Cell` function in FPDF?

That's it for today! We hope you've enjoyed learning about the FPDF cell function. Stay tuned for more in-depth PHP tutorials here at CodeYourCraft. Happy coding! πŸŽ―πŸ’‘