web 2.0

How to usd PHP & PDF - Insert Table

How to usd PHP & PDF - Insert Table Learn PHP how to using PHP and PDF file (Insert Table)

ShotDev Focus:
- PHP & PDF (Insert Table)

Example

php_pdf_table.php


<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<?php
require('fpdf.php');

class PDF extends FPDF
{
//Load data
function LoadData($file)
{
//Read file lines
$lines=file($file);
$data=array();
foreach($lines as $line)
$data[]=explode(';',chop($line));
return $data;
}

//Simple table
function BasicTable($header,$data)
{
//Header
foreach($header as $col)
$this->Cell(40,7,$col,1);
$this->Ln();
//Data
foreach($data as $row)
{
foreach($row as $col)
$this->Cell(40,6,$col,1);
$this->Ln();
}
}

//Better table
function ImprovedTable($header,$data)
{
//Column widths
$w=array(40,35,40,45);
//Header
for($i=0;$i<count($header);$i++)
$this->Cell($w[$i],7,$header[$i],1,0,'C');
$this->Ln();
//Data
foreach($data as $row)
{
$this->Cell($w[0],6,$row[0],'LR');
$this->Cell($w[1],6,$row[1],'LR');
$this->Cell($w[2],6,number_format($row[2]),'LR',0,'R');
$this->Cell($w[3],6,number_format($row[3]),'LR',0,'R');
$this->Ln();
}
//Closure line
$this->Cell(array_sum($w),0,'','T');
}

//Colored table
function FancyTable($header,$data)
{
//Colors, line width and bold font
$this->SetFillColor(255,0,0);
$this->SetTextColor(255);
$this->SetDrawColor(128,0,0);
$this->SetLineWidth(.3);
$this->SetFont('','B');
//Header
$w=array(40,35,40,45);
for($i=0;$i<count($header);$i++)
$this->Cell($w[$i],7,$header[$i],1,0,'C',true);
$this->Ln();
//Color and font restoration
$this->SetFillColor(224,235,255);
$this->SetTextColor(0);
$this->SetFont('');
//Data
$fill=false;
foreach($data as $row)
{
$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');
}
}

$pdf=new PDF();
//Column titles
$header=array('Column 1','Column 2','Column 3','Column 4');
//Data loading
$data=$pdf->LoadData('shotdev3.txt');
$pdf->SetFont('Arial','',10);

$pdf->AddPage();
$pdf->BasicTable($header,$data);

$pdf->AddPage();
$pdf->ImprovedTable($header,$data);

$pdf->AddPage();
$pdf->FancyTable($header,$data);

$pdf->Output("shotdev/mypdf.pdf","F");
?>

PDF Created Click <a href="shotdev/mypdf.pdf">here</a> to Download

</body>
</html>

Create a php file and save to path root-path/myphp/

Run
http://localhost/myphp/php_pdf_table.php

Screenshot

PHP & PDF - Insert Table
.
.
.

Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (1 votes, average: 1.00 out of 10)
Loading ... Loading ...

Leave a Reply

You must be logged in to post a comment.