FPDF provides the Line() method to draw a straight line between two points in a PDF document. Supply the starting and ending X and Y coordinates:
Line(x1,y1,x2,y2);

The coordinate origin is at the upper-left corner of the page. X values increase from left to right, while Y values increase from top to bottom.
(0,0) --------------------> X
|
|
|
v
Y
Line() uses page coordinates directly. It does not depend on the current text cursor position used by methods such as Cell().
This example draws a line from coordinate (20,20) to (150,200).
<?php
require 'fpdf.php';
$pdf=new FPDF();
$pdf->AddPage();
$pdf->Line(20,20,150,200);
$pdf->Output('I','simple-line.pdf');
View Simple Line Output
For lines that depend on the page dimensions, read the current page width and height:
$width=$pdf->GetPageWidth();
$height=$pdf->GetPageHeight();
With the default FPDF constructor:
$pdf=new FPDF();
the normal unit is millimetres and the default page format is A4 portrait. Using GetPageWidth() and GetPageHeight() is preferable to hard-coding dimensions because the same drawing logic can adapt to other page sizes and orientations.
Draw one diagonal from the upper-left to the lower-right and another from the upper-right to the lower-left.
<?php
require 'fpdf.php';
$pdf=new FPDF();
$pdf->AddPage();
$width=$pdf->GetPageWidth();
$height=$pdf->GetPageHeight();
$pdf->Line(0,0,$width,$height);
$pdf->Line($width,0,0,$height);
$pdf->Output('I','cross-lines.pdf');
View Cross Line Output
Four lines can be combined to create a rectangular border. The $gap value controls the distance between the border and the page edge.
<?php
require 'fpdf.php';
$pdf=new FPDF();
$pdf->AddPage();
$width=$pdf->GetPageWidth();
$height=$pdf->GetPageHeight();
$gap=2;
$pdf->Line($gap,$gap,$width-$gap,$gap);
$pdf->Line($gap,$height-$gap,$width-$gap,$height-$gap);
$pdf->Line($gap,$gap,$gap,$height-$gap);
$pdf->Line($width-$gap,$gap,$width-$gap,$height-$gap);
$pdf->Output('I','page-border.pdf');
The four calls create:
A PHP for loop can repeatedly draw horizontal lines. Here the Y coordinate increases by 10 mm after each line.
<?php
require 'fpdf.php';
$pdf=new FPDF();
$pdf->AddPage();
$width=$pdf->GetPageWidth();
$height=$pdf->GetPageHeight();
for($y=0;$y<=$height;$y+=10){
$pdf->Line(0,$y,$width,$y);
}
$pdf->Output('I','horizontal-lines.pdf');
Only the Y coordinate changes. The X coordinates continue from the left edge to the right edge of the page.
View Horizontal Lines OutputWe can repeatedly increase the gap from the page edges and draw four new lines. This produces a series of nested rectangles.
<?php
require 'fpdf.php';
$pdf=new FPDF();
$pdf->AddPage();
$width=$pdf->GetPageWidth();
$height=$pdf->GetPageHeight();
$limit=min($width,$height)/2;
for($gap=2;$gap<=$limit;$gap+=2){
$pdf->Line($gap,$gap,$width-$gap,$gap);
$pdf->Line($gap,$height-$gap,$width-$gap,$height-$gap);
$pdf->Line($gap,$gap,$gap,$height-$gap);
$pdf->Line($width-$gap,$gap,$width-$gap,$height-$gap);
}
$pdf->Output('I','line-pattern.pdf');
The loop stops when the gap reaches half of the smaller page dimension. This prevents the calculated left/right or top/bottom coordinates from crossing each other.
View Nested Rectangle PatternUse SetLineWidth() before drawing a line:
$pdf->SetLineWidth(0.8);
$pdf->Line(20,40,180,40);
When FPDF is using its default millimetre unit, 0.8 represents a line width of 0.8 mm.
The selected width remains active for subsequent drawing operations until it is changed again.
SetDrawColor() sets the color used for lines and borders.
$pdf->SetDrawColor(220,60,60);
$pdf->Line(20,40,180,40);
The three values represent red, green and blue:
SetDrawColor(R,G,B);
Each RGB component should remain between 0 and 255.
Line width and color can also be changed while generating the nested pattern.
<?php
require 'fpdf.php';
$pdf=new FPDF();
$pdf->AddPage();
$width=$pdf->GetPageWidth();
$height=$pdf->GetPageHeight();
$limit=min($width,$height)/2;
$pdf->SetLineWidth(0.6);
for($gap=2;$gap<=$limit;$gap+=5){
$ratio=$gap/$limit;
$red=(int)(30+(180*$ratio));
$green=(int)(80+(120*$ratio));
$blue=(int)(220-(120*$ratio));
$pdf->SetDrawColor($red,$green,$blue);
$pdf->Line($gap,$gap,$width-$gap,$gap);
$pdf->Line($gap,$height-$gap,$width-$gap,$height-$gap);
$pdf->Line($gap,$gap,$gap,$height-$gap);
$pdf->Line($width-$gap,$gap,$width-$gap,$height-$gap);
}
$pdf->Output('I','colored-line-pattern.pdf');
The color calculation deliberately keeps all RGB components inside the supported 0–255 range.
View Colored Line PatternThe Line() method is useful for more than decorative patterns. It can be used to create:
For structured rows and columns, use the FPDF table tutorial. For text inside rectangular cells, see Cell().
Remember that PDF coordinates start at the upper-left corner. Increasing X moves right and increasing Y moves down the page.
A line placed exactly on the page edge can be partly clipped by the viewer or printer. Use a small positive gap when drawing page borders.
Call SetDrawColor() before the corresponding Line() call. The selected drawing color remains active until another color is set.
When colors are calculated inside a loop, make sure the red, green and blue values remain in the range 0–255.
Use GetPageWidth() and GetPageHeight() instead of hard-coded page dimensions so coordinates adapt to the current PDF page.
Use the Line(x1,y1,x2,y2) method and supply the starting and ending X and Y coordinates of the line.
The coordinate origin is at the upper-left corner of the page. X increases toward the right and Y increases toward the bottom.
Use GetPageWidth() and GetPageHeight(). These values are useful when lines must adapt to the current page size.
Call SetLineWidth() before drawing the line. The selected width remains active for subsequent drawing operations until it is changed.
Use SetDrawColor(R,G,B) before calling Line(). Keep each RGB component between 0 and 255.
Use a PHP loop and change one or more coordinates on each iteration. For horizontal lines, keep both X values fixed and increase the Y coordinate.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.