1,安装tcpdf
composer require tecnickcom/tcpdf
成功后vendor文件夹下会有tecnickcom文件夹
2,引入
use TCPDF;
3,使用
$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('标签打印');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
$pdf->SetMargins(10, 10, 10);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
//set some language-dependent strings
$l['a_meta_charset'] = 'UTF-8';
$l['a_meta_dir'] = 'ltr';
$l['a_meta_language'] = 'cn';
$l['w_page'] = 'page';
$pdf->setLanguageArray($l);
// set default font subsetting mode
$pdf->setFontSubsetting(true);
// set font
//droidsansfallback简体中文的意思,默认是没有的,需要自己后面加文件
$pdf->SetFont('droidsansfallback', '', 12);
// add a page
$pdf->AddPage();```
$htmldetails='<table>';
$htmldetails='<tr>';
$htmldetails='<td>大致是这么个意思</td>';
$htmldetails='</tr>';
$htmldetails='</table>';
//一定要靠边
$html = <<<EOF
$htmldetails
EOF;
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->lastPage();
$pdf->Output('demo.pdf', 'I');
//不加这个页面输出会是乱码
ob_end_flush();
4,加入简体中文包
下载后解压至 vendor\tecnickcom\tcpdf\fonts\