Jump to content

Recommended Posts

I've a lot of problems with print management of commercial reports.

 

I tried two alternatives:

1. Generation of HTML code formatted using CSS

2. Generation of PDF file

 

I left the first because it isn't possible to define a Footer and a Header for each page (sure?)

 

About the second, i created a PHP class that takes a sql-query and generates a PDF file.

 

Problems:

1. Is there a software (free or not) that allow me to build a report graphically! (ex: I see a page and I drag&drop fields from database)

2. I can't insert subtotals for each coloumn of each page.

3. It's really hard write every kind of report by hand.

 

I know a program (made in Delphi) that allow the user to build his personalized report

softwaredelphibw7.gif

 

Do you know it? Is it possibile something like this in php? How can i build something like this?

 

Atm i use fpdf with these two classes:

 

<?php
require('../include/fpdf/mysql_table.php');
session_start();

/*
@author Salvatore Baglieri
@filename elenco_pazienti.php
*/
class PDF extends PDF_MySQL_Table {
function Header() {
    //Logo
   
    //Arial bold 15
    $this->SetFont('Arial','B',20);
    //Move to the right
    //$this->Cell(80);
    //Title
    $this->Cell(100,15,'Elenco dei pazienti',1,0,'C');
    $this->SetFont('Arial','',;
    $this->Image('../immagini/logo.png', ($this->GetX())+50, 10, '', 18);
    // $this->MultiCell(0,5,$_SESSION['query'],1,1,'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',;
    $this->Write(10,'Documento stampato il '.date('d-m-Y').' alle '.date('h:i').' con xxx - ');
    $this->SetTextColor(0,0,255);
    $this->SetFont('','U');
    $this->Write(10,'Digital 2B s.n.c.','http://www.digital2b.com');
    $this->SetTextColor(0,0,0);
    $this->SetFont('Arial','I',;
    // $this->Image('immagini/xxx.jpg',10,280, 10, 10);
   
    $this->Line(10, 196, 285, 196);
    //Page number
    $this->Cell(0,10,'Pagina '.$this->PageNo().'/{nb}',0,0,'R');
}


}

//Connect to database
mysql_connect('xxx','xxx','xxx');
mysql_select_db('xxx');

$pdf=new PDF();
$pdf->SetCompression(true);
$pdf->SetTitle('Elenco_Pazienti');
$pdf->AliasNbPages();
$pdf->Open();
$pdf->AddPage('L');

//Second table: specify 3 columns
$pdf->AddCol('IdAnagrafica',5,'Id','L');
$pdf->AddCol('Cognome',35,'Cognome','L');
$pdf->AddCol('Nome',35,'Nome','L');
//$pdf->AddCol('Sesso',12,'Sesso','C');
$pdf->AddCol('DatadiNascita',22,'Data Nascita','C');
$pdf->AddCol('CodiceFiscale',36,'Codice Fiscale','L');
$pdf->AddCol('Professione',45,'Professione','L');
$pdf->AddCol('Domicilio',45,'Domicilio','L');
$pdf->AddCol('ComunediResidenza',45,'Comune','L');
$prop=array('HeaderColor'=>array(255,150,100),
            'color1'=>array(210,245,255),
            'color2'=>array(255,255,210),
            'padding'=>2);
$pdf->SetFont('Arial','',9);
$pdf->Table($_SESSION['query'],$prop);
$pdf->Output("Elenco_Pazienti.pdf", "D");
?>

 

 

<?php

/*
@filename mysql_table.php
*/

require('fpdf.php');

class PDF_MySQL_Table extends FPDF {
var $ProcessingTable=false;
var $aCols=array();
var $TableX;
var $HeaderColor;
var $RowColors;
var $ColorIndex;


function Header() {
    //Print the table header if necessary
    if($this->ProcessingTable)
        $this->TableHeader();
}


function TableHeader() {
    //$this->SetFont('Arial','B',12);
    $this->SetFont('','B','');
    $this->SetX($this->TableX);
    $fill=!empty($this->HeaderColor);
    if($fill)
        $this->SetFillColor($this->HeaderColor[0],$this->HeaderColor[1],$this->HeaderColor[2]);
    foreach($this->aCols as $col)
        $this->Cell($col['w'],6,$col['c'],1,0,'C',$fill);
    $this->Ln();
    $this->SetFont('','','');
}


function Row($data)
{
    $this->SetX($this->TableX);
    $ci=$this->ColorIndex;
    $fill=!empty($this->RowColors[$ci]);
    if($fill)
        $this->SetFillColor($this->RowColors[$ci][0],$this->RowColors[$ci][1],$this->RowColors[$ci][2]);
    foreach($this->aCols as $col)
        $this->Cell($col['w'],5,$data[$col['f']],1,0,$col['a'],$fill);
    $this->Ln();
    $this->ColorIndex=1-$ci;
}

function CalcWidths($width,$align)
{
    //Compute the widths of the columns
    $TableWidth=0;
    foreach($this->aCols as $i=>$col)
    {
        $w=$col['w'];
        if($w==-1)
            $w=$width/count($this->aCols);
        elseif(substr($w,-1)=='%')
            $w=$w/100*$width;
        $this->aCols[$i]['w']=$w;
        $TableWidth+=$w;
    }
    //Compute the abscissa of the table
    if($align=='C')
        $this->TableX=max(($this->w-$TableWidth)/2,0);
    elseif($align=='R')
        $this->TableX=max($this->w-$this->rMargin-$TableWidth,0);
    else
        $this->TableX=$this->lMargin;
}

function AddCol($field=-1,$width=-1,$caption='',$align='L')
{
    //Add a column to the table
    if($field==-1)
        $field=count($this->aCols);
    $this->aCols[]=array('f'=>$field,'c'=>$caption,'w'=>$width,'a'=>$align);
}

function Table($query,$prop=array())
{
    //Issue query
    $res=mysql_query($query) or die('Error: '.mysql_error()."<BR>Query: $query");
    //Add all columns if none was specified
    if(count($this->aCols)==0)
    {
        $nb=mysql_num_fields($res);
        for($i=0;$i<$nb;$i++)
            $this->AddCol();
    }
    //Retrieve column names when not specified
    foreach($this->aCols as $i=>$col)
    {
        if($col['c']=='')
        {
            if(is_string($col['f']))
                $this->aCols[$i]['c']=ucfirst($col['f']);
            else
                $this->aCols[$i]['c']=ucfirst(mysql_field_name($res,$col['f']));
        }
    }
    //Handle properties
    if(!isset($prop['width']))
        $prop['width']=0;
    if($prop['width']==0)
        $prop['width']=$this->w-$this->lMargin-$this->rMargin;
    if(!isset($prop['align']))
        $prop['align']='C';
    if(!isset($prop['padding']))
        $prop['padding']=$this->cMargin;
    $cMargin=$this->cMargin;
    $this->cMargin=$prop['padding'];
    if(!isset($prop['HeaderColor']))
        $prop['HeaderColor']=array();
    $this->HeaderColor=$prop['HeaderColor'];
    if(!isset($prop['color1']))
        $prop['color1']=array();
    if(!isset($prop['color2']))
        $prop['color2']=array();
    $this->RowColors=array($prop['color1'],$prop['color2']);
    //Compute column widths
    $this->CalcWidths($prop['width'],$prop['align']);
    //Print header
     $this->TableHeader();
    //Print rows
    //$this->SetFont('Arial','',11);
    $this->ColorIndex=0;
    $this->ProcessingTable=true;
    while($row=mysql_fetch_array($res))
        $this->Row($row);
    $this->ProcessingTable=false;
    $this->cMargin=$cMargin;
    $this->aCols=array();
}
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/63298-report-printing-using-pdf/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.