Chinaunix首页 | 论坛 | 博客
  • 博客访问: 33416
  • 博文数量: 15
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 142
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-05 10:37
文章分类

全部博文(15)

文章存档

2011年(1)

2009年(14)

我的朋友

分类:

2009-05-08 15:33:15

php 绘制 pdf表格
2009/01/07 15:13
介绍:
php也有跟java一样的生成pdf文件的库,java里边是itext,php有一个tcpdf,意大利一家公司开发的产品,使用免费,服务收费。tcpdf的主页是:
从官网down下来的包里边,包含的API是几个PHP文件,还有一群SAMPLE。
简例:
从sample05稍作修改,便能将表格的数据打印到PDF文档,全部代码是:
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('ytao');
$pdf->SetTitle('Suggestion');
$pdf->SetSubject('suggestion');
$pdf->SetKeywords('suggestion');
// set default header data
//$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
//set some language-dependent strings
$pdf->setLanguageArray($l);
// ---------------------------------------------------------
// set font
$pdf->SetFont('times', '', 10);
// add a page
$pdf->AddPage();
// set color for filler
$pdf->SetFillColor(255, 255, 0);

// Multicell test
// table header
// 仔细看MultiCell的几个参数,第6个是颜色开关,第7个是换行开关,1为打开,0为关闭
$pdf->MultiCell(40, 5, "NAME", 1, 'L', 1, 0, 0 ,0, true);
$pdf->MultiCell(40, 5, "EMAIL", 1, 'L', 1, 0, 0 ,0, true);
$pdf->MultiCell(40, 5, "SUGGESTION", 1, 'L', 1, 1, 0 ,0, true);

// table body ( data gotten from database)
$db = new mysqli('localhost', 'root', 'root', 'php');
$sql = 'select name, email, comment from comment';
$result = $db->query($sql);
$num_result = $result->num_rows;
for ($i = 0; $i < $num_result; $i++)
{
$row = $result->fetch_assoc();
$pdf->MultiCell(40, 5, $row['name'], 1, 'L', 0, 0, 0 ,0, true);
$pdf->MultiCell(40, 5, $row['email'], 1, 'L', 0, 0, 0 ,0, true);
$pdf->MultiCell(40, 5, $row['comment'], 1, 'L', 0, 1, 0 ,0, true);
}
$result->free();
$db->close();
// reset pointer to the last page
$pdf->lastPage();
// ---------------------------------------------------------
//Close and output PDF document
$pdf->Output('suggestion.pdf', 'I');
//============================================================+
// END OF FILE                                                
//============================================================+
?>
阅读(4221) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~