Skip to content

Instantly share code, notes, and snippets.

@dtinth
Created May 28, 2010 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtinth/416863 to your computer and use it in GitHub Desktop.
Save dtinth/416863 to your computer and use it in GitHub Desktop.
<?php
function processPage($pdf, $page) {
$pdf->addPage ();
processSection ($pdf, $page['left'], 0);
processSection ($pdf, $page['right'], 147.5);
}
foreach (preparePapers(41) as $paper) {
processPage ($pdf, $paper['back']);
processPage ($pdf, $paper['front']);
}
<?php
// this class represents the sections.
class Section {
var $pageNumber = false;
var $nextSection;
}
// one paper has 4 sections. 2 at the back and 2 at the front
class Paper {
var $backLeft;
var $backRight;
var $frontLeft;
var $frontRight;
function __construct() {
$this->backLeft = new Section;
$this->backRight = new Section;
$this->frontLeft = new Section;
$this->frontRight = new Section;
$this->backRight->nextSection = $this->frontLeft;
$this->frontLeft->nextSection = $this->frontRight;
$this->frontRight->nextSection = $this->backLeft;
}
function toArray() {
return array(
'back' => array(
'left' => $this->backLeft->pageNumber,
'right' => $this->backRight->pageNumber
),
'front' => array(
'left' => $this->frontLeft->pageNumber,
'right' => $this->frontRight->pageNumber
)
);
}
}
function preparePapers($numPages) {
// calculate the number of requied paper.
$numPaper = ceil($numPages / 4);
$papers = array();
// create papers and arrange them on top of the last paper.
for ($i = 0; $i < $numPaper; $i ++) {
$papers[$i] = new Paper;
if ($i > 0) {
$papers[$i - 1]->frontLeft->nextSection = $papers[$i]->backRight;
$papers[$i]->backLeft->nextSection = $papers[$i - 1]->frontRight;
}
}
// number each page
for ($i = 1, $currentSection = $papers[0]->backRight;
$i <= $numPages;
$i ++, $currentSection = $currentSection->nextSection) {
$currentSection->pageNumber = $i;
}
// flatten paper (clean array)
foreach ($papers as $k => $v) {
$papers[$k] = $v->toArray();
}
return $papers;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment