Home > Backend Development > PHP Tutorial > How can thinkphp easily export pdf (using tcpdf plug-in)

How can thinkphp easily export pdf (using tcpdf plug-in)

little bottle
Release: 2023-04-06 12:04:01
forward
5606 people have browsed it

This article mainly talks about the method code of thinkphp to easily realize pdf export. It has certain reference value. Interested friends can learn about it. I hope it can be helpful to you.

Before writing the code, first introduce the entire tcpdf folder to the ThinkPHP folder in the project directory, as follows: /ThinkPHP/Library/Vendor/tcpdf

Not much else to say, just go to the code

Export exam result details

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

    }
Copy after login

pdf method

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;);
  }
Copy after login

Related tutorials: PHP video tutorial

The above is the detailed content of How can thinkphp easily export pdf (using tcpdf plug-in). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template