Grid

Informations

Author: Anthony Master
License: FPDF

Description

This extension adds grid support to help with aligning elements while developing reports.

The grid can be turned on/off/set for future pages by using the class $grid variable. Either assign a boolean or an integer value (indicating the grid spacing).

Source

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

class PDF_Grid extends FPDF {
    var $grid = false;

    function DrawGrid()
    {
        if($this->grid===true){
            $spacing = 5;
        } else {
            $spacing = $this->grid;
        }
        $this->SetDrawColor(204,255,255);
        $this->SetLineWidth(0.35);
        for($i=0;$i<$this->w;$i+=$spacing){
            $this->Line($i,0,$i,$this->h);
        }
        for($i=0;$i<$this->h;$i+=$spacing){
            $this->Line(0,$i,$this->w,$i);
        }
        $this->SetDrawColor(0,0,0);

        $x = $this->GetX();
        $y = $this->GetY();
        $this->SetFont('Arial','I',8);
        $this->SetTextColor(204,204,204);
        for($i=20;$i<$this->h;$i+=20){
            $this->SetXY(1,$i-3);
            $this->Write(4,$i);
        }
        for($i=20;$i<(($this->w)-($this->rMargin)-10);$i+=20){
            $this->SetXY($i-1,1);
            $this->Write(4,$i);
        }
        $this->SetXY($x,$y);
    }

    function Header()
    {
        if($this->grid)
            $this->DrawGrid();
    }
}
?>

Example

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

$pdf = new PDF_Grid();

// Add page with a grid and default spacing (5mm)
$pdf->grid = true;
$pdf->AddPage();

// Add page with a grid (10mm spacing)
$pdf->grid = 10;
$pdf->AddPage();

// Disable grid
$pdf->grid = false;
$pdf->AddPage();

$pdf->Output();
?>
View the result here.

Download

ZIP | TGZ