|
|
|
PHP PDF - MySQL Export to PDF ภาษาไทย มันเป็นภาษาต่างดาวครับ ดูกระทู้เก่าแล้วยังแก้ไม่ได้ครับ |
|
|
|
|
|
|
|
Code (PHP)
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
</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
$w=array(30,30,55,25,20,20);
//Header
for($i=0;$i<count($header);$i++)
//$this->Cell($w[$i],7,iconv('UTF-8', 'TIS-620', $header[$i]),1,0,'C');
$this->Cell($w[$i],7,iconv('UTF-8', 'TIS-620', $header[$i]),1,0,'C');
$this->Ln();
//Data
foreach ($data as $eachrs)
{
$this->Cell(30,6,$eachrs["staff"],1);
$this->Cell(30,6,$eachrs["Name"],1);
$this->Cell(55,6,$eachrs["Email"],1);
$this->Cell(25,6,$eachrs["CountryCode"],1,0,'C');
$this->Cell(20,6,$eachrs["Budget"],1);
$this->Cell(20,6,$eachrs["xxxx"],1);
$this->Ln();
}
}
*/
//Better table
function ImprovedTable($header,$data)
{
//Column widths
$w=array(20,30,55,25,25,25);
//Header
for($i=0;$i<count($header);$i++)
$this->Cell($w[$i],7,iconv('UTF-8', 'TIS-620', $header[$i]),1,0,'C');
$this->Ln();
//Data
foreach ($data as $eachrs)
{
$this->Cell(20,6,$eachrs["staff"],1);
$this->Cell(30,6,$eachrs["Name"],1);
$this->Cell(55,6,$eachrs["Email"],1);
$this->Cell(25,6,$eachrs["CountryCode"],1,0,'C');
$this->Cell(25,6,number_format($eachrs["Budget"],2),1,0,'R');
$this->Cell(25,6,number_format($eachrs["Budget"],2),1,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(20,30,55,25,25,25);
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,$row[2],'LR',0,'L',$fill);
$this->Cell($w[3],6,$row[3],'LR',0,'C',$fill);
$this->Cell($w[4],6,number_format($row[4]),'LR',0,'R',$fill);
$this->Cell($w[5],6,number_format($row[5]),'LR',0,'R',$fill);
$this->Ln();
$fill=!$fill;
}
$this->Cell(array_sum($w),0,'','T');
}
}
$pdf=new PDF();
//Column titles
$header=array('ทดสอบ','bbb','ccc','ddd','eee','fff');
//Data loading
//*** Load MySQL Data ***//
//include "connect_pdf.php";
$c = mysql_connect("localhost","root","12345") or die("Error Connect to Database");
$dbname = mysql_select_db("mydatabase");
mysql_query("SET character_set_results=utf8");
mysql_query("SET character_set_client=utf8");
mysql_query("SET character_set_connection=utf8");
$sql = "SELECT * FROM customer";
$cn = mysql_query($sql);
$rsData = array();
for ($i=0;$i < mysql_num_rows($cn);$i++) {
$rs = mysql_fetch_array($cn);
array_push($rsData,$rs);
}
//************************//
$pdf->SetFont('Arial','',10);
//*** Table 1 ***//
/*
$pdf->AddPage();
$pdf->Image('images/logo.gif',80,8,33);
$pdf->Ln(35);
$pdf->BasicTable($header,$rsData);
*/
//*** Table 2 ***//
$pdf->AddPage();
$pdf->Image('images/logo.gif',80,8,33);
$pdf->Ln(35);
$pdf->ImprovedTable($header,$rsData);
//*** Table 3 ***//
$pdf->AddPage();
$pdf->Image('images/logo.gif',80,8,33);
$pdf->Ln(35);
$pdf->FancyTable($header,$rsData);
$pdf->Output("MyPDF/MyPDF.pdf","F");
?>
PDF Created Click <a href="MyPDF/MyPDF.pdf">here</a> to Download
</body>
</html>
Tag : PHP, MySQL, HTML/CSS
|
|
|
|
|
|
Date :
2011-04-26 10:24:12 |
By :
benze240 |
View :
2604 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เคยใช้แต่ PHPExcel อะครับ มันต้องมีการ setFont ที่รองรับภาษาไทยด้วยนะ
มีโค้ดตัวอย่างประมาณนี้อะ
Code (PHP)
require("lib/PHPExcel.php");
#Create Object
$objPHPExcel = new PHPExcel();
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet->getDefaultStyle()->getFont()->setName('Tahoma');
$objWorksheet->getDefaultStyle()->getFont()->setSize(12);
$objWorksheet->getColumnDimension('A')->setAutoSize(true);
$objWorksheet->setCellValue('A1','นี่คือภาษาไทยนะคร้าบ');
$objWorksheet->setCellValue('B5','B5');
$objWorksheet->setCellValue('F5','F5');
$objWorksheet->setCellValue('G15','G15');
$objWorksheet->setCellValue('K30','K30');
$objWorksheet->setCellValue('A4', 'Miscellaneous glyphs');
$objWorksheet->setShowGridlines(false);
$objWriter = new PHPExcel_Writer_PDF($objPHPExcel);
$file_path = "PDFfeaturedemo.pdf";
$objWriter->setFont('cordiaupc');
$objWriter->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_CUSTOM);
$objWriter->save($file_path);
|
|
|
|
|
Date :
2011-04-26 10:31:23 |
By :
naskw |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ใช้ function iconv แก้แล้วยังไม่หายเลยครับ
$this->Cell($w[$i],7,iconv('UTF-8', 'TIS-620', $header[$i]),1,0,'C');
|
|
|
|
|
Date :
2011-04-26 11:50:03 |
By :
benze240 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เหมือนผมจะคุ้นว่า PHPExcel นั้นใช้ FPDF ในการสร้างไฟล์ PDF ถ้าจำไม่ผิดนะครับ
ถ้าเป็นนแบบนั้นมันต้องมีการ set อะไรอีกนิดหน่อยครับ คือ ต้องไปดาวน์โหลดฟอนไทยมาด้วย
และตอนไรท์ก็
$objWriter->setFont('cordiaupc');
|
|
|
|
|
Date :
2011-04-26 12:01:35 |
By :
naskw |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|