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
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 have a problem. I write codes of to display images from mysql database to FPDF but the images are displayed as overlapping (in same position)
<?php
include("connection.php");
$que1=mysql_query("select * from TableName);
ob_start();
require('fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
while($rw=mysql_fetch_array($que1))
{
$profile=$rw['profile'];
$pdf->Image($profile,10,'',30);
}
$pdf->Output();
ob_end_flush();
?>
How can I display my images in vertical form?
Please anyone can help me.
The problem is with line
$pdf->Image($profile,10,'',30);
The first attribute is "file", Second is the axis X position, 3rd is axis Y, fourth is width.
Refference: Documentation
Please give different x,y values to prevent overlapping
From documentation I can see that Image method take [x, y] coordinates. So just for every image calculate new position:
$currentY = 0;
while ($rw = mysql_fetch_array()) {
$imageSize = $this->getSize($rw['profile']);
$pdf->Image($profile, 10, $currentY, 30);
$currentY += $imageSize['height'];
}
OR try setting y to null - Image($profile, 10, null, 30)
I need to create an excel document with only 2 col. The first one will contain multiple images 150px large and the seconde one will contain a web code. For some reason only one image is added and the file appears to be currupted. Not sure what i am doing wrong ...
<?php
include("../../init.php");
if (is_numeric($_GET[groupe])){
define( 'PCLZIP_TEMPORARY_DIR', $_CONFIG['upload']['root'].'/cache' );
// filename for download
$groupe = mysql_query("SELECT * FROM photographe_groupe WHERE id='$_GET[groupe]'");
$records = "Groupe - $groupe[nom].xlsx";
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition:inline;filename='.$records);
$workbook = new PHPExcel;
$sheet = $workbook->getActiveSheet();
$i=0;
$select = mysql_query("SELECT * FROM photographe_images WHERE sid='$_GET[groupe]' group by `paire`");
while($photo = mysql_fetch_array($select)){
// Table header
if ($i=="1"){
$sheet->setCellValue("A1",'Photo(s)');
$sheet->setCellValue('B1','Code Web');
$i++;
}
// Set images in col 1
$select1 = mysql_query("SELECT * FROM photographe_images WHERE paire='$photo[paire]'");
while($photo1 = mysql_fetch_array($select1)){
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName($photo1[img]);
$objDrawing->setDescription($photo1[img]);
$objDrawing->setWorksheet($workbook->getActiveSheet());
$objDrawing->setPath($_CONFIG['upload']['root'].'/userfiles/photos/'.$photo1[img]);
$objDrawing->setWidth(150);
$objDrawing->setCoordinates('A'.$i);
}
// Set web code in col 2
$sheet->setCellValue("B$i",$photo[code_web]);
$i++;
}
}
$workbook->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
$writer = new PHPExcel_Writer_Excel2007($workbook);
PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);
$writer->save('php://output');
?>
You can download and output demo here
Just a few points to note before I try taking a look at your file:
The first point to note here is that Excel doesn't store images in cells, it "overlays" images, and doesn't particularly care where cell boundaries occur, so an image can be a fixed size irrespective of how many cells it overlays
When you specify a coordinate for a image, you're relating the top-left corner of that image to be placed at the top-left corner of that cell.... where the bottom and/or right corner of the image lies isn't determined by the cells at all. Setting the row height or column width to autosize will not be affected by the image in any way, because the image is overlaid, not a content of the cell
You can, offset the image from the top-left corner of the related cell by using the Drawing object's setOffsetX() and setOffsetY() methods.
You can link more than one image to be placed relative to the same cell's top-left corner using different offset values so they don't overlap, but you need to work out the offsets from the size of the images
I am trying to create barcode using mpdf. while creating, its automatically changing height and width. Please give me solution to fix height and width even though characters are different.
<barcode code="abcdefghijklmnopqrstuvwxyz1234" type="C39E+"
class="barcode" size="3" height="3" />
want to generate 5 barcodes on a page with equal space.
page size 50*100mm.
According to mpdf documentation and examples provided when you download the scripts from their website: http://www.mpdf1.com/mpdf/index.php?page=Download
If you have a look at their barcode example example37_barcodes.php (examples/example37_barcodes.php), if you want to modify the size for the C39E+ barcode type you will have to do this:
These barcodes are all of variable length depending on the code entered. There is no recommended maximum size for any of these specs, but all recommend a minimum X-dimension (width of narrowest bar) as 7.5mil (=0.19mm). The default used here is twice the minimum i.e. X-dim = 0.38mm.
The specifications give a minimum height of 15% of the barcode length (which can be variable). The bar height in mPDF is set to a default value of 10mm. 'size' will scale the barcode in both dimensions. mPDF will accept any number, but bear in mind that size="0.5" will set the bar width to the minimum. The 'height' attribute further allows scaling - this factor is applied to already scaled barcode. Thus size="2" height="0.5" will give a barcode twice the default width (X-dim=0.76mm) and at the default height set in mPDF i.e. 10mm.
The results may vary according to the value passed to the barcode class, so I think if you want a fixed width and height you should try an external library that creates an barcode image file with the desired size or forcing the barcode to adjust to a fixed row or div in a table.
I had the same problem and tried the following using an external library passing values to a function:
$codigoBarras = "<img src='barcode/script/html/image.php?code=code128&o=1&t=12&r=1&text=$productoID&f1=Arial.ttf&f2=0&a1=A&a2=' width='110' height='25'>";
image.php file:
<?php
if(isset($_GET['code']) && isset($_GET['t']) && isset($_GET['r']) && isset($_GET['text']) && isset($_GET['f1']) && isset($_GET['f2']) && isset($_GET['o']) && isset($_GET['a1']) && isset($_GET['a2'])){
define('IN_CB',true);
require('config.php');
require($class_dir.'/index.php');
require($class_dir.'/FColor.php');
require($class_dir.'/BarCode.php');
require($class_dir.'/FDrawing.php');
require($class_dir.'/Font.php');
if(include($class_dir.'/'.$_GET['code'].'.barcode.php')){
if($_GET['f1'] !== '0' && intval($_GET['f2']) >= 1){
$font = new Font($class_dir.'/font/'.$_GET['f1'], intval($_GET['f2']));
} else {
$font = 0;
}
$color_black = new FColor(0,0,0);
$color_white = new FColor(255,255,255);
if(!empty($_GET['a2']))
$code_generated =& new $_GET['code']($_GET['t'],$color_black,$color_white,$_GET['r'],$_GET['text'],$font,$_GET['a1'],$_GET['a2']);
elseif(!empty($_GET['a1']))
$code_generated =& new $_GET['code']($_GET['t'],$color_black,$color_white,$_GET['r'],$_GET['text'],$font,$_GET['a1']);
else
$code_generated =& new $_GET['code']($_GET['t'],$color_black,$color_white,$_GET['r'],$_GET['text'],$font);
$drawing =& new FDrawing('',$color_white);
$drawing->add_barcode($code_generated);
$drawing->draw_all();
$drawing->finish(intval($_GET['o']));
}
else{
header('Content: image/png');
readfile('error.png');
}
}
else{
header('Content: image/png');
readfile('error.png');
}
?>
<barcode type="EAN13" code="978-0-9542246-0" text="0" class="barcode" size="1" height="0.5"/>
always size ='1'
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.