FPDF break page error - php

i'm developin an app that sholud create a pdf document with more than one page, my problem is that i found the way to add pages when needen but after the break page it doesn't write.
So the result is a pdf with 8 pages where 7 are blank and only one, the first page that in the document is the last, is correctly writed.
THIS IS MY CODE:
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Helvetica','',10);
$pdf->SetXY(20,20);
$pdf->AcceptPageBreak();
$pdf->SetAutoPageBreak(true);
//PDF content before table
$pdf->Write(14,"Resoconto dell'applicazione EBI dell'ultimo mese:");
$pdf_table=new FPDF_Table($pdf);
//$pdf_table->setDefaultCellWidth(50);
//echo "<br>";
//echo "------------------------------------------- <br>";
$i=0;
$righe=0;
while (...) {
if ($righe % 20 === 0) {
$pdf->AddPage();
$pdf->SetFont('Helvetica','',10);
$pdf->SetXY(20,20);
}
$righe++;
$pdf->Write(...);
}
How can i solve this?
Thank's

Related

FPDF: Add some text at the END of the page

I want to add some text like, "Prepared by," on the last page of the PDF. In my case, I display 2 rows of data in the first page and 3 rows of data for second page onwards.
The problem that I faced is if the last page got 1 or 2 rows data only, the "Prepared by" will show on the same page. But, if the last page got 3 rows of data, the "Prepared by" will show at the next page which is a blank page (no rows of data). Below is my code:
$count = 0;
while ($row = mysqli_fetch_array($user3)){
//rendering row.....
//.....
if ((($count - 4) % 3) === 0) {
$pdf->AddPage();
}
$count++;
}
$pdf->Cell(0,5,'',0,1,"C");
$pdf->Cell(63,8,'Prepared by,',0,0,"C");
$pdf->Output();
Can anyone know what is the problem and how to fix it?
You could try to add this .
$count = 0;
while ($row = mysqli_fetch_array($user3)){
//rendering row.....
//.....
if ((($count - 4) % 3) === 0) {
$pdf->AddPage();
}
$count++;
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Page number
$this->Cell(0,10,'Prepared by: Your Name');
}
}
$pdf->Output();

PHP Insert multiple PDF's into PDF on specific page number

My problem is - A user can upload a PDF-File and now I want to add other (between 1 and n) single page PDF's after every Page of the user uploaded PDF.
For example a user uploads a PDF with 4 Pages and I have got 3 single page PDF's on my Webserver that I want to merge with the user PDF. The resulting PDF should then look like this:
Page 1: Page #1 from User uploaded PDF
Page 2: First Single-Page-PDF from Webserver
Page 3: Page #2 from User uploaded PDF
Page 4: Second Single-Page-PDF from Webserver
Page 5: Page #3 from User uploaded PDF
Page 6: Third Single-Page-PDF from Webserver
Page 7: Page #4 from User uploaded PDF
Page 8: First Single-Page-PDF from Webserver
So far I've tried it with PDFMerger
$pdf = new PDFMerger;
$pdf->addPDF('samplepdfs/userpdf.pdf', 'all')
->addPDF('samplepdfs/one.pdf', 'all')
->addPDF('samplepdfs/two.pdf', 'all')
->addPDF('samplepdfs/three.pdf', 'all')
->merge('file', 'xxx/result.pdf');
but this only adds the other PDF's at the end..
Is there a solution in PHP where I can specify the number of the page where the other PDF's shall be inserted?
You should avoid using the PDFMerger class. It is almost 6 (!!!) years old!
You can do the same with FPDI without much effort. Anyhow you should insert the pages in the order you want them to appear. So I guess it's more a logic problem. From what you'd written in your question I assume that you want to import a whole document while spreading another document with 3 pages between the imported pages. So a simple POC could look like this:
<?php
require_once("libs/fpdf/fpdf.php");
require_once("libs/fpdi/fpdi.php");
$pdf = new FPDI();
// let's prepare the static "filler pages"
$staticIds = array();
$pageCount = $pdf->setSourceFile('pdf/on/the/webserver.pdf');
for ($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++) {
$staticIds[$pageNumber] = $pdf->importPage($pageNumber);
}
// get the page count of the uploaded file
$pageCount = $pdf->setSourceFile('the/uploaded.pdf');
// let's track the page number for the filler page
$fillerPageCount = 1;
// import the uploaded document page by page
for ($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++) {
$tplId = $pdf->importPage($pageNumber);
$pdf->AddPage();
$pdf->useTemplate($tplId, null, null, 0, 0, true);
// add the current filler page
$pdf->AddPage();
$pdf->useTemplate($staticIds[$fillerPageCount], null, null, 0, 0, true);
// update the filler page number or reset it
$fillerPageCount++;
if ($fillerPageCount > count($staticIds)) {
$fillerPageCount = 1;
}
}
// done
$pdf->Output('xxx/result.pdf', 'F');

How to add table header to all pages using FPDF and WriteHTML

I'm generating a large table (more than one page) using FPDF in Landscape mode and need to add table header in all pages. How can I do that? Here is my code:
require('html_table.php');
$pdf=new PDF();
$pdf->AddPage('L');
$pdf->SetFont('Arial','',12);
$html='<table border="1">';
for ($i=0; $i<300; $i++)
{
$html.='<tr><td>hello</td><td>hello</td><td>hello</td><td>hello</td></tr>';
}
$html.='</table>';
$pdf->WriteHTML($html);
$pdf->Output("reporte_semanal_".date("d").date("m").date("Y")."_".date("H")."H".date("i")."M".date("s")."S".".pdf", "D");

FPDF Get page numbers at footer on Every A4 size page

I am creating PDF reports using FPDF. Now how do I generate page numbers on each page of a report at the bottom of the page.
Below is the sample code for generating a 2 page PDF.
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial','',16);
$start_x=$pdf->GetX();
$current_y = $pdf->GetY();
$current_x = $pdf->GetX();
$cell_width = 25; $cell_height=14;
$j = 20; // This value will be coming from Database so we dont know how many pages the report is going to be
for ($i = 0; $i<$j ; $i++){
$pdf->MultiCell($cell_width,$cell_height,'Hello1',1);
$current_x+=$cell_width;
$pdf->Ln();
}
$pdf->Output();
?>
Note : The $j value will be coming from the database so we don't know how many pages is the report going to be.
To add an A4 page, with portrait orientation, do:
$pdf->AddPage("P","A4");
Create a new class which extends the FPDF class, and override the pre-defined Footer method.
Example:
class PDF extends FPDF
{
function Footer()
{
// Go to 1.5 cm from bottom
$this->SetY(-15);
// Select Arial italic 8
$this->SetFont('Arial','I',8);
// Print centered page number
$this->Cell(0,10,'Page '.$this->PageNo(),0,0,'C');
}
}
According to my comment you can place
$pdf->PageNo();
on your page where ever you like. Also you can add a placeholder to this
$pdf->AliasNbPages();
What would look like
$pdf->AliasNbPages('{totalPages}');
By default it's {nb}. It's not necessary to add a placeholder
Than you could add the pagesum like
$pdf->Cell(0, 5, "Page " . $pdf->PageNo() . "/{totalPages}", 0, 1);
or without your own placeholder
$pdf->Cell(0, 5, "Page " . $pdf->PageNo() . "/{nb}", 0, 1);
this would produce e.g.
Page 1/10
in case there were 10 pages :)
But beware
Using the placeholder will mess up the width of the cell. So if you have e.g. 180 page-width than 90 isn't the mid anymore (In the line where you use the placeholder). You will see if you try :)

FPDF Header Question - Driving me crazy!

I am trying to generate a PDF that outputs Item Names onto a template PDF (using FPDI) with the Username listed on the top of each page. Every user can have a different number of items (i.e. if there are 1-4 items, only output one page; if there 5-8 items, output two pages, etc.)
Here is an example of what I am trying to do: http://www.mediafire.com/?k2ngmqm1jmm
This is what I have so far. I am able to get all of the spacing to work by setting a TopMargin, but this doesn't allow me to put the Username header in.
<?php
require_once('auth.php');
require_once('config.php');
require_once('connect.php');
$username=$_GET['username'];
$sql="SELECT * FROM $tbl_items WHERE username='$username'";
$result=mysql_query($sql);
require_once('pdf/fpdf.php');
require_once('pdf/fpdi.php');
$pdf =& new FPDI();
$pdf->SetTopMargin(30);
$pdf->AddPage('L', 'Letter');
$pdf->setSourceFile('../pdf/files/chart_template.pdf');
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx);
$pdf->SetTextColor(0,0,0);
$pdf->SetFont('Arial', 'B');
$pdf->SetFontSize(7);
while($rows=mysql_fetch_array($result)){
$pdf->Cell(20,5,$rows['itemname'],0,0,'C');
$pdf->Ln(45);
}
$pdf->useTemplate($tplIdx);
$pdf->Output('file.pdf', 'I');
?>
Please help!
I've done it previously using the 'header' class extension:
class PDF extends FPDF
{
function Header()
{
//Select Arial bold 15
$this->SetFont('Arial','B',15);
//Move to the right
$this->Cell(80);
//Framed title
$this->Cell(30,10,'Title',1,0,'C');
//Line break
$this->Ln(20);
}
Have a look at the tutorial which explains the header usage at : http://www.fpdf.org/en/tutorial/tuto2.htm

Categories