php - How to write every two pages with TCPDF

one text

Solution:

Drawing a background image onto every page is straight out of example 51 in the documentation. We can add something similar to $img_file = $this->page % 2 ? 'recto.jpg' : 'verso.jpg'; to put a different background on odd and even pages.

Then it's just a case of adding a new page every time an even page is created. I've done this by extending endPage() which works a bit better with the final page than extending startPage().

You'll want to set some margins so that the text starts in the right place if it overruns the initial page.

class MyPDF extends TCPDF
{

    public function Header()
    {
        // get the current page break margin
        $bMargin = $this->getBreakMargin();
        // get current auto-page-break mode
        $auto_page_break = $this->AutoPageBreak;
        // disable auto-page-break
        $this->SetAutoPageBreak(false, 0);
        // set background image
        $img_file = $this->page % 2 ? 'recto.jpg' : 'verso.jpg';
        $this->Image($img_file, 0, 0, 210, 297, '', '', '', false, 300, '', false, false, 0);
        // restore auto-page-break status
        $this->SetAutoPageBreak($auto_page_break, $bMargin);
        // set the starting point for the page content
        $this->setPageMark();
    }

    public function endPage($tocpage = false)
    {
        parent::endPage($tocpage);
        if ($this->page % 2) {
            $this->startPage();
            $this->endPage();
        }
    }

}

$bloc_body = '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Curabitur pretium tincidunt lacus. Nulla gravida orci a odio. Nullam varius, turpis et commodo pharetra, est eros bibendum elit, nec luctus magna felis sollicitudin mauris. Integer in mauris eu nibh euismod gravida. Duis ac tellus et risus vulputate vehicula. Donec lobortis risus a elit. Etiam tempor. Ut ullamcorper, ligula eu tempor congue, eros est euismod turpis, id tincidunt sapien risus a quam. Maecenas fermentum consequat mi. Donec fermentum. Pellentesque malesuada nulla a mi. Duis sapien sem, aliquet nec, commodo eget, consequat quis, neque. Aliquam faucibus, elit ut dictum aliquet, felis nisl adipiscing sapien, sed malesuada diam lacus eget erat. Cras mollis scelerisque nunc. Nullam arcu. Aliquam consequat. Curabitur augue lorem, dapibus quis, laoreet et, pretium ac, nisi. Aenean magna nisl, mollis quis, molestie eu, feugiat in, orci. In hac habitasse platea dictumst.</p>';
$bloc_body .= $bloc_body;

$pdf = new MyPDF();
$pdf->SetAutoPageBreak(true, 106); // (boolean) Boolean indicating if mode should be on or off , (float) Distance from the bottom of the page.
$pdf->addPage();
$pdf->writeHTMLCell(0, 130, 8, 74, $bloc_body, 0, 0, false, true, '', true); // bloc body de la page
$pdf->output($fileName, 'I');

Source