php 结合 PCDF 生成 pdf 文件 及 二维码教程

时间:2019-01-22
本文章向大家介绍php 结合 PCDF 生成 pdf 文件 及 二维码教程,主要包括php 结合 PCDF 生成 pdf 文件 及 二维码教程使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1、PCDF官网 https://tcpdf.org/  下载 查看 例子。下载 QRcode

2、php 代码。

2.1、生成pdf的php代码

   /**
     * @param $html
     * @param $author
     * @param $title
     * @param $sub
     * @return TCPDF
     * 生成pdf文件
     */
     /*新建一个pdf文件:

       Orientation:orientation属性用来设置文档打印格式是“Portrait”还是“Landscape”。 
      Landscape为横式打印,Portrait为纵向打印

       Unit:设置页面的单位。pt:点为单位,mm:毫米为单位,cm:厘米为单位,in:英尺为单位

       Format:设置打印格式,一般设置为A4

       Unicode:为true,输入的文本为Unicode字符文本

       Encoding:设置编码格式,默认为utf-8

       Diskcache:为true,通过使用文件系统的临时缓存数据减少RAM的内存使用。 */

    public function tcpdf($html,$author,$title,$sub,$name){
       //加载 类库文件
        require_once APP_ROOT."/extensions/phppdf/tcpdf_include.php";
        require_once(APP_ROOT.'extensions/phppdf/lang/chi.php');

        $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
 //设置文件信息
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor($author);
        $pdf->SetTitle($title);
        $pdf->SetSubject($sub);
        $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// set default header data
        $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, '', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));

        $pdf->setFooterData(array(0,64,0), array(0,64,128));
// set header and footer fonts
        $pdf->setHeaderFont(Array('cid0cs', '', 12));
        $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

        //设置默认等宽字体

        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        $pdf->setCellPaddings(1, 5, 0, 1);
// set default monospaced font
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
/*设置页面边幅:

       Left:左边幅

       Top:顶部边幅

       Right:右边幅

       Keepmargins:为true时,覆盖默认的PDF边幅。 */

        $pdf->SetMargins(PDF_MARGIN_LEFT, 20, PDF_MARGIN_RIGHT);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

        $l['a_meta_charset'] = 'UTF-8';
        $l['a_meta_dir'] = 'ltr';
        $l['a_meta_language'] = 'cn';
        // TRANSLATIONS --------------------------------------
        $l['w_page'] = 'page';

        // set some language-dependent strings (optional)
        if (@file_exists(APP_ROOT.'extensions/phppdf/lang/eng.php')) {
            require_once(APP_ROOT.'extensions/phppdf/lang/eng.php');
            $pdf->setLanguageArray($l);
        }

        $pdf->setFontSubsetting(true);
        $pdf->SetFont('dejavusans', '', 14, '', true);
        $pdf->SetFont('cid0cs', '', 10);
        $pdf->SetFont('stsongstdlight','', 10);

        $pdf->AddPage();

        $pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));

        $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 5, 0, true, '', true);
         /*输入PDF文档 :

       Name:PDF保存的名字

       Dest:PDF输出的方式。I,默认值,在浏览器中打开;D,点击下载按钮, PDF文件会被下载下来;F,文件会被保存在服务器中;S,PDF会以字符串形式输出;E:PDF以邮件的附件输出。 */

        $pdf->Output(APP_ROOT.'/uploadfiles/pdf_file/fenxi/'.$name.'.pdf', 'F');

    }

2.2、pdf里面的html代码

  /**
     * @param $data
     * pdf html代码
     */
    public function html_Make($data){
           $ht=<<<EOD
<table  align="center" width="100%"  border="1" cellpadding="0" cellspacing="0" style="border-color: gainsboro;" >
		 <tr align="center" style="line-height: 100px;">
            <td colspan="5" height="100" align="left" style="background-color: rgb(190,26,33);">
                 <img src="{$data['head']}" alt="{$data['head']}" width="900" height="100" style="margin-left: 0px; text-align: left;">
             </td>
</tr>

			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>客户姓名</b></td>
				<td colspan="2" height="25" align="center">{$data['xingming']}</td>
				<td width="20%" ><b>性别</b></td>
				<td  height="25" align="center">{$data['sex']}</td>
			</tr>
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>出生年月</b></td>
				<td colspan="2" height="25" align="center">{$data['date']}</td>
				<td width="20%" ><b>学历</b></td>
				<td  height="25" align="center">{$data['education']}</td>
			</tr>

			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>工作单位</b></td>
				<td colspan="4" height="25" align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$data['job']}</td>
			</tr>
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>所属科室</b></td>
				<td colspan="2" height="25" align="center">{$data['department']}</td>
				<td width="20%" ><b>职称</b></td>
				<td  height="25" align="center">{$data['job']}</td>
			</tr>
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>研究领域</b></td>
				<td colspan="4" height="25" align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$data['field']}</td>
			</tr>

			<tr align="left" >
				<td colspan="5" style="color: white; font-size: 16px; font-weight: bold;background-color: rgb(190,26,33);" > <font style="margin-left: 2%;">既往学术成果:</font></td>
			</tr>

			<tr align="center"  style="line-height: 20px;">
				<td colspan="5" height="20" align="center"  style="color: blue;font-weight: bold;" >基金项目</td>
			</tr>
EOD;

        foreach ($data['jijin'] as $v) {
            $ht.= <<<EOD
<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>基金项目</b></td>
				<td colspan="3" height="25" align="center">{$v[0]}</td>
				<td align="center">{$v[1]}</td>
			</tr>
EOD;
        }
        $ht.=<<<EOD
<tr align="center"  style="line-height: 20px;">
				<td colspan="5" height="20" align="center" style="color: blue;font-weight: bold;">SCI论文</td>
			</tr>
EOD;


        foreach ($data['sci'] as $v){
           $ht.=<<<EOD
<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>SCI论文</b></td>
				<td colspan="3" height="25" align="center">{$v[0]}</td>
				<td align="center">{$v[1]}</td>
			</tr>
EOD;
        }
        $ht.=<<<EOD
<tr align="center"  style="line-height: 20px;">
				<td colspan="5" height="20" align="center" style="color: blue;font-weight: bold;">中文论文</td>
			</tr>
EOD;


        foreach ($data['lun'] as $v){
            $ht.=<<<EOD
<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>中文论文</b></td>
				<td colspan="3" height="25" align="center">{$v[0]}</td>
				<td align="center">{$v[1]}</td>
			</tr>
EOD;
        }

       $ht.=<<<EOD
<tr align="center"  style="line-height: 20px;">
				<td colspan="5" height="20" align="center" style="color: blue;font-weight: bold;">专著出版</td>
			</tr>
EOD;

        foreach ($data['ban'] as $v){
        $ht.=<<<EOD
<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>专著出版</b></td>
				<td colspan="3" height="25" align="center">{$v[0]}</td>
				<td align="center">{$v[1]}</td>
			</tr>
EOD;

        }
       $ht.=<<<EOD
<tr align="center"  style="line-height: 20px;">
				<td colspan="5" height="20" align="center" style="color: blue;font-weight: bold;">专利申请</td>
			</tr>
EOD;


        foreach ($data['zhuan'] as $v){
             $ht.=<<<EOD
<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>专利申请</b></td>
				<td colspan="3" height="25" align="center">{$v[0]}</td>
				<td align="center">{$v[1]}</td>
			</tr>
EOD;
        }
        $ht.=<<<EOD
<tr align="left" >
				<td colspan="5" style="color: white; font-size: 16px; font-weight: bold;background-color: rgb(190,26,33);" > <font style="margin-left: 2%;">职称晋升条件:</font></td>
			</tr>
			
			<tr align="left"  style="line-height: 20px;">
				<td colspan="5" height="20" align="left">副高晋升条件:</td>
			</tr>
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>基本条件</b></td>
				<td colspan="2" height="25" align="center">{$data['fgao'][0]}</td>
				<td width="20%" ><b>选择条件</b></td>
				<td  height="25" align="center">{$data['fgao'][1]}</td>
			</tr>
			<tr align="left"  style="line-height: 20px;">
				<td colspan="5" height="20" align="left">正高晋升条件:</td>
			</tr>
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>基本条件</b></td>
				<td colspan="2" height="25" align="center">{$data['zgao'][0]}</td>
				<td width="20%" ><b>选择条件</b></td>
				<td  height="25" align="center">{$data['zgao'][1]}</td>
			</tr>
			<tr align="left"  style="line-height: 20px;">
				<td colspan="5" height="20" align="left">其他职称条件:</td>
			</tr>
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>基本条件</b></td>
				<td colspan="2" height="25" align="center">{$data['other'][0]}</td>
				<td width="20%" ><b>选择条件</b></td>
				<td  height="25" align="center">{$data['other'][1]}</td>
			</tr>
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>备注</b></td>
				<td colspan="4" height="25" align="center">{$data['other'][3]}</td>
			</tr>
			
	         <tr align="left" >
				<td colspan="5" style="color: white; font-size: 16px; font-weight: bold;background-color: rgb(190,26,33);" > <font style="margin-left: 2%;">晋升条件需求:</font></td>
			</tr>
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>预期晋升职称</b></td>
				<td colspan="2" height="25" align="center">{$data['pro_job']}</td>
				<td width="20%" ><b>预期晋升时间</b></td>
				<td  height="25" align="center">{$data['pro_time']}</td>
			</tr>
			
			<tr align="center" >
				<td width="20%" style="line-height: 40px;"><b>基金项目需求</b></td>
				<td colspan="4" align="center" height="40" style="line-height: 20px;">{$data['pro_jijin']}</td>
			</tr>
			
			<tr align="center" >
				<td width="20%" style="line-height: 40px;"><b>SCI论文需求</b></td>
				<td colspan="4" align="center" height="40" style="line-height: 20px;">{$data['pro_sci']}</td>
			</tr>
			
			<tr align="center" >
				<td width="20%" style="line-height: 40px;"><b>中文论文需求</b></td>
				<td colspan="4" align="center" height="40" style="line-height: 20px;">{$data['pro_lun']}</td>
			</tr>
			
			<tr align="center" >
				<td width="20%" style="line-height: 40px;"><b>专著出版需求</b></td>
				<td colspan="4" align="center" height="40" style="line-height: 20px;">{$data['pro_ban']}</td>
			</tr>
			
			<tr align="center" >
				<td width="20%" style="line-height: 40px;"><b>专利申请需求</b></td>
				<td colspan="4" align="center" height="40" style="line-height: 20px;">{$data['pro_zhuan']}</td>
			</tr>

            <tr align="left" >
				<td colspan="5" style="color: white; font-size: 16px; font-weight: bold;background-color: rgb(190,26,33);" > <font style="margin-left: 2%;">学术成果规划:</font></td>
			</tr>
			<tr align="center"  style="line-height: 20px;">
				<td colspan="5" height="20" align="center" style="color: red;font-weight: bold;">基金项目申报</td>
			</tr>
			
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>申报类型</b></td>
				<td colspan="4" height="25" align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$data['jin_type']}</td>
			</tr>
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>申报时间</b></td>
				<td colspan="4" height="25" align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$data['jin_time']}</td>
			</tr>
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>前期基础准备</b></td>
				<td colspan="4" height="25" align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$data['jin_ready']}</td>
			</tr>
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>申报课题名称</b></td>
				<td colspan="4" height="25" align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$data['jin_title']}</td>
			</tr>
<tr align="center"  style="line-height: 20px;">
                    <td colspan="5" height="20" align="center" style="color: red;font-weight: bold;">SCI论文发表</td>
                </tr>
                
                <tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>IF值要求</b></td>
				<td colspan="3" height="25" align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$data['sci_if']}分</td>
				<td align="center">{$data['sci_qu']}</td>
			    </tr>
              
                <tr align="center"  style="line-height: 25px;">
                    <td width="20%"><b>预期发表时间</b></td>
                    <td colspan="4" height="25" align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$data['sci_time']}</td>
                </tr>
                <tr align="center"  style="line-height: 25px;">
                    <td width="20%"><b>研究方向</b></td>
                    <td colspan="4" height="25" align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$data['sci_dir']}</td>
                </tr>
                <tr align="center"  style="line-height: 25px;">
                    <td width="20%"><b>论文初步定题</b></td>
                    <td colspan="4" height="25" align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$data['sci_ti']}</td>
                </tr>
                
                  <tr align="center"  style="line-height: 20px;">
				<td colspan="5" height="20" align="center" style="color: red;font-weight: bold;">中文论文发表</td>
			</tr>
			
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>期刊要求</b></td>
				<td colspan="4" height="25" align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$data['lun_kan']}</td>
			</tr>
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>预期发表时间</b></td>
				<td colspan="4" height="25" align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$data['lun_time']}</td>
			</tr>
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>研究方向</b></td>
				<td colspan="4" height="25" align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$data['lun_dir']}</td>
			</tr>
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>论文初步定题</b></td>
				<td colspan="4" height="25" align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$data['lun_ti']}</td>
			</tr>
			
			   <tr align="center"  style="line-height: 20px;">
				<td colspan="5" height="20" align="center" style="color: red;font-weight: bold;">专著出版</td>
			</tr>
			
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>专著字数要求</b></td>
				<td colspan="4" height="25" align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$data['ban_wordnum']}</td>
			</tr>
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>出版社要求</b></td>
				<td colspan="4" height="25" align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$data['ban_require']}</td>
			</tr>
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>专著方向</b></td>
				<td colspan="4" height="25" align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$data['ban_dir']}</td>
			</tr>
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>专著名称</b></td>
				<td colspan="4" height="25" align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$data['ban_name']}</td>
			</tr>
			

              <tr align="center"  style="line-height: 20px;">
				<td colspan="5" height="20" align="center" style="color: red;font-weight: bold;">专利申请</td>
			</tr>
			
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>期刊要求</b></td>
				<td colspan="4" height="25" align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$data['li_kan']}</td>
			</tr>
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>预期下证时间</b></td>
				<td colspan="4" height="25" align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$data['li_time']}</td>
			</tr>
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>申请方向</b></td>
				<td colspan="4" height="25" align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$data['li_dir']}</td>
			</tr>
			<tr align="center"  style="line-height: 25px;">
				<td width="20%"><b>专利名称</b></td>
				<td colspan="4" height="25" align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$data['li_name']}</td>
			</tr>
     
			<tr align="left"  >
				<td colspan="5" style="color: white; font-size: 16px; font-weight: bold;background-color: rgb(190,26,33);" > <font style="margin-left: 2%;">系统参数:</font></td>
			</tr>
			
			<tr align="center" style="line-height: 25px;">
				<td width="20%" ><b>评估时间</b></td>
				<td width="20%" height="25" >{$data['time']}</td>
				<td width="20%" ><b>评估专家</b></td>
				<td width="20%" >{$this->memberinfo['xingming']}</td>
				<td width="20%" ><b>高级学术顾问</b></td>
			</tr>
			<tr align="center" >
				<td width="20%" style="line-height: 25px;"><b>报告编号</b></td>
				<td colspan="4" align="center" height="25" style="line-height: 25px;">{$data['num']}</td>
			</tr>
			 <tr align="center" style="line-height: 200px;"> 
            <td colspan="5" height="200" align="left" style="background-color: rgb(190,26,33);">
                 <img src="{$data['foot']}" alt="{$data['foot']}" width="900" height="200" style="margin-left: 0px; text-align: left;">
             </td>
</tr>
			
		</table>
EOD;

return $ht;

    }

2.3、php处理方法

    /**
     * 添加处理
     */
    public function makeAction(){
        //定义pdf和二维码存储路径  
        $df="uploadfiles/pdf_file/fenxi/";
        $qf="uploadfiles/QRcode/fenxi/";
      
        if ($_POST){
            $data=$_POST;
            $m=$this->table->where("xingming='".$data['xingming']."'")->select();
            if ($m){
                exit(error("该客户已经存在"));
            }

            $bian=self::make_num();
            $data['num']=$bian['num'];
            $data['userid']=$this->memberinfo['id'];
            $data['username']=$this->memberinfo['xingming'];
            $data['inputtime']=time();
            $data['ip']=client::get_user_ip();
            $data['time']=date("Y-m-d",time());
            $data['head']=APP_ROOT.'extensions/phppdf/images/head.png';
            $data['foot']=APP_ROOT.'extensions/phppdf/images/foot.png';

            @$this->tcpdf($this->html_Make($data),$this->memberinfo['xingming'],$data['xingming'],'高级学术顾问',$bian['pdf']);
            if (!file_exists($df.$bian['pdf'].".pdf")){
                exit(error("pdf文件生成失败"));
            }

            $data['jijin']=json_encode($data['jijin']);
            $data['sci']=json_encode($data['sci']);
            $data['lun']=json_encode($data['lun']);
            $data['ban']=json_encode($data['ban']);
            $data['zhuan']=json_encode($data['zhuan']);

            $data['fgao']=json_encode($data['fgao']);
            $data['zgao']=json_encode($data['zgao']);
            $data['other']=json_encode($data['other']);

            $data['tag']="高级学术顾问";
            $data['file']=$df.$bian['pdf'].'.pdf';
            $data['img']=$qf.$bian['qr'].'.png';
           // var_dump($data);die;
            $res=$this->table->insert($data);
            if ($res){
                exit(success('学术规划分析报告生成成功',$res));
            }else{
                exit(error("提交错误"));
            }
        }

        $this->view->display('fenxi/make');
    }

2.4、php生成二维码代码

  /**
     * @param $value
     * @param int $size
     * @param $name
     * 生成二维码
     */
    public function qrcode($value,$size=6,$name){
        //加载类库文件
        require_once APP_ROOT."/extensions/QRcode/phpqrcode.php";
        // $value = 'http://sy.techlandgroup.com'; //二维码内容
        $errorCorrectionLevel = 'H';//容错级别
        $matrixPointSize = $size;//生成图片大小
        ob_clean();
        QRcode::png($value, 'uploadfiles/QRcode/fenxi/'.$name.'.png', $errorCorrectionLevel, $matrixPointSize, 2);
    }

2.5、生成随机字符编号

    /**
     * @return array
     * 生成随机字符编号
     */
    public static function make_num(){
        list($usee,$sec)=explode(" ",microtime());
        $usee=substr(str_replace('0.','',$usee),0,6);
        $str=rand(10,99);
        $yy=date("YmdHis").$usee.$str;
        $arr=array(
            'pdf'=>"PDF_".$yy,
            'qr'=>"QR_".$yy,
            'num'=>$yy
        );
        return $arr;
    }

3:pdf呈现界面。

 

                                                                                                                                           联系作者:jackhhy520@qq.com