首頁  >  文章  >  後端開發  >  thinkphp如何輕鬆實現pdf的匯出(利用tcpdf插件)

thinkphp如何輕鬆實現pdf的匯出(利用tcpdf插件)

little bottle
little bottle轉載
2019-04-26 17:18:085472瀏覽

本篇文章主要講述的是thinkphp輕鬆實現pdf導出的方法程式碼,具有一定的參考價值,有興趣的朋友可以了解一下,希望能對你有幫助。

程式碼寫前先引入tcpdf整個資料夾到專案目錄的ThinkPHP資料夾下如:/ThinkPHP/Library/Vendor/tcpdf

其他的不多說直接上程式碼 

匯出考試結果明細

    public function export()
    {
    //  导出考试结果明细(PDF)
        $id = I('id');
        $detailed = D('member_test_result');
        $parameter = $detailed->detailedResults($id);
        $name = $parameter['member_name'];
        $result = json_decode($parameter['test_result_str']);
        foreach ($result as $k => $v) {
            $test = M('test_cont');
            $array['question_title'] = $test->where('id=' . $k)->getField('qustion_title');
            //正确选项
            $array['state'] = $test->where('id=' . $k)->getField('state');
            //正确答案
            $wheres['test_id'] = $k;
            $wheres['state'] = $array['state'];
            $array['stateresult'] = M('test_answer')->where($wheres)->getField('answer_name');
            //选项
            $array['cont'] = $v;
            //选项内容
            $where['test_id'] = $k;
            $where['state'] = $array['cont'];
            $array['result'] = M('test_answer')->where($where)->getField('answer_name');
            $data[] = $array;
        }
        $content = &#39;<!doctype html>&#39;;
        $content .= &#39;<html lang="en">&#39;;
        $content .= &#39;<head>&#39;;
        $content .= &#39;<meta charset="UTF-8" />&#39;;
        $content .= &#39;<title>考试结果</title>&#39;;
        $content .= &#39;</head>&#39;;
        $content .= &#39;<body>&#39;;
        $content .= &#39;<p class="content">&#39;;
        $content .= &#39;<p align="center" style="color: #0a6ebd;font-size: 24px"><b>考试结果</b></p>&#39;;
        $content .= &#39; <p style="color:#6a6a6a;letter-spacing:4px">&#39;;
        $content .= &#39;<p><span>姓名:&#39;;
        $content .= $name;
        $content .= &#39;</span>&#39;;
        $content .= &#39;<span style="color:#fff;">1231&#39;;
        $content .= &#39;</span>&#39;;
        $content .= &#39;<span style="" >考试用时:&#39;;
        $content .= gmdate("i:s", $parameter[&#39;time_cost&#39;]);
        $content .= &#39;</span>&#39;;
        $content .= &#39;<span style="color:#fff;">1231&#39;;
        $content .= &#39;</span>&#39;;
        $content .= &#39;<span style="">考试分数:&#39;;
        $content .= $parameter[&#39;score&#39;];
        $content .= &#39;</span>&#39;;
        $content .= &#39;<hr/>&#39;;
        foreach ($data as $k => $v) {
            $content .= &#39;<p style=font-size: 20px><b>&#39;;
            $content .= $k + 1;
            $content .= &#39;、</b>&#39;;
            $content .= $v[&#39;question_title&#39;];
            $content .= &#39;</p>&#39;;
            $content .= &#39;<p style=" font-size: 14px">您的选项为:<span style="color:#0a6ebd;">&#39;;
            $content .= $v[&#39;cont&#39;];
            $content .= &#39;</span></p>&#39;;
            $content .= &#39;<p style=" font-size: 14px">您的答案为:<span style="color:#0a6ebd;">&#39;;
            $content .= $v[&#39;result&#39;];
            $content .= &#39;</span></p>&#39;;
            $content .= &#39;<p style=" font-size: 14px">正确选项为:<span style="color:red;">&#39;;
            $content .= $v[&#39;state&#39;];
            $content .= &#39;</span></p>&#39;;
            $content .= &#39;<p style=" font-size: 14px">正确答案为:<span style="color:red;">&#39;;
            $content .= $v[&#39;stateresult&#39;];
            $content .= &#39;</span></p>&#39;;
        };
        $content .= &#39;</p>&#39;;
        $content .= &#39;</body>&#39;;
        $content .= &#39;</html>&#39;;
        pdf($content);

    }

#pdf方法
##

function pdf($html){
    vendor(&#39;Tcpdf.tcpdf&#39;);
    $pdf = new \Tcpdf(&#39;P&#39;, &#39;mm&#39;, &#39;A4&#39;, true, &#39;UTF-8&#39;, false);
    // 设置打印模式,设置文档信息
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor(&#39;Nicola Asuni&#39;);
    $pdf->SetTitle(&#39;Examination result(考试结果)&#39;);
    $pdf->SetSubject(&#39;TCPDF Tutorial&#39;);
    $pdf->SetKeywords(&#39;TCPDF, PDF, PHP, example, test, guide&#39;);
    // 是否显示页眉和是否显示页脚
    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(true);
    // 设置页眉和页脚信息内容
    $pdf->SetHeaderData(&#39;logo.jpg&#39;, 40, &#39;Helloweba.com&#39;, &#39;小白测试&#39;, array(0,64,255), array(0,64,128));
    $pdf->setFooterData(array(0,64,0), array(0,64,128));
    // 设置页眉和页脚字体
    $pdf->setHeaderFont(Array(&#39;dejavusans&#39;, &#39;&#39;, &#39;12&#39;));
    $pdf->setFooterFont(Array(&#39;dejavusans&#39;, &#39;&#39;, &#39;10&#39;));
    // 设置间距
    $pdf->SetHeaderMargin(&#39;5&#39;);
    $pdf->SetFooterMargin(&#39;10&#39;);
    // 设置左、上、右的间距
    $pdf->SetMargins(&#39;10&#39;, &#39;10&#39;, &#39;10&#39;);
    // 设置是否自动分页  距离底部多少距离时分页
    $pdf->SetAutoPageBreak(TRUE, &#39;15&#39;);
    // 设置默认等宽字体
    $pdf->SetDefaultMonospacedFont(&#39;courier&#39;);
    // 设置行高
    $pdf->setCellHeightRatio(1);
    // 设置图像比例因子
    $pdf->setImageScale(1.25);
    //设置默认字体子集模式
    $pdf->setFontSubsetting(true);
    // 设置字体
    $pdf->SetFont(&#39;stsongstdlight&#39;, &#39;&#39;, 14, &#39;&#39;, true);
    $pdf->AddPage();
    $pdf->writeHTMLCell(0, 0, &#39;&#39;, &#39;&#39;, $html, 0, 1, 0, true, &#39;&#39;, true);
  //PDF输出   I:在浏览器中打开,D:下载,F:在服务器生成pdf ,S:只返回pdf的字符串
  $pdf->Output(rand_string(&#39;9&#39;).&#39;.pdf&#39;, &#39;I&#39;);
  }


######################################相關教學:###PHP影片教學#########

以上是thinkphp如何輕鬆實現pdf的匯出(利用tcpdf插件)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:cnblogs.com。如有侵權,請聯絡admin@php.cn刪除