Jump to content

Generate pdf in php and mysql


Alicia

Recommended Posts

Hi guys,

 

Can any php guru gives me an idea how can I convert the web page I generated by using php and mysql directly to pdf file.

 

I need the pdf file to have exactly the same output and format as what I have in the web page. I used a table to display all the records (mysql_fetch_array function is used) in mysql database and I need the web page (report) to be converted to pdf everytime a user submit a query.

 

I did try fpdf but to no avail.. anyone can suggest the easiet method to accomplish this?

 

Please advise and thanks.

Link to comment
https://forums.phpfreaks.com/topic/99860-generate-pdf-in-php-and-mysql/
Share on other sites

i want to output this table but failed... i really confused with the cell class.. i tried to use the html sample i got from fpdf but failed as well.. please advise :

 

 

             //iterate through the cart, the $product_id is the key and $quantity is the value
             foreach($_SESSION['cart'] as $product_id => $quantity) {    
                 
                                  //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
                 $sql = sprintf("SELECT * FROM `hardwares` WHERE A = %d;",
                                 $product_id); 

                 $result = mysql_query($sql);
                     
                 //Only display the row if there is a product (though there should always be as we have already checked)
                 if(mysql_num_rows($result) > 0) {
                     list($no, $code, $series, $name, $desc, $price, $G, $H, $I, $J, $k, $image, $image2, $image3) = mysql_fetch_row($result);
                   //  list($name, $description, $price) = mysql_fetch_row($result);
                 ?>
                  <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td width="56%" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="5">
                        <tr>
                          <td valign="top"><strong><u><? echo $name ?> (<? echo $code ?>)</u> : RM<? echo $price ?></strong></td>
                        </tr>
                        <tr>
                          <td valign="top"><? echo $desc ?></td>
                        </tr>
                        <tr>
                          <td valign="top"> </td>
                        </tr>

                      </table></td>
                      <td width="44%" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="5">
                        <tr>
                          <td valign="top"><img src="../EN/images/<? echo $image ?>" border="1"></td>
                        </tr>
                        <tr>
                          <td valign="top"><? if($image2 != ""){?>
                              <img src="../EN/images/<? echo $image2 ?>" border="1">
                            <? } ?></td>
                        </tr>

                      </table></td>
                    </tr>
                  </table>
                   <?  
                 }
             
             } // close of for loop
         

   

Hi Barand,

 

Can you check my code please.. it is not showing the table, data and image at all

 


<?php
session_start();
include "../includes/dbconn.php";  
require('fpdf.php');

class PDF extends FPDF
{
var $B;
var $I;
var $U;
var $HREF;

function PDF($orientation='P',$unit='mm',$format='A4')
{
    //Call parent constructor
    $this->FPDF($orientation,$unit,$format);
    //Initialization
    $this->B=0;
    $this->I=0;
    $this->U=0;
    $this->HREF='';
}

function WriteHTML($html)
{
    //HTML parser
    $html=str_replace("\n",' ',$html);
    $a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
    foreach($a as $i=>$e)
    {
        if($i%2==0)
        {
            //Text
            if($this->HREF)
                $this->PutLink($this->HREF,$e);
            else
                $this->Write(5,$e);
        }
        else
        {
            //Tag
            if($e{0}=='/')
                $this->CloseTag(strtoupper(substr($e,1)));
            else
            {
                //Extract attributes
                $a2=explode(' ',$e);
                $tag=strtoupper(array_shift($a2));
                $attr=array();
                foreach($a2 as $v)
                    if(ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3))
                        $attr[strtoupper($a3[1])]=$a3[2];
                $this->OpenTag($tag,$attr);
            }
        }
    }
}

function OpenTag($tag,$attr)
{
    //Opening tag
    if($tag=='B' or $tag=='I' or $tag=='U')
        $this->SetStyle($tag,true);
    if($tag=='A')
        $this->HREF=$attr['HREF'];
    if($tag=='BR')
        $this->Ln(5);
}

function CloseTag($tag)
{
    //Closing tag
    if($tag=='B' or $tag=='I' or $tag=='U')
        $this->SetStyle($tag,false);
    if($tag=='A')
        $this->HREF='';
}

function SetStyle($tag,$enable)
{
    //Modify style and select corresponding font
    $this->$tag+=($enable ? 1 : -1);
    $style='';
    foreach(array('B','I','U') as $s)
        if($this->$s>0)
            $style.=$s;
    $this->SetFont('',$style);
}

function PutLink($URL,$txt)
{
    //Put a hyperlink
    $this->SetTextColor(0,0,255);
    $this->SetStyle('U',true);
    $this->Write(5,$txt,$URL);
    $this->SetStyle('U',false);
    $this->SetTextColor(0);
}
}

// checkout module starts here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

     if($_SESSION['cart']) {    //if the cart isn't empty
         //show the cart
         
      //   echo "<table border=\"1\" padding=\"3\" width=\"40%\">";    //format the cart using a HTML table
         
             //iterate through the cart, the $product_id is the key and $quantity is the value
             foreach($_SESSION['cart'] as $product_id => $quantity) {    
                 
                 //get the name, description and price from the database - this will depend on your database implementation.
                 //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
                 $sql = sprintf("SELECT * FROM `hardware` WHERE A = %d;",
                                 $product_id); 

                 $result = mysql_query($sql);
                     
                 //Only display the row if there is a product (though there should always be as we have already checked)
                 if(mysql_num_rows($result) > 0) {
                     list($no, $code, $series, $name, $desc, $price, $G, $H, $I, $J, $k, $image, $image2, $image3) = mysql_fetch_row($result);
                   //  list($name, $description, $price) = mysql_fetch_row($result);
            $html='
                  <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td width="56%" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="5">
                        <tr>
                          <td valign="top"><strong><u>$name ($code)</u> : US$price</strong></td>
                        </tr>
                        <tr>
                          <td valign="top">$desc</td>
                        </tr>
                        <tr>
                          <td valign="top"> </td>
                        </tr>

                      </table></td>
                      <td width="44%" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="5">
                        <tr>
                          <td valign="top"><img src="../EN/images/$image" border="1"></td>
                        </tr>
                        <tr>
                          <td valign="top">
                              <img src="../EN/images/$image2" border="1">
                            </td>
                        </tr>

                      </table></td>
                    </tr>
                  </table>';
     }
          }   
             } // close of for loop
$pdf=new PDF();
//First page
/*
$pdf->AddPage();
$pdf->SetFont('Arial','',20);
$pdf->Write(5,'To find out what\'s new in this tutorial, click ');
$pdf->SetFont('','U');
$link=$pdf->AddLink();
$pdf->Write(5,'here',$link);
$pdf->SetFont('');
*/
//Second page
$pdf->AddPage();
$pdf->SetLink($link);
$pdf->Image('logo.JPG',10,10,30,0,'','http://www.fpdf.org');
$pdf->SetLeftMargin(45);
$pdf->SetFontSize(14);
$pdf->WriteHTML($html);
$pdf->Output();

?> 

i want to output this table but failed... i really confused with the cell class.. i tried to use the html sample i got from fpdf but failed as well.. please advise :

 

 

I wouldn't attempt to convert the html, I'd just create a method in my pdf clas to lay out each record and call it for each row.

[pre]           

+----------------------------+  +------------------------+

|        cell              |  |                        |

|          1                |  |                        |

+----------------------------+  |      picture 1        |

|        cell              |  |                        |

|          2                |  |                        |

+----------------------------+  |                        |

                                +------------------------+

                                |                        |

                                |                        |

                                |      picture 2        |

                                |                        |

                                |                        |

                                |                        |

                                +------------------------+

[/pre]

sample code

<?php
define('FPDF_FONTPATH','C:/php/fpdf/font/');
require('fpdf.php');
mysql_connect('localhost');
mysql_select_db('test3');

class PDF extends FPDF
{
    var $time_printed;
    
    
    
    // constructor
    function PDF() {
             parent::FPDF();
             $this->time_printed = date('jS M Y g:i a');
             
    }

    //Page header
    function Header()
    {


        //Arial bold 15
        $this->SetFont('Arial','B',18);
        //Title
        $this->Cell(0,20, 'Sample PDF',0,0,'L');
        //Line break
        $this->Ln(15);
    }

    //Page footer
    function Footer()
    {
        //Position at 1.5 cm from bottom
        $this->SetY(-15);
        //Arial italic 8
        $this->SetFont('Arial','I',;
        // time created
        $this->Cell(40,10,$this->time_printed,0,0,'L');
        //Page number
        $this->Cell(0,10,'Page '.$this->PageNo().' / {nb}',0,0,'C');
    }
    
    function printRecord($name, $code, $price, $desc, $im1, $im2)
    {
        $this->SetFont('Arial','B',12);
        $ypos = $this->getY() + 5;
        $this->setY($ypos);
        $this->Cell (100, 6, "$name ($code)", 0, 0);
        $this->Cell (30, 6, "\$$price", 0, 1, 'R');
        $this->SetFont('Arial','I',10);
        $this->Cell (130, 6, 'Description', 0, 1);
        $this->SetFont('Arial','',10);
        $this->Cell(10);      // indent
        $this->Multicell (90, 4, $desc);
        $ypos2 = $this->getY();    // end of text
        
        if ($im1)
        {
            $this->image ($im1, 155, $ypos, 40, 0);
            $ypos += 35;
        }
        if ($im2)
        {
            $this->image ($im2, 155, $ypos, 40, 0);
            $ypos += 35;
        }
        $this->setY(max($ypos2, $ypos));
    }
    
    function printReport()
    {
        $sql = "SELECT name, code, price, description, image1, image2 FROM hardware";
        $res = mysql_query($sql);
        
        while (list($name, $code, $price, $desc, $i1, $i2) = mysql_fetch_row($res))
        {
            $this->printRecord($name, $code, $price, $desc, $i1, $i2);
        }
    }
    
}  # class

//Instantiation of inherited class
$pdf=new PDF();

$pdf->Open();
$pdf->setLeftMargin(15);
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFillColor(200);
$pdf->SetDrawColor(200);

$pdf->printReport();

$pdf->output();
?> 

failed... =(

 

I tried to update your code according to my requirement but no output is displayed except this error :

 

 

My modified code :

<?php
// session used to capture product ID added into the shopping cart
session_start();
include "../includes/dbconn.php";  
//define('FPDF_FONTPATH','fpdf/font/');
require('fpdf.php');

class PDF extends FPDF
{
    var $time_printed;
    
    
    
    // constructor
    function PDF() {
             parent::FPDF();
             $this->time_printed = date('jS M Y g:i a');
             
    }

    //Page header
    function Header()
    {


        //Arial bold 15
        $this->SetFont('Arial','B',18);
        //Title
        $this->Cell(0,20, 'Sample PDF',0,0,'L');
        //Line break
        $this->Ln(15);
    }

    //Page footer
    function Footer()
    {
        //Position at 1.5 cm from bottom
        $this->SetY(-15);
        //Arial italic 8
        $this->SetFont('Arial','I',;
        // time created
        $this->Cell(40,10,$this->time_printed,0,0,'L');
        //Page number
        $this->Cell(0,10,'Page '.$this->PageNo().' / {nb}',0,0,'C');
    }
    
//    function printRecord($name, $code, $price, $desc, $im1, $im2)
  function printRecord($no, $code, $series, $name, $desc, $price, $G, $H, $I, $J, $k, $image, $image2, $image3)
    {
        $this->SetFont('Arial','B',12);
        $ypos = $this->getY() + 5;
        $this->setY($ypos);
        $this->Cell (100, 6, "$name ($code)", 0, 0);
        $this->Cell (30, 6, "\$$price", 0, 1, 'R');
        $this->SetFont('Arial','I',10);
        $this->Cell (130, 6, 'Description', 0, 1);
        $this->SetFont('Arial','',10);
        $this->Cell(10);      // indent
        $this->Multicell (90, 4, $desc);
        $ypos2 = $this->getY();    // end of text
        
        if ($image1)
        {
            $this->image ($images1, 155, $ypos, 40, 0);
            $ypos += 35;
        }
        if ($image2)
        {
            $this->image ($images2, 155, $ypos, 40, 0);
            $ypos += 35;
        }
        if ($image3)
        {
            $this->image ($images3, 155, $ypos, 40, 0);
            $ypos += 35;
        }

	$this->setY(max($ypos2, $ypos));
    }
    
    function printReport()
    {
                   $sql = sprintf("SELECT * FROM `wares` WHERE A = %d;",$product_id); 

        $res = mysql_query($sql);
        
       // while (list($name, $code, $price, $desc, $i1, $i2) = mysql_fetch_row($res))
     while (list($no, $code, $series, $name, $desc, $price, $G, $H, $I, $J, $k, $image, $image2, $image3)= mysql_fetch_row($res))
        {
            //$this->printRecord($name, $code, $price, $desc, $i1, $i2);
		$this->printRecord($no, $code, $series, $name, $desc, $price, $G, $H, $I, $J, $k, $image, $image2, $image3);
	}
    }
    
}  # class

//Instantiation of inherited class
$pdf=new PDF();

$pdf->Open();
$pdf->setLeftMargin(15);
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFillColor(200);
$pdf->SetDrawColor(200);

$pdf->printReport();

$pdf->output();
?> 

 

the following is my db structure :

 

CREATE TABLE `wares` (
  `A` varchar(255) default NULL,
  `B` varchar(255) default NULL,
  `C` varchar(255) default NULL,
  `D` varchar(255) default NULL,
  `E` varchar(255) default NULL,
  `F` varchar(255) default NULL,
  `G` varchar(255) default NULL,
  `H` varchar(255) default NULL,
  `I` varchar(255) default NULL,
  `J` varchar(255) default NULL,
  `K` varchar(255) default NULL,
  `L` varchar(255) default NULL,
  `M` varchar(255) default NULL,
  `N` varchar(255) default NULL,
  `O` varchar(255) default NULL,
  `P` varchar(255) default NULL,
  `Q` varchar(255) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

 

can some guru please advise me what to do to accomplish what I want ?Thank you

I have updated my script and there is no more error but now it is showing blank page.. can some expert give me an idea what is wrong with my code ? thanks

 

<?php

session_start();

include "../includes/dbconn.php"; 

//define('FPDF_FONTPATH','fpdf/font/');

require('fpdf.php');

 

class PDF extends FPDF

{

    var $time_printed;

   

   

   

    // constructor

    function PDF() {

            parent::FPDF();

            $this->time_printed = date('jS M Y g:i a');

           

    }

 

    //Page header

    function Header()

    {

 

 

        //Arial bold 15

        $this->SetFont('Arial','B',18);

        //Title

        $this->Cell(0,20, 'Sample PDF',0,0,'L');

        //Line break

        $this->Ln(15);

    }

 

    //Page footer

    function Footer()

    {

        //Position at 1.5 cm from bottom

        $this->SetY(-15);

        //Arial italic 8

        $this->SetFont('Arial','I',8);

        // time created

        $this->Cell(40,10,$this->time_printed,0,0,'L');

        //Page number

        $this->Cell(0,10,'Page '.$this->PageNo().' / {nb}',0,0,'C');

    }

   

//    function printRecord($name, $code, $price, $desc, $im1, $im2)

  function printRecord($no, $code, $series, $name, $desc, $price, $G, $H, $I, $J, $k, $image, $image2, $image3)

    {

        $this->SetFont('Arial','B',12);

        $ypos = $this->getY() + 5;

        $this->setY($ypos);

        $this->Cell (100, 6, "$name ($code)", 0, 0);

        $this->Cell (30, 6, "\$$price", 0, 1, 'R');

        $this->SetFont('Arial','I',10);

        $this->Cell (130, 6, 'Description', 0, 1);

        $this->SetFont('Arial','',10);

        $this->Cell(10);      // indent

        $this->Multicell (90, 4, $desc);

        $ypos2 = $this->getY();    // end of text

       

        if ($L)

        {

            $this->image ($L, 155, $ypos, 40, 0);

            $ypos += 35;

        }

        if ($M)

        {

            $this->image ($M, 155, $ypos, 40, 0);

            $ypos += 35;

        }

        if ($N)

        {

            $this->image ($N, 155, $ypos, 40, 0);

            $ypos += 35;

        }

 

$this->setY(max($ypos2, $ypos));

    }

   

    function printReport()

    {

 

            //iterate through the cart, the $product_id is the key and $quantity is the value

            foreach($_SESSION['cart'] as $product_id => $quantity) {   

 

                  $sql = sprintf("SELECT * FROM `wares` WHERE A = %d;",$product_id);

 

        $res = mysql_query($sql);

       

      // while (list($name, $code, $price, $desc, $i1, $i2) = mysql_fetch_row($res))

list($no, $code, $series, $name, $desc, $price, $G, $H, $I, $J, $k, $L, $M, $N)= mysql_fetch_array($res);

        {

            //$this->printRecord($name, $code, $price, $desc, $i1, $i2);

$this->printRecord($no, $code, $series, $name, $desc, $price, $G, $H, $I, $J, $k, $L, $M, $N);

}

    }//echo "$code <br>";

    } // close for loop

}  # class

 

//Instantiation of inherited class

$pdf=new PDF();

 

$pdf->Open();

$pdf->setLeftMargin(15);

$pdf->AliasNbPages();

$pdf->AddPage();

$pdf->SetFillColor(200);

$pdf->SetDrawColor(200);

 

$pdf->printReport();

 

$pdf->output();

?>

 

you commented out the echo also "" around the echo not needed :P

 

 

<?php
session_start();
include "../includes/dbconn.php";  
//define('FPDF_FONTPATH','fpdf/font/');
require('fpdf.php');

class PDF extends FPDF
{
    var $time_printed;
    
    
    
    // constructor
    function PDF() {
             parent::FPDF();
             $this->time_printed = date('jS M Y g:i a');
             
    }

    //Page header
    function Header()
    {


        //Arial bold 15
        $this->SetFont('Arial','B',18);
        //Title
        $this->Cell(0,20, 'Sample PDF',0,0,'L');
        //Line break
        $this->Ln(15);
    }

    //Page footer
    function Footer()
    {
        //Position at 1.5 cm from bottom
        $this->SetY(-15);
        //Arial italic 8
        $this->SetFont('Arial','I',;
        // time created
        $this->Cell(40,10,$this->time_printed,0,0,'L');
        //Page number
        $this->Cell(0,10,'Page '.$this->PageNo().' / {nb}',0,0,'C');
    }
    
//    function printRecord($name, $code, $price, $desc, $im1, $im2)
  function printRecord($no, $code, $series, $name, $desc, $price, $G, $H, $I, $J, $k, $image, $image2, $image3)
    {
        $this->SetFont('Arial','B',12);
        $ypos = $this->getY() + 5;
        $this->setY($ypos);
        $this->Cell (100, 6, "$name ($code)", 0, 0);
        $this->Cell (30, 6, "\$$price", 0, 1, 'R');
        $this->SetFont('Arial','I',10);
        $this->Cell (130, 6, 'Description', 0, 1);
        $this->SetFont('Arial','',10);
        $this->Cell(10);      // indent
        $this->Multicell (90, 4, $desc);
        $ypos2 = $this->getY();    // end of text
        
        if ($L)
        {
            $this->image ($L, 155, $ypos, 40, 0);
            $ypos += 35;
        }
        if ($M)
        {
            $this->image ($M, 155, $ypos, 40, 0);
            $ypos += 35;
        }
        if ($N)
        {
            $this->image ($N, 155, $ypos, 40, 0);
            $ypos += 35;
        }
      
      $this->setY(max($ypos2, $ypos));
    }
    
    function printReport()
    {
   
                //iterate through the cart, the $product_id is the key and $quantity is the value
             foreach($_SESSION['cart'] as $product_id => $quantity) {    
   
                   $sql = sprintf("SELECT * FROM `wares` WHERE A = %d;",$product_id); 

        $res = mysql_query($sql);
        
       // while (list($name, $code, $price, $desc, $i1, $i2) = mysql_fetch_row($res))
   list($no, $code, $series, $name, $desc, $price, $G, $H, $I, $J, $k, $L, $M, $N)= mysql_fetch_array($res);
        {
            //$this->printRecord($name, $code, $price, $desc, $i1, $i2);
         $this->printRecord($no, $code, $series, $name, $desc, $price, $G, $H, $I, $J, $k, $L, $M, $N);
      }
    }//
    echo $code;
    } // close for loop
}  # class

//Instantiation of inherited class
$pdf=new PDF();

$pdf->Open();
$pdf->setLeftMargin(15);
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFillColor(200);
$pdf->SetDrawColor(200);

$pdf->printReport();

$pdf->output();

?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.