Draw Lines in PDF using PHP FPDF Line()

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);
Line coordinates in a PDF document using FPDF

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().

Draw a Simple Line in FPDF Top ↑

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

Straight line drawn in PDF using FPDF

Get the Width and Height of the PDF Page Top ↑

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 an X across the Full PDF Page Top ↑

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

Two diagonal FPDF lines forming an X across a PDF page

Draw a Border around the PDF Page Top ↑

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:

  • top horizontal line,
  • bottom horizontal line,
  • left vertical line,
  • right vertical line.
View Page Border Output

Draw Horizontal Lines at Regular Intervals Top ↑

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 Output

Horizontal lines drawn at regular intervals using PHP FPDF

Create Nested Rectangle Patterns using FPDF Lines Top ↑

We can repeatedly increase the gap from the page edges and draw four new lines. This produces a series of nested rectangles.

On an A4 portrait page these shapes are rectangles rather than squares because the page width and height are different.
<?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 Pattern

Nested rectangle line pattern created using FPDF

Change Line Width using SetLineWidth() Top ↑

Use 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.

Change Line Color using SetDrawColor() Top ↑

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.

Create a Colored Nested Line Pattern Top ↑

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 Pattern

Colored nested line pattern created using FPDF SetDrawColor

FPDF Line() Video Tutorials Top ↑

Line() method to draw lines using coordinates and by measuring page width and height

Drawing patterns of lines on PDF document by FPDF class in PHP using color and width of the line

Practical Uses of FPDF Line() Top ↑

The Line() method is useful for more than decorative patterns. It can be used to create:

  • report separators,
  • invoice sections,
  • page borders,
  • signature lines,
  • form separators,
  • chart axes,
  • custom PDF layouts.

For structured rows and columns, use the FPDF table tutorial. For text inside rectangular cells, see Cell().

Common FPDF Line Problems Top ↑

The line does not appear where expected Top ↑

Remember that PDF coordinates start at the upper-left corner. Increasing X moves right and increasing Y moves down the page.

A border is partly outside the visible page Top ↑

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.

The line color is not changing Top ↑

Call SetDrawColor() before the corresponding Line() call. The selected drawing color remains active until another color is set.

A generated RGB value becomes invalid Top ↑

When colors are calculated inside a loop, make sure the red, green and blue values remain in the range 0–255.

Patterns become distorted on another page size Top ↑

Use GetPageWidth() and GetPageHeight() instead of hard-coded page dimensions so coordinates adapt to the current PDF page.

Frequently Asked Questions Top ↑

Q1: How do I draw a line in a PDF using PHP FPDF?

Use the Line(x1,y1,x2,y2) method and supply the starting and ending X and Y coordinates of the line.

Q2: Where does the FPDF coordinate system start?

The coordinate origin is at the upper-left corner of the page. X increases toward the right and Y increases toward the bottom.

Q3: How do I get the width and height of the current FPDF page?

Use GetPageWidth() and GetPageHeight(). These values are useful when lines must adapt to the current page size.

Q4: How do I change the thickness of a line in FPDF?

Call SetLineWidth() before drawing the line. The selected width remains active for subsequent drawing operations until it is changed.

Q5: How do I change the color of an FPDF line?

Use SetDrawColor(R,G,B) before calling Line(). Keep each RGB component between 0 and 255.

Q6: How can I draw several lines at regular intervals?

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.


MultiCell Cell() Add Image to PDF

Create PDF Table Database Records to PDF


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer