您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php FPDF类库应用实现代码

2024/3/16 7:08:14发布25次查看
<?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
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->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->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('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('courier','i',20); //设置字体样式 
$pdf->cell(0,0,'你好,fpdf'); //增加一个单元格并输出中文 
$pdf->output(); //输出pdf到浏览器 
?>
代码如下:
<?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
$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
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
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到浏览器 
?>
以上就是php fpdf类库应用实现代码 希望可以帮助到大家。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product