Thinkphp框架引用tcpdf插件,插件下載地址:待續。。。
代碼編寫前先引入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 = '<!doctype html>';
$content .= '<html lang="en">';
$content .= '<head>';
$content .= '<meta charset="UTF-8" />';
$content .= '<title>考試結果</title>';
$content .= '</head>';
$content .= '<body>';
$content .= '<div class="content">';
$content .= '<p align="center" style="color: #0a6ebd;font-size: 24px"><b>考試結果</b></p>';
$content .= ' <div style="color:#6a6a6a;letter-spacing:4px">';
$content .= '<p><span>姓名:';
$content .= $name;
$content .= '</span>';
$content .= '<span style="color:#fff;">1231';
$content .= '</span>';
$content .= '<span style="" >考試用時:';
$content .= gmdate("i:s", $parameter['time_cost']);
$content .= '</span>';
$content .= '<span style="color:#fff;">1231';
$content .= '</span>';
$content .= '<span style="">考試分數:';
$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">您的選項為:<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">正確選項為:<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 .= '</div>';
$content .= '</body>';
$content .= '</html>';
pdf($content);
}
pdf方法
function pdf($html){
vendor('Tcpdf.tcpdf');
$pdf = new \Tcpdf('P', 'mm', 'A4', true, 'UTF-8', false);
// 設置打印模式,設置文檔信息
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('Examination result(考試結果)');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, PHP, example, test, guide');
// 是否顯示頁眉和是否顯示頁腳
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(true);
// 設置頁眉和頁腳信息內容
$pdf->SetHeaderData('logo.jpg', 40, 'Helloweba.com', '小白測試', array(0,64,255), array(0,64,128));
$pdf->setFooterData(array(0,64,0), array(0,64,128));
// 設置頁眉和頁腳字體
$pdf->setHeaderFont(Array('dejavusans', '', '12'));
$pdf->setFooterFont(Array('dejavusans', '', '10'));
// 設置間距
$pdf->SetHeaderMargin('5');
$pdf->SetFooterMargin('10');
// 設置左、上、右的間距
$pdf->SetMargins('10', '10', '10');
// 設置是否自動分頁 距離底部多少距離時分頁
$pdf->SetAutoPageBreak(TRUE, '15');
// 設置默認等寬字體
$pdf->SetDefaultMonospacedFont('courier');
// 設置行高
$pdf->setCellHeightRatio(1);
// 設置圖像比例因子
$pdf->setImageScale(1.25);
//設置默認字體子集模式
$pdf->setFontSubsetting(true);
// 設置字體
$pdf->SetFont('stsongstdlight', '', 14, '', true);
$pdf->AddPage();
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
//PDF輸出 I:在瀏覽器中打開,D:下載,F:在服務器生成pdf ,S:只返回pdf的字符串
$pdf->Output(rand_string('9').'.pdf', 'I');
}