fpdf类库应用实现代码

1
1 入门例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
header("Content-Type:text/html;charset=gb2312");    //设置页码编码格式
$conn=@mysql_connect("localhost","root","") or die('连接失败:' . mysql_error());    //连接服务器
mysql_select_db("db_pdf",$conn) or die ('数据库选择失败:' . mysql_error());            //选择数据库
mysql_query("set names utf8");                      //设置页面编码格式
include('pdf/chinese.php');                         //载入中文组件
 
class PDF extends PDF_Chinese{                      //继承中文类
    function Header(){                              //定义方法设置标题
        $this->SetFont('gb','',10);                  //设置字体
        $this->Write(10,'文章');                   //输出标题内容
        $this->Ln(20);                               //换行
    }
    function Footer(){                              //设置页脚
        $this->SetY(-15);
        $this->SetFont('gb','',10);
        $this->Cell(0,10,'第'.$this->PageNo().'页');
    }
}
$colname_rs_id=$_GET['id'];                         //获取指定数据的ID
$query_rs_article=sprintf("select * from tb_articles where id=%s",$colname_rs_id);  //定义SQL语句
$rs_article=mysql_query($query_rs_article,$conn) or die(mysql_error());             //执行SQL查询语句
$row_rs_article=mysql_fetch_assoc($rs_article);     //获取查询结果
 
$pdf=new PDF();                                     //实例化PDF类
$pdf->AddGBFont();                                   //设置字体
$pdf->Open();                                        //打开文件
$pdf->AliasNbPages();                                //为每页定义一个别名
$pdf->AddPage();                                 //分页方法
 
$pdf->SetFont('gb','B',20);                          //设置字体
$pdf->Cell(0,10,iconv("utf-8","gb2312",$row_rs_article['topic']));       //输出一个内容
$pdf->Ln();                                          //换行
 
$pdf->SetFont('gb','',10);
$pdf->Cell(0,10,iconv("utf-8","gb2312",$row_rs_article['author']));
$pdf->Ln();
 
$pdf->SetFont('gb','',10);
$content= iconv('utf-8','gb2312',$row_rs_article['content']);
$pdf->MultiCell(0,5,$content);                       //输出内容,并且则行终止
$pdf->Ln();
 
$pdf->AddPage();                                 //分页
$pdf->Output(iconv("utf-8","gb2312",$row_rs_article['topic']).'.pdf',true);  //生成PDF文件
?>

 这个办法在浏览器直接显示一个PDF

2 中文处理
采用那个chinese包来处理中文

1
2
3
4
5
6
7
8
require(‘chinese/chinese.php‘);
$pdf=new PDF_Chinese();
$pdf->AddGBFont(‘simhei‘,‘黑体‘);
$pdf->Open();
$pdf->AddPage();
$pdf->SetFont(‘simsun‘,‘‘,20);
$pdf->Cell(40,10,‘Hello World!我们是中文哦‘);
$pdf->Output();

 
3 图片处理

1
$pdf->Image(‘sight.jpg‘,20,20,0,0);

各种说法说这个可以生成图片。经过处理,发现加了图片的PDF是文件变大了。不过图片看不见。不知道什么原理
翻开源码,image函数里有set_magic_quotes_runtime 不知道是不是这个玩意儿造成的。
翻遍各种资料,说TCPDF比较流行。下一步研究一下TCPDF.
目前结论FPDF处理不带图片的还行。

4 中文函数参考
AcceptPageBreak - 接受或不接受自動分頁
AddFont - 加入一個新的字型
AddLink - 建立一個內部連結
AddPage - 加入新的一頁
AliasNbPages - 為每一頁定義一個別名
Cell - 顯示一個儲存格
Close - 使 FDP 文件終止
Error - 嚴重錯誤
Footer - 頁尾
FPDF - PDF 核心建設程式
GetStringWidth - 計算字串的寬度
GetX - 取得現時(X)橫座標的位置
GetY - 取得現時縱座標(Y)的位置
Header - 頁首
Image - 輸出一個圖象
Line - 劃出一條線
Link - 放入一個連結
Ln - 行中斷,換行
MultiCell - 顯示出文字並且則行行中止
Open - 建立一個文件
Output - 儲存或送出文件
PageNo - 取得目前的頁數
Rect - 產生一個矩形
SetAuthor - 設定文件的著作者
SetAutoPageBreak - 設定為自動分頁模式
SetCompression - 啟動(on)或關閉(off)壓縮。
SetCreator - 設定文件的創作者名稱
SetDisplayMode - 設定顯示模式
SetDrawColor - 設定繪畫顏色
SetFillColor - 設定繪畫顏色
SetFont - 設定字型
SetFontSize - 設定字型大小
SetKeywords - 在文件中,把關鍵字聯在一起。
SetLeftMargin - 設定左邊邊界
SetLineWidth - 設定線的寬度
SetLink - 設定內部連結的目的地
SetMargins - 設定頁邊的空白距離
SetRightMargin - 設定右邊的空白距離
SetSubject - 設定文件的主題
SetTextColor - 設定文字的顏色
SetTitle - 設定文件標題
SetTopMargin - 設定頂部空白距離
SetX - 設定目前 x 的位置
SetXY - 設定目前 x 和 y 的位置
SetY - 設定目前 y 的位置
Text - 顯示一個字串
Write - 顯示流暢的文字

thinkphp整合系列之tcpdf类生成pdf文件https://baijunyao.com/article/73

部分出处www.cnblogs.com/yuexiaosheng/archive/2012/08/30/2664313.html
在线参考 http://twpug.net/docs/fpdf152/
首发于http://java-er.com - http://java-er.com/blog/fpdf/

posted @   TangYJun  阅读(345)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示