Search code examples
phppdffpdf

FPDF How to force a page break


I am using FPDF (1.7) to convert a TXT file into PDF. I would like to force a page break in the PDF product. The text file is created using php, and successfully uses carriage returns (\r). But I cannot get form feeds (\f) to appear in the PDF.

Is there another way to force a page break in FPDF by either changing the original text file or the php code? Perhaps a different character? For now, I have a tilde marking where pages should break.

If it is of any interest, I will post the simple php used to call FPDF:

<?php
require('fpdf.php');
$pdf = new FPDF('P','mm','A5');
$pdf->AddPage('P'); 
$pdf->SetDisplayMode(real,'default');
$pdf->SetFont('Arial','',10);
$txt = file_get_contents('comments.txt', true);
$pdf->Write(8, $txt);
$pdf->Output();
?>

Thanks for your attention.


Solution

  • Here's an idea: Edit: complete code

    <?php
        require('fpdf.php');
        $pdf = new FPDF('P','mm','A5');
        $pdf->AddPage('P'); 
        $pdf->SetDisplayMode(real,'default');
        $pdf->SetFont('Arial','',10);
        $txt = file_get_contents('comments.txt', true);
        $pages = explode('~', $txt);
        foreach ($pages as $page) {
            $pdf->Write(8, $page);
            $pdf->AddPage();
        }
        $pdf->Output();
    ?>
    

    Brief explanation in case not clear: Splits the text up into 'pages' and writes each of them with a page break in between.

    Possible bugs: adding extra page at the end. Solution: iterate with for loop instead of foreach and stop one page short.

    Another possible bug: won't be able to ever use tilde again. (Bad) solution: use a more complex string to signify a page break.