1、composer require tecnickcom/tcpdf
2、在控制器中use TCPDF;
3、
public function bpdf(){ $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'); // set default header data $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128)); $pdf->setFooterData(array(0,64,0), array(0,64,128)); // set header and footer fonts $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); // set default monospaced font $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); // set margins $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); $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 (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); // Set font // dejavusans is a UTF-8 Unicode font, if you only need to // print standard ASCII chars, you can use core fonts like // helvetica or times to reduce file size. $pdf->SetFont('dejavusans', '', 14, '', true); // 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')); // Set some content to print $html = <<<EOD <h2 style="text-align:center">Advanced Biomedicine</h2><h3 style="text-align:center">$title</h3><h4 style="text-align:center">--Manuscript Draft--</h4> <table style="margin-top:10px;" width="100%" border="1" align="left" cellpadding="0" cellspacing="0"> <tr><td>Manuscript Number:</td><td>$item_number</td></tr> <tr><td>Article Type:</td><td>$type</td></tr> <tr><td>Corresponding Author</td><td>$author</td></tr> <tr><td>First Author:</td><td>$first_author</td></tr> <tr><td>Communication Author:</td><td>$communication_author</td></tr> <tr><td>Order of Authors:</td><td>$author</td></tr> <tr><td>Suggested Reviewers:</td><td>$reviewers</td></tr> </table> EOD; // 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. $time=time(); $year=date('Y',$time); $month=date('m',$time); $day=date('d',$time); $dir=public_path().'upload/'.$year.$month.$day; if (!file_exists($dir)) { mkdir ($dir,0777); } $pdf->Output(public_path().'upload/'.$year.$month.$day.'/'.$time.'.pdf', 'F'); $pdf_name='/upload/'.$year.$month.$day.'/'.$time.'.pdf'; //$pdf->Output(public_path().'example_001.pdf', 'F'); return 'example_001.pdf'; }
如果生成的页面需要是一个表格,那么注意以下事项:
1、手动换行
<tr nobr="true"> <td>your content here</td> </tr>
2、可以设置css:
$html = <<<EOD <style> div{ maring-left:20px; } td{ border:1px solid #ccc; font-size:16px; line-height:130%; } p{font-size:16px} </style> <h2 style="margin-top:15px">$productData[product_name]</h2> <p>Catalog Number:$productData[catalog_number]</p> <table cellspacing="0" cellpadding="10"> <tr style="color:#fff;background-color:#1c6aba;font-weight:bold;line-height:36px"><td colspan="2"> DESCRIPTION</td></tr> <tr><td width="30%">Product Name</td><td width="70%">$productData[product_name]</td></tr> <tr><td width="30%">Gene Name</td><td width="70%">$productData[gene_name]</td></tr> <tr><td width="30%">Source</td><td width="70%">$productData[source]</td></tr> <tr><td width="30%">Alternative names</td><td width="70%">$productData[alternative_names]</td></tr> </table> <table cellspacing="0" cellpadding="10"> <tr style="color:#fff;background-color:#1c6aba;font-weight:bold;line-height:36px"><td colspan="2"> SPECIFICATIONS</td></tr> $spe_thml </table> <table cellspacing="0" cellpadding="10"> <tr style="color:#fff;background-color:#1c6aba;font-weight:bold;line-height:36px"><td colspan="2"> BACKGROUND</td></tr> <tr><td width="30%" >Gene Accession</td><td width="70%" >$productData[gene_accession]</td></tr> <tr><td width="30%" >Gene Alias</td><td width="70%" >$productData[gene_alias]</td></tr> <tr><td width="30%">Background</td><td width="70%">$productData[background]</td></tr> </table> EOD;
3、是否自动翻页:
// 设置是否自动分页 距离底部多少距离时分页 $pdf->SetAutoPageBreak(false, '15');