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');
?>

Why there is no post on last four months?

Hi Friends,

If you see the last post was published in Jul 11, 2010.After that I cant post a single line.Because of the following reasons.

  1.  I switch over to another company.
  2. Moved to new busiest city regarding the job.
  3. Huge number of challenging tasks make me busy

From the challenging tasks I learned a lot.So for TT readers you can expect more number of post in the following days.
Have a nice day.

Authorize.net Payment gateway Integeration

Hi Friends,

I have recently completed my payment gateway Integeration using authorize.net.In the full of development I refer one website John Conde that helps me a lot.Whenever I post the doubts wherever John conde will answer as soon as possible.
Here I want to really thank him for his helps and his answers.I suggest you whenever you had the doubts go to his website or developer forum of authorize.net.

I just want to share this information.

Calculate four weeks date from current date and time


Hi Friends,

I found one default function in PHP that is reduce our time to calculate the date an time for the weeeks and months and days.

Below I give the simple example to calculate 4 weeks date from current date.



$weekstocalculate[0]='4 Months'

$text=trim($weekstocalculate[0]);
$text1="now +".$text;

echo date("F jS, Y",($text1));
It gives the output like this
Month date,Year 
Ex:October,2010 

Please try it and give your feedback 

Marquee using Jquery

Hi Friends,

we can make the marquee using the Jquery very simple and easy.

For that we need to implement the simple plugin jscroller

You can find the plugin here.They provide the example also.

So I think I dont need to explain the all the things how  we implement.Advantage is we can make the direction as per our wish.

I like a lot.

Simple tooltip using Jquery

Hi Friends,

The tooltip provide the hints to user when they are in the particular point or particular link.

When the user get the hint they may get the idea with in the seconds.Mostly the hints are used in the form fill up process.

Because we need to make the user fill up the form as soon as possible.The tooltip wont occupy the lot of spaces.

Today I found a tooltip that realy make the tooltip in a single file no need to make lot of JS and CSS.

This is the link

You can get the information. I am really lucky today.Because when I  found this I implemented in my project and got the output.

Sorry for No Post

Now a days it makes very difficult for me with managing time.I am working on some busy projects.It wont give me the chance to make a POST.

 
Real Time Web Analytics