Skip to content

PHP FPDF library Example to Add Shadow to Text Cell inside PDF Document

This extension contributes a shadow effect to the text contained in a cell. The ShadowCell() method has the same parameters as Cell(), plus the following ones:

color : color of the shadow. Can be either a string (G for grey, B for black) or an integer between 0 and 255 (greyscale value). Default value: G.

distance : distance between the shadow and the text. Default value: 0.5.

shadow.php

<?php
require('fpdf.php');

class PDF_Shadow extends FPDF
{
    function ShadowCell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='', $color='G', $distance=0.5)
    {
        if($color=='G')
            $ShadowColor = 100;
        elseif($color=='B')
            $ShadowColor = 0;
        else
            $ShadowColor = $color;
        $TextColor = $this->TextColor;
        $x = $this->x;
        $this->SetTextColor($ShadowColor);
        $this->Cell($w, $h, $txt, $border, 0, $align, $fill, $link);
        $this->TextColor = $TextColor;
        $this->x = $x;
        $this->y += $distance;
        $this->Cell($w, $h, $txt, 0, $ln, $align);
    }
}
?>

index.php

<?php
require('shadow.php');

$pdf = new PDF_Shadow();
$pdf->SetFont('Arial','',30);

$pdf->AddPage();    
$pdf->SetTextColor(255,  255, 255);
for ($i = 1; $i < 6; $i++) {
    $Text = sprintf('Gray shadow with %.1F distance', $i / 2);
    $pdf->ShadowCell(0, 40, $Text, 1, 1, 'C', true, '', 'G', $i / 2);
    $pdf->Ln(10);
}

$pdf->AddPage();    
$pdf->SetTextColor(0, 0, 255);
for ($i = 1; $i < 6; $i++) {
    $Text = sprintf('Black shadow with %.1F distance', $i / 2);
    $pdf->ShadowCell(0, 40, $Text, 1, 1, 'C', false, '', 'B', $i / 2);
    $pdf->Ln(10);
}

$pdf->Output();
?>