<?php
class FPDF{
  protected $k,$w,$h,$pages,$page,$l,$x,$y,$tc,$fc;
  function __construct(){ $this->k=72/25.4; $this->w=210; $this->h=297; $this->pages=[]; $this->page=0; $this->l=10; $this->x=$this->l; $this->y=10; $this->tc='0 0 0 rg'; $this->fc='0.9 0.9 0.9 rg'; }
  function SetTitle($t){} function AddPage(){ $this->page++; $this->pages[$this->page]=''; $this->x=$this->l; $this->y=10; }
  function GetY(){ return $this->y; } function SetY($y){ $this->y=$y; }
  function SetTextColor($r,$g,$b){ $this->tc=($r/255).' '.($g/255).' '.($b/255).' rg'; }
  function SetFillColor($r,$g,$b){ $this->fc=($r/255).' '.($g/255).' '.($b/255).' rg'; }
  function Rect($x,$y,$w,$h,$fill=true){ $cmd=sprintf("%.2f %.2f %.2f %.2f re\n",$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,(-$h)*$this->k); $this->pages[$this->page].=$this->fc."\n".$cmd.($fill?'f':'S')."\n"; }
  function Cell($w,$h=0,$txt='',$ln=0){ $s=$this->tc."\n".sprintf("BT /F1 10 Tf %.2f %.2f Td (%s) Tj ET\n",($this->x)*$this->k,($this->h-($this->y+$h))*$this->k,$this->_e($txt)); $this->pages[$this->page].=$s; $this->x+=$w; if($ln>0){ $this->y+=$h; $this->x=$this->l; } }
  function Ln($h=5){ $this->x=$this->l; $this->y+=$h; }
  function Output(){ $n=3; header('Content-Type: application/pdf'); echo "%PDF-1.3\n"; echo "1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj\n"; echo "2 0 obj<</Type/Pages/Count ".count($this->pages)."/Kids["; for($i=0;$i<count($this->pages);$i++){ echo ($n+2*$i)." 0 R "; } echo "]>>endobj\n"; for($i=0;$i<count($this->pages);$i++){ echo ($n+2*$i)." 0 obj<</Type/Page/Parent 2 0 R/MediaBox[0 0 ".($this->w*$this->k)." ".($this->h*$this->k)."]/Contents ".($n+2*$i+1)." 0 R/Resources<</Font<</F1 100 0 R>>>>>>endobj\n"; echo ($n+2*$i+1)." 0 obj<</Length ".strlen($this->pages[$i+1]).">>stream\n".$this->pages[$i+1]."endstream endobj\n"; } echo "100 0 obj<</Type/Font/Subtype/Type1/BaseFont/Helvetica>>endobj\ntrailer<</Size ".($n+2*count($this->pages)+1)."/Root 1 0 R>>\n%%EOF"; }
  function _e($s){ return str_replace(['\\','(',')',"\r","\n","\t"],['\\\\','\\(','\\)','', '', ' '],$s); }
}
