Skip to content

Instantly share code, notes, and snippets.

@estefanionsantos
Last active April 19, 2025 04:36
Show Gist options
  • Save estefanionsantos/9033a7244fb418a61b4a9d3ff48d1498 to your computer and use it in GitHub Desktop.
Save estefanionsantos/9033a7244fb418a61b4a9d3ff48d1498 to your computer and use it in GitHub Desktop.
fpdf-tuto4-1
<?php
use Rubricate\Fpdf\FileFpdf;
class PDF extends FileFpdf
{
protected $col = 0; // Current column
protected $y0; // Ordinate of column start
public function header()
{
global $title;
$w = $this->getStringWidth($title)+6;
$this->setFont('Arial', 'B', 15);
$this->setX((210-$w)/2);
$this->setDrawColor(0, 80, 180);
$this->setFillColor(230, 230, 0);
$this->setTextColor(220, 50, 50);
$this->setLineWidth(1);
$this->cell($w, 9, $title, 1, 1, 'C', true);
$this->ln(10);
$this->y0 = $this->GetY();
}
public function footer()
{
$this->setY(-15);
$this->setFont('Arial','I', 8);
$this->setTextColor(128);
$this->cell(0, 10, 'Page ' . $this->PageNo(), 0, 0, 'C');
}
public function setCol($col)
{
$this->col = $col;
$x = 10 + $col * 65;
$this->setLeftMargin($x);
$this->setX($x);
}
public function acceptPageBreak(): bool
{
if($this->col < 2) {
$this->setCol($this->col+1);
$this->setY($this->y0);
return false;
} else {
$this->setCol(0);
return true;
}
}
public function chapterTitle($num, $label)
{
$this->setFont('Arial', '', 12);
$this->setFillColor(200, 220, 255);
$this->cell(0, 6, "Chapter $num : $label", 0, 1, 'L', true);
$this->ln(4);
$this->y0 = $this->GetY();
}
public function chapterBody($file)
{
$txt = file_get_contents($file);
$this->setFont('Times', '', 12);
$this->multiCell(60, 5, $txt);
$this->ln();
$this->setFont('', 'I');
$this->cell(0,5, '(end of excerpt)');
$this->setCol(0);
}
public function printChapter($num, $title, $file)
{
$this->addPage();
$this->chapterTitle($num, $title);
$this->chapterBody($file);
}
}
$title = '20000 Leagues Under the Seas';
$pdf = new PDF();
$pdf->setTitle($title);
$pdf->setAuthor('Jules Verne');
$pdf->printChapter(1, 'A RUNAWAY REEF', '20k_c1.txt');
$pdf->printChapter(2, 'THE PROS AND CONS', '20k_c2.txt');
$pdf->output();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment