vmirandamx
01-23-2007, 05:25 PM
Hi I have a problen creating a PDF, my code is this:
<?php
define('FPDF_FONTPATH','../../pdf/font/');
require('../../pdf/fpdf.php');
class PDF extends FPDF {
var $tablewidths;
var $headerset;
var $footerset;
function _beginpage($orientation) {
$this->page++;
if(!$this->pages[$this->page])
$this->pages[$this->page]='';
$this->state=2;
$this->x=$this->lMargin;
$this->y=$this->tMargin;
$this->lasth=0;
$this->FontFamily='';
if(!$orientation)
$orientation=$this->DefOrientation;
else
{
$orientation=strtoupper($orientation{0});
if($orientation!=$this->DefOrientation)
$this->OrientationChanges[$this->page]=true;
}
if($orientation!=$this->CurOrientation)
{
if($orientation=='P')
{
$this->wPt=$this->fwPt;
$this->hPt=$this->fhPt;
$this->w=$this->fw;
$this->h=$this->fh;
}
else
{
$this->wPt=$this->fhPt;
$this->hPt=$this->fwPt;
$this->w=$this->fh;
$this->h=$this->fw;
}
$this->PageBreakTrigger=$this->h-$this->bMargin;
$this->CurOrientation=$orientation;
}
}
function Header()
{
global $maxY;
if(!$this->headerset[$this->page]) {
foreach($this->tablewidths as $width) {
$fullwidth += $width;
}
$this->SetY(($this->tMargin) - ($this->FontSizePt/$this->k)*2);
$this->cellFontSize = $this->FontSizePt ;
$this->SetFont('Arial','',( ( $this->titleFontSize) ? $this->titleFontSize : $this->FontSizePt ));
$this->Cell(0,$this->FontSizePt,$this->titleText,0,1,'C');
$l = ($this->lMargin);
$this->SetFont('Arial','',$this->cellFontSize);
foreach($this->colTitles as $col => $txt) {
$this->SetXY($l,($this->tMargin));
$this->MultiCell($this->tablewidths[$col], $this->FontSizePt,$txt);
$l += $this->tablewidths[$col] ;
$maxY = ($maxY < $this->getY()) ? $this->getY() : $maxY ;
}
$this->SetXY($this->lMargin,$this->tMargin);
$this->setFillColor(200,200,200);
$l = ($this->lMargin);
foreach($this->colTitles as $col => $txt) {
$this->SetXY($l,$this->tMargin);
$this->cell($this->tablewidths[$col],$maxY-($this->tMargin),'',1,0,'L',1);
$this->SetXY($l,$this->tMargin);
$this->MultiCell($this->tablewidths[$col],$this->FontSizePt,$txt,0,'C');
$l += $this->tablewidths[$col];
}
$this->setFillColor(255,255,255);
$this->headerset[$this->page] = 1;
}
$this->SetY($maxY);
}
function Footer() {
if(!$this->footerset[$this->page]) {
$this->SetY(-15);
$fecha = date('d-M-Y / h:i:s A');
$this->Cell(0,10,' STSI - Banjercito, S.N.C. Pagina '.$this->PageNo().'/{nb} - '.$fecha,0,0,'C');
$this->footerset[$this->page] = 1;
}
}
function morepagestable($lineheight=8) {
$l = $this->lMargin;
$startheight = $h = $this->GetY();
$startpage = $currpage = $this->page;
foreach($this->tablewidths as $width) {
$fullwidth += $width;
}
$row = 0;
while($data=mysql_fetch_row($this->results)) {
$this->page = $currpage;
$this->Line($l,$h,$fullwidth+$l,$h);
foreach($data as $col => $txt) {
$this->page = $currpage;
$this->SetXY($l,$h);
$this->MultiCell($this->tablewidths[$col],$lineheight,$txt,0,$this->colAlign[$col]);
$l += $this->tablewidths[$col];
if($tmpheight[$row.'-'.$this->page] < $this->GetY()) {
$tmpheight[$row.'-'.$this->page] = $this->GetY();
}
if($this->page > $maxpage)
$maxpage = $this->page;
unset($data[$col]);
}
$h = $tmpheight[$row.'-'.$maxpage];
$l = $this->lMargin;
$currpage = $maxpage;
unset($data[$row]);
$row++ ;
}
$this->page = $maxpage;
$this->Line($l,$h,$fullwidth+$l,$h);
for($i = $startpage; $i <= $maxpage; $i++) {
$this->page = $i;
$l = $this->lMargin;
$t = ($i == $startpage) ? $startheight : $this->tMargin;
$lh = ($i == $maxpage) ? $h : $this->h-$this->bMargin;
$this->Line($l,$t,$l,$lh);
foreach($this->tablewidths as $width) {
$l += $width;
$this->Line($l,$t,$l,$lh);
}
}
$this->page = $maxpage;
}
function connect($host='localhost',$username='test',$password='',$db='audit'){
$this->conn = mysql_connect($host,$username,$password) or die( mysql_error() );
mysql_select_db($db,$this->conn) or die( mysql_error() );
return true;
}
function query($query){
$this->results = mysql_query($query,$this->conn);
$this->numFields = mysql_num_fields($this->results);
}
function mysql_report($query,$dump=false,$attr=array()){
foreach($attr as $key=>$val){
$this->$key = $val ;
}
$this->query($query);
if(!isset($this->tablewidths)){
$this->sColWidth = (($this->w-$this->lMargin-$this->rMargin))/$this->numFields;
for($i=0;$i<$this->numFields;$i++){
$stringWidth = $this->getstringwidth(mysql_field_name($this->results,$i)) + 6 ;
if( ($stringWidth) < $this->sColWidth){
$colFits[$i] = $stringWidth ;
}
$this->colTitles[$i] = mysql_field_name($this->results,$i) ;
switch (mysql_field_type($this->results,$i)){
case 'int':
$this->colAlign[$i] = 'R';
break;
default:
$this->colAlign[$i] = 'L';
}
}
while($row=mysql_fetch_row($this->results)){
foreach($colFits as $key=>$val){
$stringWidth = $this->getstringwidth($row[$key]) + 6 ;
if( ($stringWidth) > $this->sColWidth ){
unset($colFits[$key]);
}else{
if( ($stringWidth) > $val ){
$colFits[$key] = ($stringWidth) ;
}
}
}
}
foreach($colFits as $key=>$val){
$this->tablewidths[$key] = $val;
$totAlreadyFitted += $val;
}
$surplus = (sizeof($colFits)*$this->sColWidth) - ($totAlreadyFitted);
for($i=0;$i<$this->numFields;$i++){
if(!in_array($i,array_keys($colFits))){
$this->tablewidths[$i] = $this->sColWidth + ($surplus/(($this->numFields)-sizeof($colFits)));
}
}
ksort($this->tablewidths);
if($dump){
Header('Content-type: text/plain');
for($i=0;$i<$this->numFields;$i++){
if(strlen(mysql_field_name($this->results,$i))>$flength){
$flength = strlen(mysql_field_name($this->results,$i));
}
}
switch($this->k){
case 72/25.4:
$unit = 'millimeters';
break;
case 72/2.54:
$unit = 'centimeters';
break;
case 72:
$unit = 'inches';
break;
default:
$unit = 'points';
}
print "All measurements in $unit\n\n";
for($i=0;$i<$this->numFields;$i++){
printf("%-{$flength}s : %-10s : %10f\n",
mysql_field_name($this->results,$i),
mysql_field_type($this->results,$i),
$this->tablewidths[$i] );
}
print "\n\n";
print "\$pdf->tablewidths=\n\tarray(\n\t\t";
for($i=0;$i<$this->numFields;$i++){
($i<($this->numFields-1)) ?
print $this->tablewidths[$i].", /* ".mysql_field_name($this->results,$i)." */\n\t\t":
print $this->tablewidths[$i]." /* ".mysql_field_name($this->results,$i)." */\n\t\t";
}
print "\n\t);\n";
exit;
}
} else {
for($i=0;$i<$this->numFields;$i++){
$this->colTitles[$i] = mysql_field_name($this->results,$i) ;
switch (mysql_field_type($this->results,$i)){
case 'int':
$this->colAlign[$i] = 'R';
break;
default:
$this->colAlign[$i] = 'L';
}
}
}
mysql_data_seek($this->results,0);
$this->Open();
$this->setY($this->tMargin);
$this->AddPage();
$this->morepagestable($this->FontSizePt);
$this->Output();
}
}
$pdf = new PDF('L','pt','A3');
$pdf->SetFont('Arial','',11.5);
$pdf->AliasNbPages();
$pdf->connect('localhost','root','','audit_ctas');
$del=$_POST['DEL'];
$al=$_POST['AL'];
$attr=array('titleFontSize'=>18,'titleText'=>'Reporte de Consulta a Cuentas de Funcionarios Correspondiente al periodo del $del al $al de Febrero del 2007');
$diadel="2007-01-".$del;
$diaal="2007-01-".$al;
$pdf->mysql_report("select * from febrero2007 where fecha_con_cta between '$diadel' and '$diaal' ORDER BY fecha_con_cta",false,$attr);
?>
############ FIN CODIGO ##############
and i have this answare in my web page:
Warning: mysql_data_seek(): Offset 0 is invalid for MySQL result index 5 (or the query data is unbuffered) in D:\Website\feb07\dias_ctas_func\ctas_func.php on line 305
Warning: Cannot modify header information - headers already sent by (output started at D:\Website\feb07\dias_ctas_func\ctas_func.php:305) in D:\Website\pdf\fpdf.php on line 1022
FPDF error: Some data has already been output to browser, can't send PDF file
but if not use $del, $al, $diadel and $diaal i can obtain good results, but this is not good form to work i need use the form to generate the pdf.
Some body can help me, Thanks.
<?php
define('FPDF_FONTPATH','../../pdf/font/');
require('../../pdf/fpdf.php');
class PDF extends FPDF {
var $tablewidths;
var $headerset;
var $footerset;
function _beginpage($orientation) {
$this->page++;
if(!$this->pages[$this->page])
$this->pages[$this->page]='';
$this->state=2;
$this->x=$this->lMargin;
$this->y=$this->tMargin;
$this->lasth=0;
$this->FontFamily='';
if(!$orientation)
$orientation=$this->DefOrientation;
else
{
$orientation=strtoupper($orientation{0});
if($orientation!=$this->DefOrientation)
$this->OrientationChanges[$this->page]=true;
}
if($orientation!=$this->CurOrientation)
{
if($orientation=='P')
{
$this->wPt=$this->fwPt;
$this->hPt=$this->fhPt;
$this->w=$this->fw;
$this->h=$this->fh;
}
else
{
$this->wPt=$this->fhPt;
$this->hPt=$this->fwPt;
$this->w=$this->fh;
$this->h=$this->fw;
}
$this->PageBreakTrigger=$this->h-$this->bMargin;
$this->CurOrientation=$orientation;
}
}
function Header()
{
global $maxY;
if(!$this->headerset[$this->page]) {
foreach($this->tablewidths as $width) {
$fullwidth += $width;
}
$this->SetY(($this->tMargin) - ($this->FontSizePt/$this->k)*2);
$this->cellFontSize = $this->FontSizePt ;
$this->SetFont('Arial','',( ( $this->titleFontSize) ? $this->titleFontSize : $this->FontSizePt ));
$this->Cell(0,$this->FontSizePt,$this->titleText,0,1,'C');
$l = ($this->lMargin);
$this->SetFont('Arial','',$this->cellFontSize);
foreach($this->colTitles as $col => $txt) {
$this->SetXY($l,($this->tMargin));
$this->MultiCell($this->tablewidths[$col], $this->FontSizePt,$txt);
$l += $this->tablewidths[$col] ;
$maxY = ($maxY < $this->getY()) ? $this->getY() : $maxY ;
}
$this->SetXY($this->lMargin,$this->tMargin);
$this->setFillColor(200,200,200);
$l = ($this->lMargin);
foreach($this->colTitles as $col => $txt) {
$this->SetXY($l,$this->tMargin);
$this->cell($this->tablewidths[$col],$maxY-($this->tMargin),'',1,0,'L',1);
$this->SetXY($l,$this->tMargin);
$this->MultiCell($this->tablewidths[$col],$this->FontSizePt,$txt,0,'C');
$l += $this->tablewidths[$col];
}
$this->setFillColor(255,255,255);
$this->headerset[$this->page] = 1;
}
$this->SetY($maxY);
}
function Footer() {
if(!$this->footerset[$this->page]) {
$this->SetY(-15);
$fecha = date('d-M-Y / h:i:s A');
$this->Cell(0,10,' STSI - Banjercito, S.N.C. Pagina '.$this->PageNo().'/{nb} - '.$fecha,0,0,'C');
$this->footerset[$this->page] = 1;
}
}
function morepagestable($lineheight=8) {
$l = $this->lMargin;
$startheight = $h = $this->GetY();
$startpage = $currpage = $this->page;
foreach($this->tablewidths as $width) {
$fullwidth += $width;
}
$row = 0;
while($data=mysql_fetch_row($this->results)) {
$this->page = $currpage;
$this->Line($l,$h,$fullwidth+$l,$h);
foreach($data as $col => $txt) {
$this->page = $currpage;
$this->SetXY($l,$h);
$this->MultiCell($this->tablewidths[$col],$lineheight,$txt,0,$this->colAlign[$col]);
$l += $this->tablewidths[$col];
if($tmpheight[$row.'-'.$this->page] < $this->GetY()) {
$tmpheight[$row.'-'.$this->page] = $this->GetY();
}
if($this->page > $maxpage)
$maxpage = $this->page;
unset($data[$col]);
}
$h = $tmpheight[$row.'-'.$maxpage];
$l = $this->lMargin;
$currpage = $maxpage;
unset($data[$row]);
$row++ ;
}
$this->page = $maxpage;
$this->Line($l,$h,$fullwidth+$l,$h);
for($i = $startpage; $i <= $maxpage; $i++) {
$this->page = $i;
$l = $this->lMargin;
$t = ($i == $startpage) ? $startheight : $this->tMargin;
$lh = ($i == $maxpage) ? $h : $this->h-$this->bMargin;
$this->Line($l,$t,$l,$lh);
foreach($this->tablewidths as $width) {
$l += $width;
$this->Line($l,$t,$l,$lh);
}
}
$this->page = $maxpage;
}
function connect($host='localhost',$username='test',$password='',$db='audit'){
$this->conn = mysql_connect($host,$username,$password) or die( mysql_error() );
mysql_select_db($db,$this->conn) or die( mysql_error() );
return true;
}
function query($query){
$this->results = mysql_query($query,$this->conn);
$this->numFields = mysql_num_fields($this->results);
}
function mysql_report($query,$dump=false,$attr=array()){
foreach($attr as $key=>$val){
$this->$key = $val ;
}
$this->query($query);
if(!isset($this->tablewidths)){
$this->sColWidth = (($this->w-$this->lMargin-$this->rMargin))/$this->numFields;
for($i=0;$i<$this->numFields;$i++){
$stringWidth = $this->getstringwidth(mysql_field_name($this->results,$i)) + 6 ;
if( ($stringWidth) < $this->sColWidth){
$colFits[$i] = $stringWidth ;
}
$this->colTitles[$i] = mysql_field_name($this->results,$i) ;
switch (mysql_field_type($this->results,$i)){
case 'int':
$this->colAlign[$i] = 'R';
break;
default:
$this->colAlign[$i] = 'L';
}
}
while($row=mysql_fetch_row($this->results)){
foreach($colFits as $key=>$val){
$stringWidth = $this->getstringwidth($row[$key]) + 6 ;
if( ($stringWidth) > $this->sColWidth ){
unset($colFits[$key]);
}else{
if( ($stringWidth) > $val ){
$colFits[$key] = ($stringWidth) ;
}
}
}
}
foreach($colFits as $key=>$val){
$this->tablewidths[$key] = $val;
$totAlreadyFitted += $val;
}
$surplus = (sizeof($colFits)*$this->sColWidth) - ($totAlreadyFitted);
for($i=0;$i<$this->numFields;$i++){
if(!in_array($i,array_keys($colFits))){
$this->tablewidths[$i] = $this->sColWidth + ($surplus/(($this->numFields)-sizeof($colFits)));
}
}
ksort($this->tablewidths);
if($dump){
Header('Content-type: text/plain');
for($i=0;$i<$this->numFields;$i++){
if(strlen(mysql_field_name($this->results,$i))>$flength){
$flength = strlen(mysql_field_name($this->results,$i));
}
}
switch($this->k){
case 72/25.4:
$unit = 'millimeters';
break;
case 72/2.54:
$unit = 'centimeters';
break;
case 72:
$unit = 'inches';
break;
default:
$unit = 'points';
}
print "All measurements in $unit\n\n";
for($i=0;$i<$this->numFields;$i++){
printf("%-{$flength}s : %-10s : %10f\n",
mysql_field_name($this->results,$i),
mysql_field_type($this->results,$i),
$this->tablewidths[$i] );
}
print "\n\n";
print "\$pdf->tablewidths=\n\tarray(\n\t\t";
for($i=0;$i<$this->numFields;$i++){
($i<($this->numFields-1)) ?
print $this->tablewidths[$i].", /* ".mysql_field_name($this->results,$i)." */\n\t\t":
print $this->tablewidths[$i]." /* ".mysql_field_name($this->results,$i)." */\n\t\t";
}
print "\n\t);\n";
exit;
}
} else {
for($i=0;$i<$this->numFields;$i++){
$this->colTitles[$i] = mysql_field_name($this->results,$i) ;
switch (mysql_field_type($this->results,$i)){
case 'int':
$this->colAlign[$i] = 'R';
break;
default:
$this->colAlign[$i] = 'L';
}
}
}
mysql_data_seek($this->results,0);
$this->Open();
$this->setY($this->tMargin);
$this->AddPage();
$this->morepagestable($this->FontSizePt);
$this->Output();
}
}
$pdf = new PDF('L','pt','A3');
$pdf->SetFont('Arial','',11.5);
$pdf->AliasNbPages();
$pdf->connect('localhost','root','','audit_ctas');
$del=$_POST['DEL'];
$al=$_POST['AL'];
$attr=array('titleFontSize'=>18,'titleText'=>'Reporte de Consulta a Cuentas de Funcionarios Correspondiente al periodo del $del al $al de Febrero del 2007');
$diadel="2007-01-".$del;
$diaal="2007-01-".$al;
$pdf->mysql_report("select * from febrero2007 where fecha_con_cta between '$diadel' and '$diaal' ORDER BY fecha_con_cta",false,$attr);
?>
############ FIN CODIGO ##############
and i have this answare in my web page:
Warning: mysql_data_seek(): Offset 0 is invalid for MySQL result index 5 (or the query data is unbuffered) in D:\Website\feb07\dias_ctas_func\ctas_func.php on line 305
Warning: Cannot modify header information - headers already sent by (output started at D:\Website\feb07\dias_ctas_func\ctas_func.php:305) in D:\Website\pdf\fpdf.php on line 1022
FPDF error: Some data has already been output to browser, can't send PDF file
but if not use $del, $al, $diadel and $diaal i can obtain good results, but this is not good form to work i need use the form to generate the pdf.
Some body can help me, Thanks.