class pdf {
# 常量设置
const PDF_LOGO = '\Logo\logo_big.png'; // LOGO路径 该路径是tcpdf下
const PDF_LOGO_WIDTH = '20'; // LOGO宽度
const PDF_TITLE = 'www.liuweime.me'; //
const PDF_HEAD = '上电脑课';
const PDF_FONT = 'stsongstdlight';
const PDF_FONT_STYLE = '';
const PDF_FONT_SIZE = 10;
const PDF_FONT_MONOSPACED = 'courier';
const PDF_IMAGE_SCALE='1.25';
# tcpdf对象存储
protected $pdf = null;
/**
* 构造函数 引入插件并实例化
*/
public function __construct() {
# 实例化该插件
$this->pdf = new TCPDF();
}
/**
* 设置文档信息
* @param $user string 文档作者
* @param $title string 文档标题
* @param $subject string 文档主题
* @param $keywords string 文档关键字
* @return null
*/
protected function setDocumentInfo($user = '', $title = '', $subject ='', $keywords = '') {
if(empty($user) || empty($title)) return false;
# 文档创建者名称
$this->pdf->SetCreator(APP_NAME);
# 作者
$this->pdf->SetAuthor($user);
# 文档标题
$this->pdf->SetTitle($title);
# 文档主题
if(!empty($subject)) $this->pdf->SetSubject($subject);
# 文档关键字
if(!empty($keywords)) $this->pdf->SetKeywords($keywords);
}
/**
* 设置文档的页眉页脚信息
* @param null
* @return null
*/
protected function setHeaderFooter() {
# 设置页眉信息
# 格式 logo地址 logo宽度 页眉标题 页眉说明文字 页眉字体颜色 页眉下划线颜色
$this->pdf->SetHeaderData(self::PDF_LOGO , self::PDF_LOGO_WIDTH , self::PDF_TITLE , self::PDF_HEAD , array(35 , 35 , 35) , array(221,221,221));
# 设置页脚信息
# 格式 页脚字体颜色 页脚下划线颜色
$this->pdf->setFooterData(array(35 , 35 , 35) , array(221,221,221));
# 设置页眉页脚字体
$this->pdf->setHeaderFont(array('stsongstdlight' , self::PDF_FONT_STYLE , self::PDF_FONT_SIZE));
$this->pdf->setFooterFont(array('helvetica' , self::PDF_FONT_STYLE , self::PDF_FONT_SIZE));
}
/**
* 关闭页眉页脚
* @param null
* @return null
*/
protected function closeHeaderFooter() {
# 关闭页头
$this->pdf->setPrintHeader(false);
# 关闭页脚
$this->pdf->setPrintFooter(false);
}
/**
* 设置间距 包括正文间距 页眉页脚间距
* @param null
* @return null
*/
protected function setMargin() {
# 设置默认的等宽字体
$this->pdf->SetDefaultMonospacedFont('courier');
# 正文左侧 上侧 右侧间距
$this->pdf->SetMargins(15, 7, 15);
# 页眉间距
$this->pdf->SetHeaderMargin(5);
# 页脚间距
$this->pdf->SetFooterMargin(10);
}
/**
* 正文设置 包括 分页 图片比例 正文字体
* @param null
* @return null
*/
protected function setMainBody() {
# 开启分页 true开启 false关闭 开启分页时参数2起作用 表示正文距底部的间距
$this->pdf->SetAutoPageBreak(true , 25);
# 设置图片比例
$this->pdf->setImageScale(self::PDF_IMAGE_SCALE);
#
$this->pdf->setFontSubsetting(true);
# 设置正文字体 stsongstdlight是Adobe Reader默认字体
> $this->pdf->SetFont('stsongstdlight', '', 14);
# 添加页面 该方法如果前面已有页面 会在将页脚添加到页面中 并自动添加下一页 否则添加新一页
$this->pdf->AddPage();
}
/**
* 生成pdf
* @param $info array
* array(
* 'user'=>'文档作者' ,
* 'title'=>'文档标题' ,
* 'subject'=>'文档主题' ,
* 'keywords'=>'文档关键字' ,
* 'content'=>'文档正文内容' ,
* 'HT'=>'是否开启页眉页脚' ,
* 'path'=>'文档保存路径');
* @return null
*/
public function createPDF($info = array()) {
if(empty($info) || !is_array($info)) return false;
$this->setDocumentInfo($info['user'] , $info['title'] , $info['subject'] , $info['keywords']);
if(!$info['HT']) {
$this->closeHeaderFooter();
} else {
$this->setHeaderFooter();
}
$this->setMargin();
$this->setMainBody();
# 写入内容
$this->pdf->writeHTML($info['content'], true, false, true, false, '');
# 输出 I输出到浏览器 F输出到指定路径
$this->pdf->Output($info['path'] , 'F');
}
}
pdf生成类
最新推荐文章于 2024-04-27 09:13:51 发布