2017-06-12 16:02:08 2475次浏览 3条回答 0 悬赏 10 金钱

最近项目中要把页面直接生成pdf,老是出错,有什么好的栗子可以参考

  • 回答于 2017-06-12 16:08 举报
    public function outreport($result){
    	ob_start();
    	// Include the main TCPDF library (search for installation path).
    	vendor('Pdf.tcpdf');
    
    	//实例化
    	$pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    
    	// 设置文档信息 
    	$pdf->SetCreator(PDF_CREATOR);
    	$pdf->SetAuthor('Nicola Asuni');
    	$pdf->SetTitle('TCPDF Example 001');
    	$pdf->SetSubject('TCPDF Tutorial');
    	$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
    
    	// 设置页眉和页脚信息
    	$pdf->SetHeaderData('', '', '电脑人机工程评估报告', '', array(0,64,255), array(0,64,128));
    	$pdf->setFooterData(array(0,64,0), array(0,64,128));
    
    	// 设置页眉和页脚字体 
    	$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    	$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
    
    	// 设置默认等宽字体 
    	$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
    
    	// 设置间距 
    	$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    	$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    	$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
    
    	// 设置分页 
    	$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
    
    	// set image scale factor
    	$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
    
    	// set some language-dependent strings (optional)
    	if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
    		require_once(dirname(__FILE__).'/lang/eng.php');
    		$pdf->setLanguageArray($l);
    	}
    
    	// ---------------------------------------------------------
    
    	// set default font subsetting mode
    	$pdf->setFontSubsetting(true);
    
    	//设置字体 
    	$pdf->SetFont('dejavusans', '', 14, '', true);
    	$pdf->SetFont('stsongstdlight', '', 10);
    
    	// Add a page
    	// This method has several options, check the source code documentation for more information.
    	$pdf->AddPage();
    
    	// set text shadow effect
    	$pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));
    
    	// 内容
    	$html =$result['report'];
    
    	// Print text using writeHTMLCell()
    	$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
    
    	// ---------------------------------------------------------
    
    	// Close and output PDF document
    	// This method has several options, check the source code documentation for more information.
    	$dtime = date('Y-m-d');
    	$pdf->Output($dtime.'.pdf', 'I');
    
    	//============================================================+
    	// END OF FILE
    	//============================================================+
    		}
    }
    
    3 条回复
    回复于 2017-06-12 16:50 回复

    感谢粘贴,现在出现乱码了,调试了也没用

    回复于 2017-06-12 16:59 回复

    怎么乱码???

    回复于 2017-06-12 17:14 回复

    刚刚修改了,修改了config配置:SetHeaderData里中文的解决方法为:tcpdf\config\tcpdf_config.php

    define (‘PDF_FONT_NAME_MAIN’, ’helvetica’);
    
    改为define (‘PDF_FONT_NAME_MAIN’, ’stsongstdlight’);
    
    define (‘PDF_FONT_NAME_DATA’, ’helvetica’);
    
    改为define (‘PDF_FONT_NAME_DATA’, ’stsongstdlight’);
    
  • 回答于 2017-06-12 16:33 举报

    有报错信息没的阿,出啥问题了阿!

    2 条回复
    回复于 2017-06-12 16:51 回复

    开始用的mpdf,类里面的方法不对,真是郁闷

    回复于 2017-06-13 08:55 回复

    人生在于.....

  • 回答于 2017-06-12 16:44 举报

    我还以为你想吃栗子

    1 条回复
    回复于 2017-06-12 16:52 回复

    是不是邪恶了

您需要登录后才可以回答。登录 | 立即注册
vipdhf
见习主管

vipdhf

注册时间:2016-04-06
最后登录:2018-12-06
在线时长:21小时7分
  • 粉丝2
  • 金钱100
  • 威望10
  • 积分410

热门问题