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.
Related
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).
I'm trying to convert php into pdf with the function FPDF but I did a select on my data base and I would like to have all the element of this select in PDF:
$query="select organisme,id from client";
$resultats=$db->query($query);
$pdf=new FPDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFillColor(232,232,232);
$pdf->SetFont('Arial','B',12);
$pdf->Cell(20,6,'ID',1,0,'C',100);
$pdf->Cell(70,6,'mes organismes',1,0,'C',1);
$pdf->SetFont('Arial','',10);
while ($row = $resultats->fetch(PDO::FETCH_ASSOC))
{
$pdf->Cell(20,6,utf8_decode($row['id']),1,0,'C',1);
$pdf->Cell(70,6,$row['organisme'],1,0,'C',1);
}
$pdf->Output();
?>
But the problem is that I got the ID and organisme but I have a problem :
i would like to know how I do to skip line into all new id and organisme
Thanks
FPDF's Ln() function performs a line break.
$pdf->Ln();
Add this line below your $pdf->Cell() function calls, both for the header row and inside the while loop.
I am beginning to use the FPDF to export the pdf from the Mysql. Now that i can export the pdf. I have referenced this link and this link, but I still cannot change the cell width and text width.
Could anyone provide any suggestions? Thank you very much.
<?php
require('mysql_table.php');
class PDF extends PDF_MySQL_Table
{
function Header()
{
//Title
$this->SetFont('Arial','',18);
$this->Cell(0,6,'Test',0,1,'C');
$this->Ln(10);
//Ensure table header is output
parent::Header();
}
}
mysql_connect('localhost','root','');
mysql_select_db('test');
$query = "SELECT * FROM test";
$result = mysql_query($query) or die(mysql_error());
$rowCount = 2;
while($row = mysql_fetch_array($result)){
$thisid = $row['ID'];
$fileName =$row['name'];
$filepath = $row['path'];
$date = $row['date'];
$rowCount++;
}
$pdf=new PDF('P','mm',array(1000,1500));
$pdf->AddPage();
$pdf->Table('select * from test');
$pdf->AddPage();
$pdf->SetFont('Arial','',14);
$pdf->Cell('','',$thisid,1);
$pdf->Cell('','',$fileName,1);
$pdf->Cell('','','','','','',false,$filepath);
$pdf->Cell('','',$date);
$pdf->Output();
?>
Check the below answer to set cell width
Inserting an image with PHP and FPDF, failed to load big image
As per your question you may need to pass width and height like below to cell() function
$pdf->cell(1000,1000,'',$pdf->Image($image,10,10,235,$height*18/100),'PNG');
This is to resize the cell and load img into the same
you can use this code to solve this problem... here the concept is only if string size greater than your cell width then font size will reduce.
Note that you can not use this code for a multi-line string. for this, you have to use MultiCell()
$table_head1=array(25,46,40,20,60);
$table_head2=array(25,20,26,20,20,16,24,40);
// some code remove from here for show actual code.
$pdf->CellFit($table_head2[7],$table_height,$item['remark'],1,0,'C',0,'',1,0);
use this link
how to auto adjust cell width in fpdf using php and mysql
How to export MySQL database table contents on to a PDF file. It has to be displayed in the website as download/print data. When the customer click on that he/she has to get the PDF opened or an option to download the PDF with all the contents of the table using php.
<?php
require('fpdf17/fpdf.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',10);
$pdf->Ln();
$pdf->Ln();
$pdf->SetFont('times','B',10);
$pdf->Cell(25,7,"Stud ID");
$pdf->Cell(30,7,"Student Name");
$pdf->Cell(40,7,"Address");
$pdf->Cell(30,7,"Class");
$pdf->Cell(30,7,"Phone No");
$pdf->Cell(30,7,"E-mail");
$pdf->Ln();
$pdf->Cell(450,7,"----------------------------------------------------------------------------------------------------------------------------------------------------------------------");
$pdf->Ln();
include ('db.php');
$sql = "SELECT studid,name,address,class,phone,email FROM studinfo";
$result = mysql_query($sql);
while($rows=mysql_fetch_array($result))
{
$studid = $rows[0];
$name = $rows[1];
$address = $rows[2];
$class = $rows[3];
$phone = $rows[4];
$email = $rows[5];
$pdf->Cell(25,7,$studid);
$pdf->Cell(30,7,$name);
$pdf->Cell(40,7,$address);
$pdf->Cell(30,7,$class);
$pdf->Cell(30,7,$phone);
$pdf->Cell(30,7,$email);
$pdf->Ln();
}
$pdf->Output();
?>
You should make use of PDF creator libraries like:
FPDF
TCPDF
EzPDF
All three are easy to learn in the order I've put. The documentation of FPDF and EzPDF are very neat and clean. But TCPDF Documentation is not that readable.
Take a look at http://www.tcpdf.org/. I never heard about out of box way to print mysql table data into PDF, but with TCPDF you can programmatically build a PDF file with table filled with data inside it. It also allows to output document created on the fly easily into a browser.
You can use http://www.tcpdf.org/ to create your own PDF. The examples and the doc tabs will help you ^^ (http://www.tcpdf.org/doc/code/classTCPDF.html for all the methods)
You can create a HTML table will all your data and put it in your PDF using the WriteHTML() method.
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