From 1c7460c0140c1b2c5a3a85e99cb6a397c6fd2135 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Wed, 2 Jun 2021 16:26:58 +0200 Subject: [PATCH] Simplify a5 booklet code example --- real-life-examples/a5-booklet.md | 85 +++++++++++++++++++------------- 1 file changed, 50 insertions(+), 35 deletions(-) diff --git a/real-life-examples/a5-booklet.md b/real-life-examples/a5-booklet.md index fde955a..21bd60c 100644 --- a/real-life-examples/a5-booklet.md +++ b/real-life-examples/a5-booklet.md @@ -10,6 +10,8 @@ This script was written to create a new PDF file based on a pre-existing PDF doc into an A5 booklet ready for duplex printing. Page order is adjusted, and page orientation is rotated so that it prints a landscape booklet. +## For mpdf 8.0 + ```php 0, ]); -$mpdf->SetImportUse(); // only with mPDF <8.0 - -$ow = $mpdf->h; -$oh = $mpdf->w; $pw = $mpdf->w / 2; $ph = $mpdf->h; $mpdf->SetDisplayMode('fullpage'); $pagecount = $mpdf->SetSourceFile('A4sourcefile.pdf'); -$pp = GetBookletPages($pagecount); - -foreach ($pp as $v) { +for ($i = 1; $i <= $pagecount; $i++) { $mpdf->AddPage(); - if ($v[0] > 0 && $v[0] <= $pagecount) { - $tplIdx = $mpdf->ImportPage($v[0], 0, 0, $ow, $oh); - $mpdf->UseTemplate($tplIdx, 0, 0, $pw, $ph); - } + // add odd pages on the left side + $tplId = $mpdf->importPage($i); + $mpdf->useTemplate($tplId, 0, 0, $pw, $ph); - if ($v[1] > 0 && $v[1] <= $pagecount) { - $tplIdx = $mpdf->ImportPage($v[1], 0, 0, $ow, $oh); - $mpdf->UseTemplate($tplIdx, $pw, 0, $pw, $ph); - } + // add even pages on the right side + ++$i; + if ($i <= $pagecount) { + $tplId = $mpdf2->importPage($i); + $mpdf2->useTemplate($tplId, $pw, 0, $pw, $ph); + } } $mpdf->Output(); exit; +``` -function GetBookletPages($np, $backcover = true) { - $lastpage = $np; - $np = 4 * ceil($np / 4); - $pp = []; - for ($i = 1; $i <= $np / 2; $i++) { +## For mpdf <8.0 - $p1 = $np - $i + 1; +```php += $lastpage) { - $p1 = 0; - } - } +$mpdf = new \Mpdf\Mpdf([ + 'format' => 'A4-L', + 'margin_left' => 0, + 'margin_right' => 0, + 'margin_top' => 0, + 'margin_bottom' => 0, + 'margin_header' => 0, + 'margin_footer' => 0, +]); - $pp[] = ($i % 2 == 1) - ? [ $p1, $i ] - : [ $i, $p1 ]; - } +$mpdf->SetImportUse(); - return $pp; -} +$ow = $mpdf->h; +$oh = $mpdf->w; +$pw = $mpdf->w / 2; +$ph = $mpdf->h; -``` +$mpdf->SetDisplayMode('fullpage'); +$pagecount = $mpdf->SetSourceFile('A4sourcefile.pdf'); + +for ($i = 1; $i <= $pagecount; $i++) { + $mpdf->AddPage(); + + // add odd pages on the left side + $tplIdx = $mpdf->ImportPage($i, 0, 0, $ow, $oh); + $mpdf->UseTemplate($tplIdx, 0, 0, $pw, $ph); + + // add even pages on the right side + ++$i; + if ($i <= $pagecount) { + $tplIdx = $mpdf->ImportPage($i, 0, 0, $ow, $oh); + $mpdf->UseTemplate($tplIdx, $pw, 0, $pw, $ph); + } +} +```