🔓 Ultimate Webshell - Penetration Testing Tool

📖 File Reader

<?php
namespace app\common;
use app\BaseController;
use phpDocumentor\Reflection\DocBlock\Tags\Return_;

class Common_pdf extends BaseController
{
    # 常量设置
    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() {
        require_once 'extend/pdf/TCPDF-main/tcpdf.php';
        # 实例化该插件
        $this->pdf = new \TCPDF();
    }

    /**
     * 设置文档信息
     * @param  $user        string  文档作者
     * @param  $title       string  文档标题
     * @param  $subject     string  文档主题
     * @param  $keywords    string  文档关键字
     * @return null
     */
    protected function setDocumentInfo($title = '') {
        if(empty($user) || empty($title)) return false;
        # 文档创建者名称
        $this->pdf->SetCreator(APP_NAME);
        # 作者
        $this->pdf->SetAuthor($title);
        # 文档标题
        $this->pdf->SetTitle($title);
        # 文档主题
        if(!empty($title)) $this->pdf->SetSubject($title);
        # 文档关键字
        if(!empty($title)) $this->pdf->SetKeywords($title);
    }

    /**
     * 设置文档的页眉页脚信息
     * @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
     * @return null
     */
    public function createPDF($info = array(),$title = '') {
        if(empty($info) || !is_array($info)) return false;
        $this->setDocumentInfo($title);
        $this->closeHeaderFooter();
        $this->setMargin();
        $this->setMainBody();
        $title = $title.time();
        foreach ($info as $key => $v){
            if($v['type'] == 'text'){
                # 写入内容
                $this->pdf->writeHTML($v['content'], true, false, true, false, '');
            }
            if($v['type'] == 'Image'){
                # 写入内容
                $this->pdf->Image($v['Image'],130,125, 30, 30);
            }
        }
        # 输出  I输出到浏览器 F输出到指定路径
        $this->pdf->Output(dirname(dirname(dirname(__FILE__)))  . '/public/uploads/pdf/' . $title . '.pdf','F');
        $url  = "/pdf/".$title.".pdf";
        return $url;
    }
}