Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FPDF How to force a page break

Tags:

php

pdf

fpdf

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.

like image 890
user3283304 Avatar asked Feb 07 '14 10:02

user3283304


People also ask

How do you do a page break in Fpdf?

When creating visually pleasing PDFs, it is essential to have control over exactly where the content of one page ends and the content of another begins. To insert a page break into your document, simply place your cursor where you want the page to break and click the Page Break button.

How do I get Fpdf total pages?

The total number of pages can only be known just before the document is finished. For example: $pdf = new FPDF(); $pdf->AddPage(); $pdf->AddPage(); $nb = $pdf->PageNo(); $pdf->Output();

How do you use MultiCell in Fpdf?

MultiCell( width of cell, height of each line, text content, border, alignment of the text, fill boolean). An example would be : $this ->MultiCell(25,6,”Here's some text for display”, 'LRT', 'L', 0); In the above example, the MultiCell width is set at 25 units that were specified in the initial call to FPDF.

What is Fpdf error?

The FPDF Error Message will point you to the PHP Line that is sending some content. If you get no hint what File & Line send some content you probably have an encoding mismatch in your include / require Files. For me. fpdf.php was ANSI-encoded, my pdf-generator.php was UTF-8-encoded and.


1 Answers

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.

like image 99
cbreezier Avatar answered Nov 14 '22 23:11

cbreezier