FPDF Header Question - Driving me crazy! - php

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

Related

FPDF PHP MYSQL Text Does Not Fit In Cell

I want to export data from MySql database in pdf format. I can do this from within the mysql database, it's very nice. I tried to automate this. I used the FPDF library but the data does not fit into the tables. I searched but couldn't fix it.
What should I do ?
Images :
My Columns :
Sample Content :
My Code :
<?php
require('./fpdf/fpdf.php');
//Database
require_once __DIR__ . '/db_config.php';
$connection =
mysqli_connect(DB_SERVER,DB_USER,DB_PASSWORD,DB_DATABASE);
if(!$connection){
die("Connection error : " . mysqli_connect_eror());
}
class PDF extends FPDF
{
// Page header
function Header()
{
$ImagePath ='image url';
// Logo
$this->Image($ImagePath,10,70,0,90);
$this->SetFont('Arial','B',13);
// Move to the right
$this->Cell(80);
// Title
$this->Cell(80,10,'title',1,0,'C');
// Line break
$this->Ln(20);
}
// Page footer
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,'Page '.$this-
>PageNo().'/{nb}',0,0,'C');
}
}
// Columns
$display_heading = array('JobId'=>'JobId','ReleaseDate'=>
'ReleaseDate',
'StartingDate'=>
'StartingDate','TargetTime'=>'TargetTime','JobContent'=>
'JobContent','KindId'=>'KindId','JobStatus'=>'JobStatus','CustomerId'
=>'CustomerId');
// Database Result
$result = mysqli_query($connection, "SELECT * FROM Jobs;") or die("database
error:". mysqli_error($connection));
$header = mysqli_query($connection, "SHOW columns FROM Jobs");
// Settings
$pdf = new PDF();
//header
$pdf->AddPage();
//foter page
$pdf->AliasNbPages();
$pdf->SetFont('Arial','B',8);
foreach($header as $heading) {
$pdf->Cell(24,7,$display_heading[$heading['Field']],1);
}
foreach($result as $row) {
$pdf->Ln();
foreach($row as $column)
$pdf->Cell(24,7,$column,1);
}
$pdf->Output();
//Connection close
mysqli_close($connection);
?>
You can use GetStringWidth() to determine how many mm wide a given string in the current font configuration is. Based on that, you can decrease the font size or insert line breaks, if the string width is wider than you threshold (in your case the cell width).
$fontSize = 14;
while($pdf->GetStringWidth($text) > 80){
$pdf->setFontSize(--$fontSize);
}
You might want to take a look at the "Fix text to cell" extension at http://www.fpdf.org/en/script/script62.php. See the resulting example PDF at http://www.fpdf.org/en/script/ex62.pdf to see what it looks like.
If you don't want to compress the text like that, something along the lines of what #stui has suggested would likely work best (getting the string width, and splitting the text across multiple lines).

FPDF break page error

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

Unable to output MySQL data to FPDF table

I've tried the tutorials on fpdf and searched all over for helpful code. And I found what I thought would do the trick here on SO. That said, following the example here didn't produce good results. I definitely need help with this.
Here's my code:
<?php
require_once '../root_login.php';
require('fpdf.php');
class PDF extends FPDF
{
// Page header
function Header()
{
// Logo
//$this->Image('images/bg_all.jpg',10,6,30);
// Arial bold 15
$this->SetFont('Times','B',20);
// Move to the right
$this->Cell(80);
// Title
$this->Cell(40,10,'B A S E B A L L',0,0,'C');
// Line break
$this->Ln(6);
// Arial bold 15
$this->SetFont('Arial','B',9);
$this->Cell(200,10,'LITTLE LEAGUE ROSTERS',0,0,'C');
$this->Ln(20);
}
// Page footer
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,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}
// Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',8);
$stmt = $db->prepare('SELECT fname, lname
FROM rosters
ORDER BY lname');
$stmt->execute();
$result = $stmt->fetchALL(PDO::FETCH_ASSOC);
foreach($result as $row) {
$pdf->Cell(0,10,'F NAME:', $row['fname']);
$pdf->Ln();
$pdf->Cell(0,5,'L NAME:', $row['lname']);
$pdf->Ln();
}
$pdf->Output();
?>
You can view the output here
Does anyone see what I'm doing wrong? And why are those lines being drawn? Thanks.
In FPDF you should fully define all cell options and your text for the cell should be in the third position:
$pdf->Cell(0,5,'L NAME:'. $row['lname'], 0, 0, 'L');
------------------------^
Note the concatenation. Then follow up with the border(0 for no border, which should be the default), the next line definition (Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.) and the alignment of the text in the cell.
Joining the string and doing it with less parameters?
foreach($result as $row) {
$pdf->Cell(0,10,'F NAME: '.$row['fname']);
$pdf->Ln();
$pdf->Cell(0,5,'L NAME: '.$row['lname']);
$pdf->Ln();
}

Why doesn't FPDF show the exact data from the database?

Good day all , I've made a php script that extracts some data from a mysql table and creates a PDF with it.It works but some of the words have a '?' instead of the letters 'ș','ț','î','ă','î','â'.I don't know what causes that, I used FPDF library. Here is the code :
<?php
require('mysql_table.php');
class PDF extends PDF_MySQL_Table
{
function Header()
{
//Title
$this->SetFont('Arial','',18);
$this->Cell(0,6,'Titlu',0,1,'C');
$this->Ln(10);
//Table header
parent::Header();
}
}
//Connect to the database
mysql_connect('localhost','root','');
mysql_select_db('dct_2015_2016');
$pdf=new PDF();
$pdf->AddPage();
//Table
$pdf->AddCol('semestru',20,'Semestru','C');
$pdf->AddCol('facultate',40,'Facultate');
$pdf->AddCol('opt_id',20,'Optiune','C');
$pdf->AddCol('disciplina',100,'Disciplina','L');
$pdf->AddCol('cati_au_ales',20,'Persoane','C');
$prop=array('HeaderColor'=>array(255,150,100),
'color1'=>array(210,245,255),
'color2'=>array(255,255,210),
'padding'=>2);
$pdf->Table('select semestru,facultate,opt_id,disciplina,cati_au_ales from nr_optiuni order by semestru',$prop);
$pdf->Output();
?>
If I run the SELECT from mySql Admin the data is displayed correct , with the special characters.

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 :)

Categories