Grab the RSS feed

How to create the PDF on the fly using PHP?

Portable Document Format (PDF) is an open standard for document exchange. The file format created by Adobe Systems in 1993 is used for representing two-dimensional documents in a manner independent of the application software, hardware, and operating system. ---From wikipedia.

In my project we mostly making the Reports using PDF. So I got some experience in creating PDF on the fly using PHP and I want to share it with the readers of TT.

Libraries:

At first I used the FPDF then I moved to TCPDF contain some more features on TCPDF.
You can find the advantages of TCPDF over FPDF here and here .

Creating simple PDF:

As the name indicates its simple one. But here we are going to use the query and separate functionality for the content display.

In coding section you can get the entire Code.

Problems:
Here I am trying to put the column name when I am creating the PDF i.e.Instead of $row[0] I want to put $row[‘columnname’]. It throws an error.

Hint:
Don’t start the output In this page like echo,print,or HTML. Because it throws an error like the output already started.
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);

Use the above lines to turn off header and footer.
Code is here:
<?php
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');

// extend TCPF with custom functions
class MYPDF extends TCPDF {

    public function ColoredTable($header,$data) {
   $this->SetFillColor(255, 0, 0);
        $this->SetTextColor(255);
        $this->SetDrawColor(128, 0, 0);
        $this->SetLineWidth(0.3);
        $this->SetFont('', 'B');
        // Header
  $this->headerdisplay();
  $this->anotherdetaildisplay();
  $header = array('Country', 'Capital', 'Area (sq km)', 'Pop. (thousands)');
  $data=array('0'=>array('Country', 'Capital', 'Area (sq km)', 'Pop. (thousands)'));
        $w = array(40, 35, 40, 45);
        $num_headers = count($header);
        for($i = 0; $i < $num_headers; ++$i) {
            $this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
        }
  
        $this->Ln();
  
        // Color and font restoration
        $this->SetFillColor(224, 235, 255);
        $this->SetTextColor(0);
        $this->SetFont('');
   
        // Data
        $fill = 0;
  $connection=mysql_connect("yourhost","username","password") or die("could not connect db");
  $db=mysql_select_db("db",$connection)  or mysql_errno();
  $sql="SELECT * FROM table";
  $result=mysql_query($sql,$connection);
       /* foreach($data as $row) {*/
    while($row=mysql_fetch_array($result))
    {
            $this->Cell($w[0], 6, $row[0], 'LR', 0, 'L', $fill);
            $this->Cell($w[1], 6, $row[1], 'LR', 0, 'L', $fill);
            $this->Cell($w[2], 6, number_format($row[2]), 'LR', 0, 'R', $fill);
            $this->Cell($w[3], 6, number_format($row[3]), 'LR', 0, 'R', $fill);
            $this->Ln();
            $fill=!$fill;
        }
        $this->Cell(array_sum($w), 0, '', 'T');
  
    }
 
 public function headerdisplay()
 {
    $this->SetFillColor(224, 235, 255);
        $this->SetTextColor(0);
        $this->SetFont('');
  $tbl = <<<EOD
<table cellspacing="0" cellpadding="1" border="0">
    <tr>
        <td>Test<br/>Test<br/>Test</td>
    </tr>
</table>
EOD;
$this->writeHTML($tbl, true, false, false, false, '');
 }
 public function anotherdetaildisplay()
 {
  $this->SetFillColor(224, 235, 255);
        $this->SetTextColor(0);
        $this->SetFont('');
  $tbl = <<<EOD
<table cellspacing="0" cellpadding="1" border="0">
    <tr>
        <td></td>
        <td></td>
        <td>Test at Right<br/>P O BOX 11097<br/>CHICAGO, IL 60611</td>
    </tr>
</table>
EOD;
 
   $this->writeHTML($tbl, true, false, false, false, '');
  $tbl = <<<EOD
<table cellspacing="0" cellpadding="1" border="0">
    <tr>
        <td>LastName FirstName<br/>Address<br/>CITY STATE ZIP</td>
    </tr>
</table>
EOD;
 
   $this->writeHTML($tbl, true, false, false, false, '');
 }
 
}

$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('TechnoTiger');
$pdf->SetTitle('Tutorial');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setLanguageArray($l);
$pdf->SetFont('helvetica', '', 12);
$pdf->AddPage();
$pdf->ColoredTable($header, $data);
$pdf->Output('example_011.pdf', 'I');
?>

0 comments:

  •  
    Real Time Web Analytics