ÔõÑùʹÓÃThinkPHP6¾ÙÐÐPDFÎļþÌìÉú²Ù×÷£¿
Ëæ×ÅÐÅÏ¢»¯Ê±´úµÄÉú³¤£¬pdfÎĵµÒѳÉΪһÑùƽ³£ÊÂÇ顢ѧϰÖв»¿É»òȱµÄÒ»²¿·Ö¡£¶øËæ×Å»¥ÁªÍø¡¢Òƶ¯»¥ÁªÍøµÄÆÕ¼°£¬ÒÔ¼°µç×Ó»¯Àú³ÌµÄÍƽø£¬pdfÎĵµµÄÓ¦Óó¡¾°Ò²ÔÚÒ»Ö±À©´ó£¬ºÃ±È±¨±í¡¢Ö¤Êé¡¢ÌõÔ¼µÈ·½Ãæ¡£Òò´Ë£¬Í¨¹ý´úÂëÌìÉúpdfÎĵµµÄʵÏÖ·½·¨Ò²³ÉΪÁËÐí¶àÆóÒµºÍСÎÒ˽ÈË¿ª·¢ÕßµÄÐèÇó¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃthinkphp6¾ÙÐÐpdfÎļþÌìÉú²Ù×÷£¬Ï£Íû¶ÔÄãÓÐËù×ÊÖú¡£
Ò»¡¢Å侰֪ʶ
ÔÚ×îÏÈÏÈÈÝÔõÑùʹÓÃThinkPHP6¾ÙÐÐPDFÎļþÌìÉú֮ǰ£¬ÐèÒªÎÒÃÇÏàʶһЩ»ù±¾µÄÅ侰֪ʶ£º
PDFÊÇʲô£¿
PDF£¨Portable Document Format£©¼´±ãЯʽÎĵµÃûÌã¬ÊÇÓÉAdobe¹«Ë¾¿ª·¢µÄÒ»ÖÖÓÃÓÚÍøÂç´«ÊäºÍ´òÓ¡µÈ»·½ÚµÄµç×ÓÎļþÃûÌ㬿ÉÒÔÔÚÖݪֲÙ×÷ϵͳÉϱ»Ê¹Óá£Ëü¿ÉÒÔ½«Îĵµ¡¢Í¼ÐΡ¢Í¼Æ¬¡¢×ÖÌåµÈÔªËØÕûºÏµ½Ò»¸öÎļþÖС£
ThinkPHPÊÇʲô£¿
ThinkPHPÊÇÒ»¿î»ùÓÚPHPÓïÑԵĿªÔ´PHP¿ò¼Ü£¬ÊÇÖйú×îÊ¢ÐеÄPHP¿ò¼ÜÖ®Ò»¡£ThinkPHP×ñÕÕMVC¼Ü¹¹£¬Ö§³ÖÖÚ¶àµÄÊý¾Ý¿â²Ù×÷¡¢Ä£°åÒýÇ桢ȨÏÞÈÏÖ¤¡¢»º´æµÈ¹¦Ð§£¬¿ÉÒÔΪ¿ª·¢ÕßÌṩ¼òÆÓ¡¢¸ßЧ¡¢Çå¾²µÄ¿ª·¢ÌåÑé¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
PDFÎļþµÄÌìÉú·½·¨ÓÐÄÄЩ£¿
Ò»Ñùƽ³£À´Ëµ£¬PDFÎļþµÄÌìÉú·½·¨ÓÐÒÔϼ¸ÖÖ£º
£¨1£©ÊÖ¶¯±àдPDFÎļþ
£¨2£©Ê¹ÓõÚÈý·½¿âÌìÉúPDFÎļþ
£¨3£©Ê¹ÓÃÔÆЧÀÍÌìÉúPDFÎļþ
¶øÔÚÕâÈýÖÖ·½·¨ÖУ¬Ê¹ÓõÚÈý·½¿âÊÇ×îΪ³£ÓõÄÒ»ÖÖ£¬ÓÉÓÚÆä¾ßÓмòÆÓÒ×Óᢹ¦Ð§ÆëÈ«¡¢Ö§³ÖµÄÓïÑÔÒ²ºÜÆÕ±éµÈÓŵ㡣
¶þ¡¢Ê¹ÓÃThinkPHP6½¨ÉèÌìÉúPDFÎļþµÄ´úÂë
×°ÖÃTCPDF×é¼þ
ÔÚʹÓÃThinkPHP6¾ÙÐÐPDFÎļþÌìÉú֮ǰ£¬ÎÒÃÇÐèÒªÏÈ×°ÖÃTCPDF×é¼þ¡£TCPDFÊÇÒ»¿î¿ªÔ´µÄPHPÀà¿â£¬ÓÃÓÚÌìÉúPDFÎĵµ¡£¿ÉÒÔÔÚTCPDF¹ÙÍø»òGithubÉÏÏÂÔØTCPDF×é¼þ£¬²¢Æ¾Ö¤¹Ù·½ÎĵµµÄÖ¸Òý¾ÙÐÐ×°Öá£ÔÚÕâÀÎÒÃÇ¿ÉÒÔͨ¹ýComposer×°ÖÃËü¡£
ÔÚterminal´°¿ÚÖÐÊäÈëÒÔÏÂÏÂÁîÐУº
composer require tecnickcom/tcpdf
µÇ¼ºó¸´ÖÆ
ÆÚ´ý×°ÖÃÍê³É£¬TCPDF×é¼þ»á×Ô¶¯Ìí¼Óµ½vendorĿ¼Ï¡£
½¨ÉèPDFÎļþÌìÉú¿ØÖÆÆ÷
ÔÚʹÓÃThinkPHP6¿ò¼ÜÌìÉúPDFÎļþ֮ǰ£¬ÐèÒª½¨ÉèÒ»¸öPDFÎļþÌìÉúµÄ¿ØÖÆÆ÷¡£ÔÚÏîÄ¿ÎļþĿ¼ÖУ¬Í¨¹ýÈçÏÂÏÂÁîÔÚapp/controllerĿ¼Ï½¨ÉèÒ»¸öGeneratepdf.phpÎļþ×÷Ϊ¿ØÖÆÆ÷£º
php think make:controller Generatepdf
µÇ¼ºó¸´ÖÆ
±àдPDFÎļþÌìÉú´úÂë
ÔÚÌìÉúPDFÎļþµÄ¿ØÖÆÆ÷ÖУ¬ÐèҪʵÏÖÌìÉúPDFÎļþµÄÒªÁì¡£ÔÚ´Ë£¬ÎÒÃÇÒÔÌìÉú¼òÆÓµÄPDFÎļþΪÀý¡£
ÔÚÌìÉúPDFÎļþµÄÒªÁìÖУ¬ÎÒÃÇÐèҪŲÓÃTCPDF¿â£¬²¢ÊµÏÖPDFÎļþµÄÌìÉú¡£ÎÒÃÇÐèÒª×öÒÔϼ¸¸ö°ì·¨£º
£¨1£©ÒýÈëTCPDF¿â
ÔÚ¿ØÖÆÆ÷ÖÐÒýÈëTCPDF¿â£¬²¢¾ÙÐгõʼ»¯¡£¿ÉÒ԰ݼûÒÔÏ´úÂë¾ÙÐÐʵÏÖ£º
use TCPDF; class Generatepdf extends BaseController { public function index() { // ÒýÈëTCPDF¿â require_once('../vendor/tecnickcom/tcpdf/tcpdf.php'); // ³õʼ»¯¹¤¾ß $pdf = new TCPDF(); } }
µÇ¼ºó¸´ÖÆ
£¨2£©ÉèÖÃPDFÎļþ²ÎÊý
¿ÉÒÔÔÚÌìÉúPDFÎļþµÄÒªÁìÖУ¬ÉèÖÃPDFÎļþµÄÊôÐÔ¡¢Ò³Ãæ¾Þϸ¡¢Ò³ÃæÆ«ÏòµÈÊôÐÔ£¬¿ÉÒ԰ݼûÒÔÏ´úÂë¾ÙÐÐʵÏÖ£º
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->SetCreator('ThinkPHP6'); $pdf->SetAuthor('×÷Õß'); $pdf->SetTitle('PDFÎĵµ'); $pdf->SetSubject('PDFÎĵµ Demo'); $pdf->SetKeywords('PDF, Demo, TCPDF, PHP'); $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_RIGHT, PDF_MARGIN_TOP); $pdf->SetHeaderMargin(0); $pdf->SetFooterMargin(PDF_MARGIN_BOTTOM); $pdf->setPrintHeader(false); $pdf->setPrintFooter(false); $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); $pdf->SetFont('cid0jp', '', 14); $pdf->AddPage();
µÇ¼ºó¸´ÖÆ
£¨3£©ÏòPDFÎļþÖÐÌí¼ÓÄÚÈÝ
¿ÉÒÔʹÓÃTCPDF¿âÌṩµÄÒªÁ죬ÏòPDFÎļþÖÐÌí¼ÓͼÏñ¡¢Îı¾¡¢±í¸ñµÈÔªËØ£¬¿ÉÒ԰ݼûÒÔÏ´úÂë¾ÙÐÐʵÏÖ£º
$pdf->Image('test.png', 10, 10, 150, 100, '', '', '', false, 300, '', false, false, 0, false, false, false); $pdf->SetFont('cid0jp', '', 20); $pdf->Cell(0,20,'Hello,World',0,1,'C'); $pdf->Ln(); $pdf->Ln(); $style = array('border' => 1, 'padding' => '2', 'header_line' => true, 'color' => array(255, 255, 255), 'font' => 'cid0jp', 'font_size' => 10); $pdf->writeHTMlTable($data, $style);
µÇ¼ºó¸´ÖÆ
£¨4£©ÌìÉúPDFÎļþ
ÔÚÍê³ÉÏòPDFÎļþÖÐÌí¼ÓÔªËصIJÙ×÷ºó£¬»¹ÐèҪŲÓÃOutput()ÒªÁìÀ´Êä³öPDFÎļþ£¬ÔÚä¯ÀÀÆ÷ÉÏÏÔʾ»òÏÂÔØÎļþ¡£
$pdf->Output('test.pdf', 'D');
µÇ¼ºó¸´ÖÆ
Èý¡¢×ܽá
ͨ¹ýÒÔÉÏ°ì·¨£¬ÎÒÃǾͿÉÒÔʹÓÃThinkPHP6¿ò¼Ü¶ÔPDFÎĵµ¾ÙÐÐÌìÉú¡£ÔÚ¿ª·¢Àú³ÌÖУ¬»¹ÐèҪעÖØPDFÎļþ·¾¶µÄÎÊÌ⣬¿ÉÒÔʹÓÃÏà¶Ô·¾¶»ò¾ø¶Ô·¾¶À´½â¾ö¡£±ðµÄ£¬TCPDF¿â»¹ÌṩÁËÐí¶àÆäËûµÄ¹¦Ð§£¬¿ÉÒÔƾ֤ÏÖʵÐèÇó¾ÙÐÐŲÓúÍÀ©Õ¹¡£Ï£Íû±¾ÎÄÄÚÈݶÔʹÓÃThinkPHP6ÌìÉúPDFÎļþµÄ¿ª·¢ÕßÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃThinkPHP6¾ÙÐÐPDFÎļþÌìÉú²Ù×÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡