westdc-zf1/include/fpdf/tutorial/tuto4.php

112 lines
2.2 KiB
PHP
Raw Normal View History

2009-03-06 03:20:46 +00:00
<?php
require('../fpdf.php');
class PDF extends FPDF
{
2011-11-03 02:25:38 +00:00
// Current column
var $col = 0;
// Ordinate of column start
2009-03-06 03:20:46 +00:00
var $y0;
function Header()
{
2011-11-03 02:25:38 +00:00
// Page header
2009-03-06 03:20:46 +00:00
global $title;
$this->SetFont('Arial','B',15);
2011-11-03 02:25:38 +00:00
$w = $this->GetStringWidth($title)+6;
2009-03-06 03:20:46 +00:00
$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);
2011-11-03 02:25:38 +00:00
// Save ordinate
$this->y0 = $this->GetY();
2009-03-06 03:20:46 +00:00
}
function Footer()
{
2011-11-03 02:25:38 +00:00
// Page footer
2009-03-06 03:20:46 +00:00
$this->SetY(-15);
$this->SetFont('Arial','I',8);
$this->SetTextColor(128);
$this->Cell(0,10,'Page '.$this->PageNo(),0,0,'C');
}
function SetCol($col)
{
2011-11-03 02:25:38 +00:00
// Set position at a given column
$this->col = $col;
$x = 10+$col*65;
2009-03-06 03:20:46 +00:00
$this->SetLeftMargin($x);
$this->SetX($x);
}
function AcceptPageBreak()
{
2011-11-03 02:25:38 +00:00
// Method accepting or not automatic page break
2009-03-06 03:20:46 +00:00
if($this->col<2)
{
2011-11-03 02:25:38 +00:00
// Go to next column
2009-03-06 03:20:46 +00:00
$this->SetCol($this->col+1);
2011-11-03 02:25:38 +00:00
// Set ordinate to top
2009-03-06 03:20:46 +00:00
$this->SetY($this->y0);
2011-11-03 02:25:38 +00:00
// Keep on page
2009-03-06 03:20:46 +00:00
return false;
}
else
{
2011-11-03 02:25:38 +00:00
// Go back to first column
2009-03-06 03:20:46 +00:00
$this->SetCol(0);
2011-11-03 02:25:38 +00:00
// Page break
2009-03-06 03:20:46 +00:00
return true;
}
}
2011-11-03 02:25:38 +00:00
function ChapterTitle($num, $label)
2009-03-06 03:20:46 +00:00
{
2011-11-03 02:25:38 +00:00
// Title
2009-03-06 03:20:46 +00:00
$this->SetFont('Arial','',12);
$this->SetFillColor(200,220,255);
$this->Cell(0,6,"Chapter $num : $label",0,1,'L',true);
$this->Ln(4);
2011-11-03 02:25:38 +00:00
// Save ordinate
$this->y0 = $this->GetY();
2009-03-06 03:20:46 +00:00
}
function ChapterBody($file)
{
2011-11-03 02:25:38 +00:00
// Read text file
$txt = file_get_contents($file);
// Font
2009-03-06 03:20:46 +00:00
$this->SetFont('Times','',12);
2011-11-03 02:25:38 +00:00
// Output text in a 6 cm width column
2009-03-06 03:20:46 +00:00
$this->MultiCell(60,5,$txt);
$this->Ln();
2011-11-03 02:25:38 +00:00
// Mention
2009-03-06 03:20:46 +00:00
$this->SetFont('','I');
$this->Cell(0,5,'(end of excerpt)');
2011-11-03 02:25:38 +00:00
// Go back to first column
2009-03-06 03:20:46 +00:00
$this->SetCol(0);
}
2011-11-03 02:25:38 +00:00
function PrintChapter($num, $title, $file)
2009-03-06 03:20:46 +00:00
{
2011-11-03 02:25:38 +00:00
// Add chapter
2009-03-06 03:20:46 +00:00
$this->AddPage();
$this->ChapterTitle($num,$title);
$this->ChapterBody($file);
}
}
2011-11-03 02:25:38 +00:00
$pdf = new PDF();
$title = '20000 Leagues Under the Seas';
2009-03-06 03:20:46 +00:00
$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();
?>