PHP製作PDF報表

  • 80590
  • 0
  • PHP
  • 2013-09-08

PHP製作PDF報表

首先可以到FPDF網站下載程式,當然FPDF的網站有教學也值得前往觀看http://www.fpdf.org/

或直接點選這邊下載fpdf16.zip

因為FPDF的網站是英文的,因此若要使用中文字,

則需要再下載chinese-unicode.rar

好啦,將上面的檔案下載之後解壓縮放入網站的資料夾就可以開始囉

 

test.php

<?php
$hostname = "localhost";   //主機名稱
$database = "test";              //資料庫名稱
$username = "xxxx";            //資料庫使用者帳號
$password = "****";            //資料庫使用者密碼
 
$test = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR); 
mysql_select_db("test",$test);
mysql_query("set names 'utf8'");

 

?>

 

reportSQL.php

<?php  session_start(); 
 
mysql_select_db($database, $test);
 
//index(首頁)
$query_index = "SELECT * FROM `flow` WHERE date = CURDATE( ) AND page = 'index'";
$index = mysql_query($query_index, $test) or die(mysql_error());
$totalRows_index = mysql_num_rows($index);
$query_index2 = "SELECT * FROM `flow` WHERE page = 'index'";
$index2 = mysql_query($query_index2, $test) or die(mysql_error());
$totalRows_index2 = mysql_num_rows($index2);
 
//introduce(品牌介紹)
$query_introduce = "SELECT * FROM `flow` WHERE date = CURDATE( ) AND page = 'introduce'";
$introduce = mysql_query($query_introduce, $test) or die(mysql_error());
$totalRows_introduce = mysql_num_rows($introduce);
$query_introduce2 = "SELECT * FROM `flow` WHERE page = 'introduce'";
$introduce2 = mysql_query($query_introduce2, $test) or die(mysql_error());
$totalRows_introduce2 = mysql_num_rows($introduce2);
 
//......................(其餘頁面寫法同上)

 

?>

 

report.php

<?php
require('chinese-unicode.php');            //匯入剛剛下載的中文化的FPDF
require_once('../Connections/test.php');   //匯入連結資料庫的語法
require_once('../require/reportSQL.php');  //匯入報表的SQL語法

$pdf=new PDF_Unicode();                    //建立PDF_Unicode

$pdf->Open();                              //開啟
$pdf->AddPage();                           //新的一頁

$pdf->AddUniCNShwFont('uni');              //加入中文
$pdf->SetFont('uni','',16);                //設定字型與字體大小

//接著將資料放入一維陣列中
$row0=array('瀏覽頁面','今日人數流量(人)','總人數流量(人)');
$row1=array('首頁',$totalRows_index,$totalRows_index2);
$row2=array('品牌介紹',$totalRows_introduce,$totalRows_introduce2);
$row3=array('經營理念',$totalRows_manage,$totalRows_manage2);
$row4=array('代理品牌',$totalRows_agent,$totalRows_agent2);
$row5=array('產品',$totalRows_product,$totalRows_product2);
$row6=array('公告發佈',$totalRows_publish,$totalRows_publish2);
$row7=array('活動新訊',$totalRows_activities,$totalRows_activities2);
$row8=array('素材介紹',$totalRows_material,$totalRows_material2);
$row9=array('營業據點',$totalRows_place,$totalRows_place2);
$row10=array('聯絡我們',$totalRows_contact,$totalRows_contact2);

 

//今日人數流量統計
$today=$totalRows_index+$totalRows_introduce+$totalRows_manage+$totalRows_agent+$totalRows_product+$totalRows_publish+$totalRows_activities+$totalRows_material+$totalRows_place+$totalRows_contact;

$total=$totalRows_index2+$totalRows_introduce2+$totalRows_manage2+$totalRows_agent2+$totalRows_product2+$totalRows_publish2+$totalRows_activities2+$totalRows_material2+$totalRows_place2+$totalRows_contact2;

$row11=array('統計',$today,$total);

//再將資料放入2維陣列中
$trow=array($row0,$row1,$row2,$row3,$row4,$row5,$row6,$row7,$row8,$row9,$row10,$row11);

 

//設定表格的寬度
 $w=array(60,65,60);
 

//迴圈
 for($j=0;$j<count($trow);$j++){
  for($i=0;$i<count($trow[$j]);$i++){
   $pdf->Cell($w[$i],7,$trow[$j][$i],1,0,'C');     //將資料放入表格內
  } $pdf->Ln();                                    //下一列
 }

 $pdf->Output();                                   //輸出呈現
?>

接下來從瀏覽器觀看的結果如下圖:

 

真的是一個超方便的工具喔 ^ ^~~

 

 

參考或是複製語法時,別忘了留個言喔 ^ ^ ~