php中FPDF类库应用

http://blog.csdn.net/zhuzhao/archive/2009/03/18/4001086.aspx

-------------------------------------------------------------------------------------------

<?php  
require('chinese.php');   
class PDF extends PDF_Chinese   
{   
    function Header()                                   //设置页眉  
    {   
        $this->SetFont('GB','',10);   
        $this->Write(10,'XX公司产品名录');   
        $this->Ln(20);                                   //换行  
    }  
    function Footer()                                   //设置页脚   
    {  
        $this->SetY(-15);  
        $this->SetFont('GB','',10);   
        $this->Cell(0,10,'第'.$this->PageNo().'页');   
    }   
}   
 
$conn = mysql_connect("localhost", "root", "");                 //连接数据库  
 
mysql_select_db("product", $conn);                          //执行SQL  
$query_rs_prod = "SELECT * FROM product ORDER BY prod_id";  
$rs_prod = mysql_query($query_rs_prod, $conn) or die(mysql_error());  
$row_rs_prod = mysql_fetch_assoc($rs_prod);  
$totalRows_rs_prod = mysql_num_rows($rs_prod);  
 
$pdf=new PDF();                                     //创建新的FPDF对象  
$pdf->AddGBFont();                                   //设置中文字体  
$pdf->Open();                                            //开始创建PDF  
$pdf->AddPage();                                     //增加一页  
 
$pdf->SetFont('GB','',10);                                   //设置字体样式  
 
$header=array('产品编号','产品名称','产品类型','产品单价');     //设置表头  
$width=array(20,80,40,20);                              //设置每列宽度  
 
for($i=0;$i<count($header);$i++)                         //循环输出表头  
    $pdf->Cell($width[$i],6,$header[$i],1);  
$pdf->Ln();  
 
do                                                  //循环输出表体  
{  
    $pdf->Cell($width[0],6,$row_rs_prod['prod_id'],1);  
    $pdf->Cell($width[1],6,$row_rs_prod['prod_name'],1);  
    $pdf->Cell($width[2],6,$row_rs_prod['prod_type'],1);  
    $pdf->Cell($width[3],6,$row_rs_prod['prod_price'],1);  
    $pdf->Ln();  
} while ($row_rs_prod = mysql_fetch_assoc($rs_prod));  
 
$pdf->Output("product.pdf", true);                           //下载PDF文件  
?> 
<?php
require('chinese.php');
class PDF extends PDF_Chinese
{
 function Header()          //设置页眉
 {
  $this->SetFont('GB','',10);
  $this->Write(10,'XX公司产品名录');
  $this->Ln(20);         //换行
 }
 function Footer()         //设置页脚
 {
  $this->SetY(-15);
  $this->SetFont('GB','',10);
  $this->Cell(0,10,'第'.$this->PageNo().'页');
 }
}

$conn = mysql_connect("localhost", "root", "");     //连接数据库

mysql_select_db("product", $conn);       //执行SQL
$query_rs_prod = "SELECT * FROM product ORDER BY prod_id";
$rs_prod = mysql_query($query_rs_prod, $conn) or die(mysql_error());
$row_rs_prod = mysql_fetch_assoc($rs_prod);
$totalRows_rs_prod = mysql_num_rows($rs_prod);

$pdf=new PDF();          //创建新的FPDF对象
$pdf->AddGBFont();          //设置中文字体
$pdf->Open();           //开始创建PDF
$pdf->AddPage();          //增加一页

$pdf->SetFont('GB','',10);         //设置字体样式

$header=array('产品编号','产品名称','产品类型','产品单价');  //设置表头
$width=array(20,80,40,20);        //设置每列宽度

for($i=0;$i<count($header);$i++)       //循环输出表头
    $pdf->Cell($width[$i],6,$header[$i],1);
$pdf->Ln();

do             //循环输出表体
{
    $pdf->Cell($width[0],6,$row_rs_prod['prod_id'],1);
    $pdf->Cell($width[1],6,$row_rs_prod['prod_name'],1);
    $pdf->Cell($width[2],6,$row_rs_prod['prod_type'],1);
    $pdf->Cell($width[3],6,$row_rs_prod['prod_price'],1);
    $pdf->Ln();
} while ($row_rs_prod = mysql_fetch_assoc($rs_prod));

$pdf->Output("product.pdf", true);       //下载PDF文件
?>
  view plaincopy to clipboardprint?
<?php  
define('FPDF_FONTPATH','font/');                //定义font文件夹所在路径  
require_once('fpdf/fpdf.php');                  //包含fpdf类库文件  
$pdf=new FPDF('P', 'mm', 'A4');             //创建新的FPDF对象,竖向放纸,单位为毫米,纸张大小A4  
$pdf->Open();                                //开始创建PDF  
$pdf->AddPage();                         //增加一页  
$pdf->SetFont('Courier','I',20);                 //设置字体样式  
$pdf->Cell(0,0,'Hello World!');                  //增加一个单元格  
$pdf->Output();                              //输出PDF到浏览器  
?> 
<?php
define('FPDF_FONTPATH','font/');    //定义font文件夹所在路径
require_once('fpdf/fpdf.php');     //包含fpdf类库文件
$pdf=new FPDF('P', 'mm', 'A4');    //创建新的FPDF对象,竖向放纸,单位为毫米,纸张大小A4
$pdf->Open();        //开始创建PDF
$pdf->AddPage();       //增加一页
$pdf->SetFont('Courier','I',20);     //设置字体样式
$pdf->Cell(0,0,'Hello World!');     //增加一个单元格
$pdf->Output();        //输出PDF到浏览器
?>
 

view plaincopy to clipboardprint?
<?php  
define('FPDF_FONTPATH','font/');                //定义font文件夹所在路径  
require_once('fpdf/fpdf.php');                  //包含fpdf类库文件  
$pdf=new FPDF('P', 'mm', 'A4');             //创建新的FPDF对象,竖向放纸,单位为毫米,纸张大小A4  
$pdf->Open();                                //开始创建PDF  
$pdf->AddPage();                         //增加一页  
$pdf->SetFont('Courier','I',20);                 //设置字体样式  
$pdf->Image('sight.jpg',20,20,0,0);              //增加一张图片,文件名为sight.jpg  
$pdf->Output();                              //输出PDF到浏览器  
?> 
<?php
define('FPDF_FONTPATH','font/');    //定义font文件夹所在路径
require_once('fpdf/fpdf.php');     //包含fpdf类库文件
$pdf=new FPDF('P', 'mm', 'A4');    //创建新的FPDF对象,竖向放纸,单位为毫米,纸张大小A4
$pdf->Open();        //开始创建PDF
$pdf->AddPage();       //增加一页
$pdf->SetFont('Courier','I',20);     //设置字体样式
$pdf->Image('sight.jpg',20,20,0,0);    //增加一张图片,文件名为sight.jpg
$pdf->Output();        //输出PDF到浏览器
?>
 

view plaincopy to clipboardprint?
<?php  
define('FPDF_FONTPATH','font/');                //定义font文件夹所在路径  
require_once('fpdf/fpdf.php');                  //包含fpdf类库文件  
$pdf=new FPDF(‘P’, ‘mm’, ‘A4’);             //创建新的FPDF对象,竖向放纸,单位为毫米,纸张大小A4  
$pdf->Open();                                //开始创建PDF  
$pdf->AddPage();                         //增加一页  
$pdf->SetFont('Courier','I',20);                 //设置字体样式  
$pdf->Cell(60,10,'Hello World!',1);              //增加一个单元格 边框为1  
$pdf->Output();                              //输出PDF到浏览器  
?> 
<?php
define('FPDF_FONTPATH','font/');    //定义font文件夹所在路径
require_once('fpdf/fpdf.php');     //包含fpdf类库文件
$pdf=new FPDF(‘P’, ‘mm’, ‘A4’);    //创建新的FPDF对象,竖向放纸,单位为毫米,纸张大小A4
$pdf->Open();        //开始创建PDF
$pdf->AddPage();       //增加一页
$pdf->SetFont('Courier','I',20);     //设置字体样式
$pdf->Cell(60,10,'Hello World!',1);    //增加一个单元格 边框为1
$pdf->Output();        //输出PDF到浏览器
?>
 

view plaincopy to clipboardprint?
<?php  
define('FPDF_FONTPATH','font/');                //定义font文件夹所在路径  
require_once('fpdf/fpdf.php');                  //包含fpdf类库文件  
$pdf=new FPDF('P', 'mm', 'A4');             //创建新的FPDF对象,竖向放纸,单位为毫米,纸张大小A4  
$pdf->Open();                                //开始创建PDF  
$pdf->AddPage();                         //增加一页  
 
$pdf->SetFont('Arial','',14);                    //设置字体样式  
 
$header=array('Name','Age','Sex','Salary');     //设置表头  
$data=array();                              //设置表体  
$data[0] = array('Simon','24','Male','5,000.00');  
$data[1] = array('Elaine','25','Female','6,000.00');  
$data[2] = array('Susan','25','Female','7,000.00');  
$data[3] = array('David','26','Male','8,000.00');  
 
$width=array(40,40,40,40);                  //设置每列宽度  
 
for($i=0;$i<count($header);$i++)             //循环输出表头  
    $pdf->Cell($width[$i],6,$header[$i],1);  
$pdf->Ln();  
 
foreach($data as $row)                      //循环输出表体  
{  
    $pdf->Cell($width[0],6,$row[0],1);  
    $pdf->Cell($width[1],6,$row[1],1);  
    $pdf->Cell($width[2],6,$row[2],1);  
    $pdf->Cell($width[3],6,$row[3],1);  
    $pdf->Ln();  
}  
 
$pdf->Output();                              //输出PDF到浏览器  
?> 
<?php
define('FPDF_FONTPATH','font/');    //定义font文件夹所在路径
require_once('fpdf/fpdf.php');     //包含fpdf类库文件
$pdf=new FPDF('P', 'mm', 'A4');    //创建新的FPDF对象,竖向放纸,单位为毫米,纸张大小A4
$pdf->Open();        //开始创建PDF
$pdf->AddPage();       //增加一页

$pdf->SetFont('Arial','',14);     //设置字体样式

$header=array('Name','Age','Sex','Salary');  //设置表头
$data=array();        //设置表体
$data[0] = array('Simon','24','Male','5,000.00');
$data[1] = array('Elaine','25','Female','6,000.00');
$data[2] = array('Susan','25','Female','7,000.00');
$data[3] = array('David','26','Male','8,000.00');

$width=array(40,40,40,40);     //设置每列宽度

for($i=0;$i<count($header);$i++)    //循环输出表头
    $pdf->Cell($width[$i],6,$header[$i],1);
$pdf->Ln();

foreach($data as $row)      //循环输出表体
{
    $pdf->Cell($width[0],6,$row[0],1);
    $pdf->Cell($width[1],6,$row[1],1);
    $pdf->Cell($width[2],6,$row[2],1);
    $pdf->Cell($width[3],6,$row[3],1);
    $pdf->Ln();
}

$pdf->Output();        //输出PDF到浏览器
?>
 

view plaincopy to clipboardprint?
<?php  
define('FPDF_FONTPATH','font/');                //定义font文件夹所在路径  
require_once('fpdf/fpdf.php');                  //包含fpdf类库文件  
$pdf=new FPDF('P', 'mm', 'A4');             //创建新的FPDF对象,竖向放纸,单位为毫米,纸张大小A4  
$pdf->Open();                                //开始创建PDF  
$pdf->AddPage();                         //增加一页  
$pdf->SetFont('Courier','I',20);                 //设置字体样式  
$pdf->Cell(0,0,'你好,FPDF');                   //增加一个单元格并输出中文  
$pdf->Output();                              //输出PDF到浏览器  
?> 
<?php
define('FPDF_FONTPATH','font/');    //定义font文件夹所在路径
require_once('fpdf/fpdf.php');     //包含fpdf类库文件
$pdf=new FPDF('P', 'mm', 'A4');    //创建新的FPDF对象,竖向放纸,单位为毫米,纸张大小A4
$pdf->Open();        //开始创建PDF
$pdf->AddPage();       //增加一页
$pdf->SetFont('Courier','I',20);     //设置字体样式
$pdf->Cell(0,0,'你好,FPDF');     //增加一个单元格并输出中文
$pdf->Output();        //输出PDF到浏览器
?>
 

view plaincopy to clipboardprint?
<?php   
require('chinese.php');   
class PDF extends PDF_Chinese   
{   
function Header()                       //设定页眉  
{   
$this->SetFont('GB','',10);   
$this->Write(10,'FPDF中文测试');   
$this->Ln(20);   
}   
 
function Footer()                       //设定页脚  
{  
$this->SetY(-15);  
$this->SetFont('GB','',10);   
$this->Cell(0,10,'第'.$this->PageNo().'页');   
}   
}   
 
$pdf=new PDF();                         //创建PDF文档  
$pdf->AddGBFont();   
$pdf->Open();   
$pdf->AliasNbPages();   
$pdf->AddPage();   
$pdf->SetFont('GB','I',20);   
$pdf->Cell(0,10,'你好,FPDF');          //输出一段中文  
$pdf->Output();   
?> 
<?php
require('chinese.php');
class PDF extends PDF_Chinese
{
function Header()       //设定页眉
{
$this->SetFont('GB','',10);
$this->Write(10,'FPDF中文测试');
$this->Ln(20);
}

function Footer()       //设定页脚
{
$this->SetY(-15);
$this->SetFont('GB','',10);
$this->Cell(0,10,'第'.$this->PageNo().'页');
}
}

$pdf=new PDF();       //创建PDF文档
$pdf->AddGBFont();
$pdf->Open();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('GB','I',20);
$pdf->Cell(0,10,'你好,FPDF');    //输出一段中文
$pdf->Output();
?>
 

view plaincopy to clipboardprint?
<?php  
$conn = mysql_connect("localhost", "root", "");                 //连接数据库  
$colname_rs_article = $_GET['id'];                          //获取参数id  
 
mysql_select_db("cms", $conn);                          //执行SQL  
$query_rs_article = sprintf("SELECT * FROM articles WHERE article_id = %s", $colname_rs_article);  
$rs_article = mysql_query($query_rs_article, $conn) or die(mysql_error());  
$row_rs_article = mysql_fetch_assoc($rs_article);  
$totalRows_rs_article = mysql_num_rows($rs_article);  
 
function conv($Text)                                    //对返回文本进行处理  
{  
$Text=htmlspecialchars($Text);                              //转换HTML关键字符  
$Text=nl2br($Text);                                         //转换换行符  
return $Text;  
}  
?>  
<p align="center"><B><?php echo $row_rs_article['title']; ?></B></p>  
<p align="center"><font size=2><?php echo $row_rs_article['author']; ?> | <a href="showpdf.php?id=<?php echo $row_rs_article['article_id']; ?>">下载PDF文档</a></font></p>  
<HR>  
<p><?php echo conv($row_rs_article['content']); ?></p> 
<?php
$conn = mysql_connect("localhost", "root", "");     //连接数据库
$colname_rs_article = $_GET['id'];       //获取参数id

mysql_select_db("cms", $conn);       //执行SQL
$query_rs_article = sprintf("SELECT * FROM articles WHERE article_id = %s", $colname_rs_article);
$rs_article = mysql_query($query_rs_article, $conn) or die(mysql_error());
$row_rs_article = mysql_fetch_assoc($rs_article);
$totalRows_rs_article = mysql_num_rows($rs_article);

function conv($Text)          //对返回文本进行处理
{
$Text=htmlspecialchars($Text);        //转换HTML关键字符
$Text=nl2br($Text);           //转换换行符
return $Text;
}
?>
<p align="center"><B><?php echo $row_rs_article['title']; ?></B></p>
<p align="center"><font size=2><?php echo $row_rs_article['author']; ?> | <a href="showpdf.php?id=<?php echo $row_rs_article['article_id']; ?>">下载PDF文档</a></font></p>
<HR>
<p><?php echo conv($row_rs_article['content']); ?></p>
 

view plaincopy to clipboardprint?
<?php   
require('chinese.php');   
class PDF extends PDF_Chinese   
{   
    function Header()                                   //设置页眉  
    {   
        $this->SetFont('GB','',10);   
        $this->Write(10,'文章系统 - XX网站');   
        $this->Ln(20);                                   //换行  
    }  
    function Footer()                                   //设置页脚   
    {  
        $this->SetY(-15);  
        $this->SetFont('GB','',10);   
        $this->Cell(0,10,'第'.$this->PageNo().'页');   
    }   
}   
//主程序开始  
$conn = mysql_connect("localhost", "root", "");                 //连接数据库  
$colname_rs_article = $_GET['id'];                          //获取参数id  
 
mysql_select_db("cms", $conn);                          //执行SQL  
$query_rs_article = sprintf("SELECT * FROM articles WHERE article_id = %s", $colname_rs_article);  
$rs_article = mysql_query($query_rs_article, $conn) or die(mysql_error());  
$row_rs_article = mysql_fetch_assoc($rs_article);  
$totalRows_rs_article = mysql_num_rows($rs_article);  
//开始创建PDF文档  
$pdf=new PDF();   
$pdf->AddGBFont();   
$pdf->Open();   
$pdf->AliasNbPages();   
$pdf->AddPage();   
$pdf->SetFont('GB','B',20);   
$pdf->Cell(0,10,$row_rs_article['title']);                       //输出文章标题  
$pdf->Ln();                                          //换行  
$pdf->SetFont('GB','',10);   
$pdf->Cell(0,10,$row_rs_article['author']);                  //输出文章作者  
$pdf->Ln();  
$pdf->SetFont('GB','',12);  
$content = $row_rs_article['content'];  
while($content != "")                                       //循环逐页将文章内容写入PDF  
{  
    $length = strlen($content);                             //获取文章长度  
    $output = substr($content, 0, 1024);                        //获取本页输出内容,每1024个字符为1页  
    $pdf->Cell(0,10,$output);                            //输出文章内容  
    $content = substr($content, 1024, $length);             //获取剩余未输出内容  
    $pdf->AddPage();                                 //换页  
}  
$pdf->Output($row_rs_article['title'].".pdf", true);                 //输出PDF文件,文件名为文章标题  
?> 
<?php
require('chinese.php');
class PDF extends PDF_Chinese
{
 function Header()          //设置页眉
 {
  $this->SetFont('GB','',10);
  $this->Write(10,'文章系统 - XX网站');
  $this->Ln(20);         //换行
 }
 function Footer()         //设置页脚
 {
  $this->SetY(-15);
  $this->SetFont('GB','',10);
  $this->Cell(0,10,'第'.$this->PageNo().'页');
 }
}
//主程序开始
$conn = mysql_connect("localhost", "root", "");     //连接数据库
$colname_rs_article = $_GET['id'];       //获取参数id

mysql_select_db("cms", $conn);       //执行SQL
$query_rs_article = sprintf("SELECT * FROM articles WHERE article_id = %s", $colname_rs_article);
$rs_article = mysql_query($query_rs_article, $conn) or die(mysql_error());
$row_rs_article = mysql_fetch_assoc($rs_article);
$totalRows_rs_article = mysql_num_rows($rs_article);
//开始创建PDF文档
$pdf=new PDF();
$pdf->AddGBFont();
$pdf->Open();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('GB','B',20);
$pdf->Cell(0,10,$row_rs_article['title']);        //输出文章标题
$pdf->Ln();           //换行
$pdf->SetFont('GB','',10);
$pdf->Cell(0,10,$row_rs_article['author']);      //输出文章作者
$pdf->Ln();
$pdf->SetFont('GB','',12);
$content = $row_rs_article['content'];
while($content != "")          //循环逐页将文章内容写入PDF
{
 $length = strlen($content);        //获取文章长度
 $output = substr($content, 0, 1024);      //获取本页输出内容,每1024个字符为1页
 $pdf->Cell(0,10,$output);         //输出文章内容
 $content = substr($content, 1024, $length);    //获取剩余未输出内容
 $pdf->AddPage();         //换页
}
$pdf->Output($row_rs_article['title'].".pdf", true);     //输出PDF文件,文件名为文章标题
?>
 

view plaincopy to clipboardprint?
<?php  
define('FPDF_FONTPATH','font/');                    //定义font文件夹所在路径  
require_once('fpdf/fpdf.php');                      //包含fpdf类库文件  
 
class PDF extends FPDF  
{  
    function Header()                           //设置页眉  
    {  
        $this->SetFont('Arial','B',15);              //设置页眉字体  
        $this->Cell(80);                         //移动单元格  
        $this->Cell(30,10,'Title');                  //写入页眉文字  
        $this->Ln(20);                           //换行  
    }  
      
    function Footer()                           //设置页脚  
    {  
        $this->SetY(-15);                        //设置页脚所在位置  
        $this->SetFont('Arial','I',8);                   //设置页脚字体  
        $this->Cell(0,10,'Page - '.$this->PageNo());  //输出当前页码作为页脚内容  
    }  
}  
 
$pdf=new PDF('P', 'mm', 'A4');              //创建新的FPDF对象,竖向放纸,单位为毫米,纸张大小A4  
$pdf->Open();                            //开始创建PDF  
$pdf->AddPage();                     //增加一页  
$pdf->SetFont('Courier','I',20);             //设置字体样式  
$pdf->Cell(0,0,'Hello World!');              //增加一个单元格  
$pdf->Output();                          //输出PDF到浏览器  
?> 

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhuzhao/archive/2009/03/18/4001086.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值