FPDF - Rectangle


We can draw Rectangle in pdf documents by using Rect() function in FPDF.

Syntax
Rect($x,$y,$width,$height,$style)

The following example shows, how to

  • Draw simple Rectangle
  • Change Rectangle Border Width
  • Rectangle with Background Color
  • Rectangle with Border Color
  • Rectangle with Background and Border Color
Example
<?php
  require('fpdf/fpdf.php');

  $pdf=new FPDF("P","mm","A4");
  
  $pdf->AddPage();
  
  //Simple Rectangle
  $pdf->Rect(10,15,190,20);

  //Change Border Width
  $pdf->SetLineWidth(1);
  $pdf->Rect(10,45,190,20);
  
  //Rectangle with Background color
  $pdf->SetFillColor(0,0,255);
  $pdf->Rect(10,75,190,20,"F");
  
  //Rectangle with Border Color
  $pdf->SetDrawColor(255,0,0);
  $pdf->Rect(10,105,190,20,"D");
  
  //Rectangle with Background and Border Color
  $pdf->SetFillColor(0,0,255);
  $pdf->SetDrawColor(255,0,0);
  $pdf->Rect(10,135,190,20,"DF");
  
  $pdf->Output();
?>
Output