侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

php FPDF类库应用实现代码

php  /  管理员 发布于 4年前   562

复制代码 代码如下:


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$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文件
?>


复制代码 代码如下:

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到浏览器
?>

复制代码 代码如下:

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到浏览器
?>

复制代码 代码如下:

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到浏览器
?>

复制代码 代码如下:

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$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到浏览器
?>


复制代码 代码如下:

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到浏览器
?>

复制代码 代码如下:

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();
?>


复制代码 代码如下:

$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;
}
?>


| 下载PDF文档






复制代码 代码如下:

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文件,文件名为文章标题
?>


复制代码 代码如下:

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实现生成PDF文件的方法示例【基于FPDF类库】
  • PHP中使用Imagick读取pdf并生成png缩略图实例
  • 使用PHP把HTML生成PDF文件的几个开源项目介绍
  • PHP实现HTML生成PDF文件的方法
  • PHP在网页中动态生成PDF文件详细教程
  • PHP中使用TCPDF生成PDF文档实例
  • PHP中使用imagick实现把PDF转成图片
  • PHP实现在线阅读PDF文件的方法
  • PHP使用MPDF类生成PDF的方法
  • PHP下SSL加密解密、验证、签名方法(很简单)
  • php 生成签名及验证签名详解
  • php实现往pdf中加数字签名操作示例【附源码下载】


  • 上一条:
    PHP CURL模拟GET及POST函数代码
    下一条:
    php是什么格式的文件?
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客