当前位置:首页 > CMS教程 > Thinkphp > 列表

thinkphp如何轻松实现pdf的导出(利用tcpdf插件)

发布:smiling 来源: PHP粉丝网  添加日期:2020-04-06 22:37:20 浏览: 评论:0 

本篇文章主要讲述的是thinkphp轻松实现pdf导出的方法代码,具有一定的参考价值,感兴趣的朋友可以了解一下,希望能对你有所帮助。

代码编写前先引入tcpdf整个文件夹到项目目录的ThinkPHP文件夹下 如:/ThinkPHP/Library/Vendor/tcpdf

其他的不多说直接上代码

导出考试结果明细

  1. public function export() 
  2.  
  3. { 
  4.  
  5. //  导出考试结果明细(PDF) 
  6.  
  7.     $id = I('id'); 
  8.  
  9.     $detailed = D('member_test_result'); 
  10.  
  11.     $parameter = $detailed->detailedResults($id); 
  12.  
  13.     $name = $parameter['member_name']; 
  14.  
  15.     $result = json_decode($parameter['test_result_str']); 
  16.  
  17.     foreach ($result as $k => $v) { 
  18.  
  19.         $test = M('test_cont'); 
  20.  
  21.         $array['question_title'] = $test->where('id=' . $k)->getField('qustion_title'); 
  22.  
  23.         //正确选项 
  24.  
  25.         $array['state'] = $test->where('id=' . $k)->getField('state'); 
  26.  
  27.         //正确答案 
  28.  
  29.         $wheres['test_id'] = $k; 
  30.  
  31.         $wheres['state'] = $array['state']; 
  32.  
  33.         $array['stateresult'] = M('test_answer')->where($wheres)->getField('answer_name'); 
  34.  
  35.         //选项 
  36.  
  37.         $array['cont'] = $v; 
  38.  
  39.         //选项内容 
  40.  
  41.         $where['test_id'] = $k; 
  42.  
  43.         $where['state'] = $array['cont']; 
  44.  
  45.         $array['result'] = M('test_answer')->where($where)->getField('answer_name'); 
  46.  
  47.         $data[] = $array; 
  48.  
  49.     } 
  50.  
  51.     $content = '<!doctype html>'; 
  52.  
  53.     $content .= '<html lang="en">'; 
  54.  
  55.     $content .= '<head>'; 
  56.  
  57.     $content .= '<meta charset="UTF-8" />'; 
  58.  
  59.     $content .= '<title>考试结果</title>'; 
  60.  
  61.     $content .= '</head>'; 
  62.  
  63.     $content .= '<body>'; 
  64.  
  65.     $content .= '<p class="content">'; 
  66.  
  67.     $content .= '<p align="center" style="color: #0a6ebd;font-size: 24px"><b>考试结果</b></p>'; 
  68.  
  69.     $content .= ' <p style="color:#6a6a6a;letter-spacing:4px">'; 
  70.  
  71.     $content .= '<p><span>姓名:'; 
  72.  
  73.     $content .= $name; 
  74.  
  75.     $content .= '</span>'; 
  76.  
  77.     $content .= '<span style="color:#fff;">1231'; 
  78.  
  79.     $content .= '</span>'; 
  80.  
  81.     $content .= '<span style="" >考试用时:'; 
  82.  
  83.     $content .= gmdate("i:s", $parameter['time_cost']); 
  84.  
  85.     $content .= '</span>'; 
  86.  
  87.     $content .= '<span style="color:#fff;">1231'; 
  88.  
  89.     $content .= '</span>'; 
  90.  
  91.     $content .= '<span style="">考试分数:'; 
  92.  
  93.     $content .= $parameter['score']; 
  94.  
  95.     $content .= '</span>'; 
  96.  
  97.     $content .= '<hr/>'; 
  98.  
  99.     foreach ($data as $k => $v) { 
  100.  
  101.         $content .= '<p style=font-size: 20px><b>'; 
  102.  
  103.         $content .= $k + 1; 
  104.  
  105.         $content .= '、</b>'; 
  106.  
  107.         $content .= $v['question_title']; 
  108.  
  109.         $content .= '</p>'; 
  110.  
  111.         $content .= '<p style=" font-size: 14px">您的选项为:<span style="color:#0a6ebd;">'; 
  112.  
  113.         $content .= $v['cont']; 
  114.  
  115.         $content .= '</span></p>'; 
  116.  
  117.         $content .= '<p style=" font-size: 14px">您的答案为:<span style="color:#0a6ebd;">'; 
  118.  
  119.         $content .= $v['result']; 
  120.  
  121.         $content .= '</span></p>'; 
  122.  
  123.         $content .= '<p style=" font-size: 14px">正确选项为:<span style="color:red;">'; 
  124.  
  125.         $content .= $v['state']; 
  126.  
  127.         $content .= '</span></p>'; 
  128.  
  129.         $content .= '<p style=" font-size: 14px">正确答案为:<span style="color:red;">'; 
  130.  
  131.         $content .= $v['stateresult']; 
  132.  
  133.         $content .= '</span></p>'; 
  134.  
  135.     }; 
  136.  
  137.     $content .= '</p>'; 
  138.  
  139.     $content .= '</body>'; 
  140.  
  141.     $content .= '</html>'; 
  142.  
  143.     pdf($content); 
  144.  
  145.   
  146.  
  147. } 

pdf方法

  1. function pdf($html){ 
  2.  
  3.     vendor('Tcpdf.tcpdf'); 
  4.  
  5.     $pdf = new \Tcpdf('P', 'mm', 'A4', true, 'UTF-8', false); 
  6.  
  7.     // 设置打印模式,设置文档信息 
  8.  
  9.     $pdf->SetCreator(PDF_CREATOR); 
  10.  
  11.     $pdf->SetAuthor('Nicola Asuni'); 
  12.  
  13.     $pdf->SetTitle('Examination result(考试结果)'); 
  14.  
  15.     $pdf->SetSubject('TCPDF Tutorial'); 
  16.  
  17.     $pdf->SetKeywords('TCPDF, PDF, PHP, example, test, guide'); 
  18.  
  19.     // 是否显示页眉和是否显示页脚 
  20.  
  21.     $pdf->setPrintHeader(false); 
  22.  
  23.     $pdf->setPrintFooter(true); 
  24.  
  25.     // 设置页眉和页脚信息内容 
  26.  
  27.     $pdf->SetHeaderData('logo.jpg', 40, 'Helloweba.com', '小白测试', array(0,64,255), array(0,64,128)); 
  28.  
  29.     $pdf->setFooterData(array(0,64,0), array(0,64,128)); 
  30.  
  31.     // 设置页眉和页脚字体 
  32.  
  33.     $pdf->setHeaderFont(Array('dejavusans', '', '12')); 
  34.  
  35.     $pdf->setFooterFont(Array('dejavusans', '', '10')); 
  36.  
  37.     // 设置间距 
  38.  
  39.     $pdf->SetHeaderMargin('5'); 
  40.  
  41.     $pdf->SetFooterMargin('10'); 
  42.  
  43.     // 设置左、上、右的间距 
  44.  
  45.     $pdf->SetMargins('10', '10', '10'); 
  46.  
  47.     // 设置是否自动分页  距离底部多少距离时分页 
  48.  
  49.     $pdf->SetAutoPageBreak(TRUE, '15'); 
  50.  
  51.     // 设置默认等宽字体 
  52.  
  53.     $pdf->SetDefaultMonospacedFont('courier'); 
  54.  
  55.     // 设置行高 
  56.  
  57.     $pdf->setCellHeightRatio(1); 
  58.  
  59.     // 设置图像比例因子 
  60.  
  61.     $pdf->setImageScale(1.25); 
  62.  
  63.     //设置默认字体子集模式 
  64.  
  65.     $pdf->setFontSubsetting(true); 
  66.  
  67.     // 设置字体 
  68.  
  69.     $pdf->SetFont('stsongstdlight', '', 14, '', true); 
  70.  
  71.     $pdf->AddPage(); 
  72.  
  73.     $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); 
  74.  
  75.   //PDF输出   I:在浏览器中打开,D:下载,F:在服务器生成pdf ,S:只返回pdf的字符串 
  76.  
  77.   $pdf->Output(rand_string('9').'.pdf', 'I'); 
  78.  
  79.   } 

Tags: thinkphp tcpdf插件

分享到: