pdf生成类

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');
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值