123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Elton
- * Date: 2019/7/24
- * Time: 10:21
- */
- namespace app\common\service;
- class PdfService
- {
- /**
- * @var PdfService
- */
- private static $self;
- /**
- * @return PdfService
- */
- public static function instance()
- {
- if(self::$self == NULL){
- self::$self = new self();
- }
- return self::$self;
- }
- /**
- * @param $content
- * @param $outType I,在浏览器中打开;D,以文件形式下载;F,保存到服务器中;S,以字符串形式输出;E:以邮件的附件输出。
- */
- public function mpdf($filename, $content, $filetitle = '', $showType='I')
- {
- require_once ROOT_PATH .'vendor/tecnickcom/tcpdf/tcpdf.php';
- $title = $filename;
- $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
- $fontFile=ROOT_PATH . 'vendor/tecnickcom/tcpdf/fonts/stsongstdlight.php';
- $pdf->AddFont('stsongstdlight', '', $fontFile, FALSE);
- $pdf->SetCreator(PDF_CREATOR);
- $pdf->SetAuthor("作者");//设置作者
- $pdf->SetTitle($title);
- $pdf->SetSubject('TCPDF Tutorial');
- //$pdf->SetKeywords('TCPDF, PDF, example, test, guide');//设置关键字
- // 是否显示页眉
- $pdf->setPrintHeader(false);
- // 设置页眉显示的内容
- //$pdf->SetHeaderData($logo, 60, '', '');
- // 设置页眉字体
- //$pdf->setHeaderFont(Array('deja2vusans', '', '12'));
- // 页眉距离顶部的距离
- $pdf->SetHeaderMargin('5');
- // 是否显示页脚
- //$pdf->setPrintFooter(true);
- // 设置页脚显示的内容
- //$pdf->setFooterData(array(0,64,0), array(0,64,128));
- // 设置页脚的字体
- //$pdf->setFooterFont(Array('dejavusans', '', '10'));
- // 设置页脚距离底部的距离
- //$pdf->SetFooterMargin('10');
- // 设置默认等宽字体
- $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
- // 设置行高
- $pdf->setCellHeightRatio(1.5);
- // 设置左、上、右的间距
- $pdf->SetMargins('15', '15', '15');
- // set auto page breaks
- $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
- // 设置字体
- $pdf->SetFont('stsongstdlight', '', 12, '', true);
- // 设置是否自动分页 距离底部多少距离时分页
- //$pdf->SetAutoPageBreak(TRUE, '15');
- // 设置图像比例因子
- $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
- //$pdf->setFontSubsetting(true);
- $pdf->AddPage("A4","Landscape",true,true);
- //$pdf->writeHTML($content);//HTML生成PDF
- $pdf->SetFont('stsongstdlight', '', 20, '', true);
- $pdf->writeHTMLCell(0, 10, '', '', $filetitle, 0, 1, 0, true, 'C', true);
- $pdf->SetFont('stsongstdlight', '', 12, '', true);
- $pdf->writeHTML($content, true, false, true, false, 'L');//设置logo
- //$pdf->writeHTMLCell(0, 0, '', '', $content, 0, 1, 0, true, '', true);
- $pdf->Output("{$filename}.pdf", $showType);
- //return $pdf->Output(ROOT_PATH."/{$filename}.pdf", $showType);
- //exit();
- }
- }
|