如何在for循环内实现MPDF Page Break

时间:2019-06-10 13:14:43

标签: mpdf

我无法在for循环中使用干净的分页符

我尝试了mpdf-> addPage和带有page-break-after:的内联CSS:一直没有成功。所有变量均正确传递

<?php
// Require composer autoload
require_once __DIR__ . '/vendor/autoload.php';
// Create an instance of the class:
$mpdf = new \Mpdf\Mpdf();

//Set Header
$mpdf->SetHTMLHeader('  <div class="pageHeaderLeft">
        <img src='.$data_1.' title="Logo" width=150px>
        <div>'.$data_2.'</div>
    </div>

    <div class="pageHeaderRight">Date:'.$data_4.'
    </div>

    <div class="pageHeaderRight2">
        <div>'.$data_3.'</div>
    </div>

    <div class="pageOffer">
        <div>OFFRE DE PRIX N°'.$data_5.'</div>
    </div>
');

//Set Footer
$mpdf->SetHTMLFooter('<hr>
<div class="pageFooter">
<div>'.$data_10.'</div>
</div>
{PAGENO}
');

$main='!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <title>Offre de Prix</title>
    <link rel="stylesheet" href="./PDF/cssPDF.css">
</head>
<body>

<div class="pageMain">
    <p>Contact:</p>
    <div class="pageMainContact">'.$data_6.'</div>
</div>

<div class="pageMain2">
<p>Destinataire:</p>
<div class="pageMainContact">
'.$data_7.'
    </div>
</div>

<div class="offreDetails">
<div class="pageDescrip">';

$ligneDesc = explode("</p>",$data_8);
$nbLignes = count($ligneDesc);
$ligneBreak = substr_count($data_8,"<br>");
$totalLignes = $nbLignes+$ligneBreak;
for ($i=0; $i<$nbLignes; $i++)
{
    $ligneDesc[$i] = $ligneDesc[$i]."</p>";
}



for ($index = 0;$index < $nbLignes; ++$index)
{
    if (($index+$ligneBreak) == 100)
    {
        $main =$main.'<div style="page-break-after:always"></div>';
        $totalLignes = 0;
    }
    $main = $main.$ligneDesc[$index];
    $totalLignes +=1;
}


$main =$main.'</div>


</div>

</body>
</html>';

// Write some HTML code:
$mpdf->WriteHTML($main);

// Output a PDF file directly to the browser
$mpdf->SetTitle($data_5);
$mpdf->Output($data_5.'.pdf', 'I');
try {
$mpdf->Output();
} catch (\Mpdf\MpdfException $e) { // Note: safer fully qualified exception name used for catch
    // Process the exception, log, print etc.
    echo $e->getMessage();
}
?>

对于"$main =$main.'<div style="page-break-after:always"></div>';"所在的for循环,我得到了空白数据输出。

如果我删除此行,则会显示所有文本,但仅缩小到一页。

我希望数组显示在第二页上。

谢谢

0 个答案:

没有答案