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.
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.
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.
// 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.
The Cell function accepts several arguments that control the properties of the cell and the text inside it:
$w)$h)$text)$border)$align)$fill)$link)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.
The text to be written in the cell is defined by the $text parameter.
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 borderTM: Top-middle borderTR: Top-right borderBL: Bottom-left borderBM: Bottom-middle borderBR: Bottom-right borderThe $align parameter accepts the following values:
L: Left-alignedC: CenteredR: Right-alignedThe $fill parameter accepts the following values:
F: FilledT: Filled with a pattern (default)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.
// Create a clickable link
$pdf->Cell($w = 40, $h = 7, 'Google', $border = 0, $link = 1);Let's create a simple invoice using the cell function.
// 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();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! π―π‘