thinkphp實(shí)現(xiàn)pdf導(dǎo)出的方法

小編給大家分享一下thinkphp實(shí)現(xiàn)pdf導(dǎo)出的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

成都創(chuàng)新互聯(lián)長(zhǎng)期為上千家客戶(hù)提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為烈山企業(yè)提供專(zhuān)業(yè)的成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作,烈山網(wǎng)站改版等技術(shù)服務(wù)。擁有十余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。

代碼編寫(xiě)前先引入tcpdf整個(gè)文件夾到項(xiàng)目目錄的ThinkPHP文件夾下 如:/ThinkPHP/Library/Vendor/tcpdf

其他的不多說(shuō)直接上代碼

導(dǎo)出考試結(jié)果明細(xì)

    public function export()
    {
    //  導(dǎo)出考試結(jié)果明細(xì)(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');
            //正確選項(xiàng)
            $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');
            //選項(xiàng)
            $array['cont'] = $v;
            //選項(xiàng)內(nèi)容
            $where['test_id'] = $k;
            $where['state'] = $array['cont'];
            $array['result'] = M('test_answer')->where($where)->getField('answer_name');
            $data[] = $array;
        }
        $content = '<!doctype html>';
        $content .= '<html lang="en">';
        $content .= '<head>';
        $content .= '<meta charset="UTF-8" />';
        $content .= '<title>考試結(jié)果</title>';
        $content .= '</head>';
        $content .= '<body>';
        $content .= '<p class="content">';
        $content .= '<p align="center" style="color: #0a6ebd;font-size: 24px"><b>考試結(jié)果</b></p>';
        $content .= ' <p style="color:#6a6a6a;letter-spacing:4px">';
        $content .= '<p><span>姓名:';
        $content .= $name;
        $content .= '</span>';
        $content .= '<span style="color:#fff;">1231';
        $content .= '</span>';
        $content .= '<span style="" >考試用時(shí):';
        $content .= gmdate("i:s", $parameter['time_cost']);
        $content .= '</span>';
        $content .= '<span style="color:#fff;">1231';
        $content .= '</span>';
        $content .= '<span style="">考試分?jǐn)?shù):';
        $content .= $parameter['score'];
        $content .= '</span>';
        $content .= '<hr/>';
        foreach ($data as $k => $v) {
            $content .= '<p style=font-size: 20px><b>';
            $content .= $k + 1;
            $content .= '、</b>';
            $content .= $v['question_title'];
            $content .= '</p>';
            $content .= '<p style=" font-size: 14px">您的選項(xiàng)為:<span style="color:#0a6ebd;">';
            $content .= $v['cont'];
            $content .= '</span></p>';
            $content .= '<p style=" font-size: 14px">您的答案為:<span style="color:#0a6ebd;">';
            $content .= $v['result'];
            $content .= '</span></p>';
            $content .= '<p style=" font-size: 14px">正確選項(xiàng)為:<span style="color:red;">';
            $content .= $v['state'];
            $content .= '</span></p>';
            $content .= '<p style=" font-size: 14px">正確答案為:<span style="color:red;">';
            $content .= $v['stateresult'];
            $content .= '</span></p>';
        };
        $content .= '</p>';
        $content .= '</body>';
        $content .= '</html>';
        pdf($content);

    }

pdf方法

function pdf($html){
    vendor('Tcpdf.tcpdf');
    $pdf = new \Tcpdf('P', 'mm', 'A4', true, 'UTF-8', false);
    // 設(shè)置打印模式,設(shè)置文檔信息
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Nicola Asuni');
    $pdf->SetTitle('Examination result(考試結(jié)果)');
    $pdf->SetSubject('TCPDF Tutorial');
    $pdf->SetKeywords('TCPDF, PDF, PHP, example, test, guide');
    // 是否顯示頁(yè)眉和是否顯示頁(yè)腳
    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(true);
    // 設(shè)置頁(yè)眉和頁(yè)腳信息內(nèi)容
    $pdf->SetHeaderData('logo.jpg', 40, 'Helloweba.com', '小白測(cè)試', array(0,64,255), array(0,64,128));
    $pdf->setFooterData(array(0,64,0), array(0,64,128));
    // 設(shè)置頁(yè)眉和頁(yè)腳字體
    $pdf->setHeaderFont(Array('dejavusans', '', '12'));
    $pdf->setFooterFont(Array('dejavusans', '', '10'));
    // 設(shè)置間距
    $pdf->SetHeaderMargin('5');
    $pdf->SetFooterMargin('10');
    // 設(shè)置左、上、右的間距
    $pdf->SetMargins('10', '10', '10');
    // 設(shè)置是否自動(dòng)分頁(yè)  距離底部多少距離時(shí)分頁(yè)
    $pdf->SetAutoPageBreak(TRUE, '15');
    // 設(shè)置默認(rèn)等寬字體
    $pdf->SetDefaultMonospacedFont('courier');
    // 設(shè)置行高
    $pdf->setCellHeightRatio(1);
    // 設(shè)置圖像比例因子
    $pdf->setImageScale(1.25);
    //設(shè)置默認(rèn)字體子集模式
    $pdf->setFontSubsetting(true);
    // 設(shè)置字體
    $pdf->SetFont('stsongstdlight', '', 14, '', true);
    $pdf->AddPage();
    $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
  //PDF輸出   I:在瀏覽器中打開(kāi),D:下載,F(xiàn):在服務(wù)器生成pdf ,S:只返回pdf的字符串
  $pdf->Output(rand_string('9').'.pdf', 'I');
  }

看完了這篇文章,相信你對(duì)thinkphp實(shí)現(xiàn)pdf導(dǎo)出的方法有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)站名稱(chēng):thinkphp實(shí)現(xiàn)pdf導(dǎo)出的方法
本文來(lái)源:http://muchs.cn/article18/ijdggp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、App設(shè)計(jì)、用戶(hù)體驗(yàn)、定制網(wǎng)站ChatGPT、自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化