{"id":223573,"date":"2024-05-11T19:22:22","date_gmt":"2024-05-11T11:22:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/223573.html"},"modified":"2024-05-11T19:22:32","modified_gmt":"2024-05-11T11:22:32","slug":"%e5%a6%82%e4%bd%95%e7%94%a8-php-%e5%ae%9e%e7%8e%b0%e7%94%9f%e6%88%90-pdf-%e6%96%87%e4%bb%b6%e7%9a%84%e5%8a%9f%e8%83%bd-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/223573.html","title":{"rendered":"\u5982\u4f55\u7528 PHP \u5b9e\u73b0\u751f\u6210 PDF \u6587\u4ef6\u7684\u529f\u80fd"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25143146\/04d5e0e6-d412-45f1-9f0f-022d27ffc0e8.webp\" alt=\"\u5982\u4f55\u7528 PHP \u5b9e\u73b0\u751f\u6210 PDF \u6587\u4ef6\u7684\u529f\u80fd\" \/><\/p>\n<p><p>\u5728PHP\u4e2d\u751f\u6210PDF\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528\u5e93\u5982<strong>TCPDF<\/strong>\u3001<strong>FPDF<\/strong>\u6216<strong>dompdf<\/strong>\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u7a0b\u5e8f\u5458\u6240\u9700\u7684\u4e00\u5207\u529f\u80fd\u4ee5\u4fbf\u4ecePHP\u4ee3\u7801\u751f\u6210PDF\u6587\u4ef6\u3002\u5c24\u5176\u662f<strong>TCPDF<\/strong>\uff0c\u5b83\u652f\u6301UTF-8\u3001\u81ea\u5b9a\u4e49\u5b57\u4f53\u3001\u80fd\u591f\u751f\u6210\u590d\u6742\u7684\u6587\u6863\uff0c\u5e76\u4e0eCSS\u6837\u5f0f\u9ad8\u5ea6\u517c\u5bb9\u3002\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u5e93\u4e2d\u7684\u4efb\u4f55\u4e00\u4e2a\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u4e00\u4e2a\u5168\u9762\u7684PDF\u751f\u6210\u7cfb\u7edf\uff0c\u80fd\u591f\u5904\u7406\u6587\u672c\u3001\u56fe\u50cf\u751a\u81f3\u662fHTML\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u9009\u62e9PDF\u751f\u6210\u5e93<\/h2>\n<\/p>\n<p><p>\u5728PHP\u73af\u5883\u4e2d\u751f\u6210PDF\u6587\u4ef6\u524d\uff0c\u9996\u5148\u9700\u8981\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u5e93\u6765\u5b8c\u6210\u8fd9\u9879\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>TCPDF<\/h3>\n<\/p>\n<p><p><strong>TCPDF<\/strong>\u662f\u76ee\u524d\u6700\u53d7\u6b22\u8fce\u7684\u5e93\u4e4b\u4e00\uff0c\u5b83\u4e0d\u9700\u8981\u5916\u90e8PDF\u5e93\u5c31\u80fd\u5b9e\u73b0\u5168\u90e8\u529f\u80fd\uff0c\u652f\u6301\u6240\u6709\u7684ISO\u9875\u9762\u5c3a\u5bf8\u548c\u81ea\u5b9a\u4e49\u9875\u9762\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>FPDF<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u4e2a\u9009\u9879\u662f<strong>FPDF<\/strong>\uff0c\u5b83\u662f\u4e00\u4e2a\u5f00\u6e90\u5e93\uff0c\u5b83\u4ee5\u4ee3\u7801\u7b80\u6d01\u8457\u79f0\uff0c\u5bf9\u4e8e\u65b0\u624b\u6765\u8bf4\u975e\u5e38\u5bb9\u6613\u4e0a\u624b\u3002<\/p>\n<\/p>\n<p><h3>dompdf<\/h3>\n<\/p>\n<p><p><strong>dompdf<\/strong>\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u5e93\uff0c\u5b83\u53ef\u4ee5\u63a5\u6536HTML\u6587\u6863\u5e76\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3aPDF\u683c\u5f0f\u3002\u8fd9\u5bf9\u9700\u8981\u6e32\u67d3\u590d\u6742\u5e03\u5c40\u548c\u6837\u5f0f\u5316\u6587\u672c\u7684\u5f00\u53d1\u8005\u6765\u8bf4\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u5b89\u88c5\u548c\u914d\u7f6e<\/h2>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u4e2a\u5e93\uff0c\u4f60\u90fd\u9700\u8981\u6309\u7167\u76f8\u5e94\u7684\u6307\u5357\u6765\u5b89\u88c5\u548c\u914d\u7f6e\u5b83\u3002<\/p>\n<\/p>\n<p><h3>TCPDF\u5b89\u88c5<\/h3>\n<\/p>\n<p><pre><code class=\"language-php\">require_once(&#039;tcpdf_include.php&#039;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6b64\u4ee3\u7801\u5f15\u5165TCPDF\u5e93\uff0c\u6216\u8005\u53ef\u4ee5\u4f7f\u7528Composer\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">composer require tecnickcom\/tcpdf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>FPDF\u5b89\u88c5<\/h3>\n<\/p>\n<p><pre><code class=\"language-php\">require_once(&#039;fpdf.php&#039;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0eTCPDF\u76f8\u4f3c\uff0c\u4e5f\u53ef\u901a\u8fc7Composer\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h3>dompdf\u5b89\u88c5<\/h3>\n<\/p>\n<p><pre><code class=\"language-php\">require_once(&#039;dompdf\/autoload.inc.php&#039;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u540c\u6837\u4e5f\u652f\u6301\u4f7f\u7528Composer\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u57fa\u7840\u7684PDF\u751f\u6210<\/h2>\n<\/p>\n<p><p>\u751f\u6210PDF\u7684\u57fa\u672c\u6b65\u9aa4\uff0c\u4e0d\u8bba\u4f7f\u7528\u54ea\u4e2a\u5e93\uff0c\u90fd\u9075\u5faa\u7740\u7c7b\u4f3c\u7684\u6d41\u7a0b\uff1a\u521d\u59cb\u5316\u3001\u6dfb\u52a0\u9875\u9762\u3001\u5199\u5165\u5185\u5bb9\u3001\u8f93\u51faPDF\u3002<\/p>\n<\/p>\n<p><h3>TCPDF\u7684\u57fa\u7840\u751f\u6210<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528TCPDF\u751f\u6210PDF\u7684\u4e00\u4e2a\u57fa\u672c\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, &#039;UTF-8&#039;, false);<\/p>\n<p>$pdf-&gt;AddPage();<\/p>\n<p>$pdf-&gt;Write(0, &#039;Hello World&#039;);<\/p>\n<p>$pdf-&gt;Output(&#039;example_001.pdf&#039;, &#039;I&#039;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>FPDF\u4f7f\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5728FPDF\u4e2d\u7684\u57fa\u672c\u751f\u6210\u6d41\u7a0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">$pdf = new FPDF();<\/p>\n<p>$pdf-&gt;AddPage();<\/p>\n<p>$pdf-&gt;SetFont(&#039;Arial&#039;, &#039;B&#039;, 16);<\/p>\n<p>$pdf-&gt;Cell(40, 10, &#039;Hello World!&#039;);<\/p>\n<p>$pdf-&gt;Output();<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u52a0\u6df1\u7406\u89e3\u548c\u6269\u5c55<\/h2>\n<\/p>\n<p><p>\u5728\u638c\u63e1\u4e86\u57fa\u7840\u751f\u6210\u6b65\u9aa4\u540e\uff0c\u53ef\u4ee5\u8fdb\u884c\u66f4\u590d\u6742\u7684PDF\u6587\u6863\u751f\u6210\uff0c\u6bd4\u5982\u6dfb\u52a0<strong>\u56fe\u50cf<\/strong>\u3001<strong>\u8868\u683c<\/strong>\u548c<strong>HTML<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u63d2\u5165\u56fe\u50cf\u548c\u8868\u683c<\/h3>\n<\/p>\n<p><p>\u5728PDF\u4e2d\u63d2\u5165\u56fe\u50cf\u548c\u521b\u5efa\u8868\u683c\u662f\u5e38\u89c1\u9700\u6c42\uff0c\u5404\u4e2a\u5e93\u90fd\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u529f\u80fd\u6765\u5b9e\u73b0\u8fd9\u4e9b\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u5728TCPDF\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>Image()<\/code>\u65b9\u6cd5\u63d2\u5165\u56fe\u50cf\uff0c\u4f7f\u7528\u591a\u4e2a<code>Cell()<\/code>\u65b9\u6cd5\u6765\u521b\u5efa\u8868\u683c\u3002<\/p>\n<\/p>\n<p><p>FPDF\u4e5f\u63d0\u4f9b\u4e86<code>Image()<\/code>\u65b9\u6cd5\u63d2\u5165\u56fe\u50cf\uff0c\u8868\u683c\u540c\u6837\u901a\u8fc7<code>Cell()<\/code>\u65b9\u6cd5\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u652f\u6301HTML\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u60f3\u5728PDF\u4e2d\u5305\u542bHTML\u5185\u5bb9\uff0cdompdf\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u89e3\u6790HTML\u548cCSS\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">$dompdf = new Dompdf();<\/p>\n<p>$dompdf-&gt;loadHtml(&#039;&lt;h1&gt;Welcome to my website&lt;\/h1&gt;&#039;);<\/p>\n<p>$dompdf-&gt;render();<\/p>\n<p>$dompdf-&gt;stream();<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>TCPDF\u540c\u6837\u652f\u6301\u4e00\u4e9bHTML\u5185\u5bb9\u7684\u751f\u6210\uff0c\u901a\u8fc7<code>writeHTML()<\/code>\u65b9\u6cd5\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u7ed3\u8bed<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u5728PHP\u4e2d\u5b9e\u73b0\u751f\u6210PDF\u6587\u4ef6\u7684\u529f\u80fd\u3002\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u7279\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u5927\u5e45\u63d0\u5347\u5de5\u4f5c\u6548\u7387\u5e76\u4e3a\u4f60\u7684\u9879\u76ee\u5e26\u6765\u66f4\u5927\u7684\u7075\u6d3b\u6027\u548c\u5f3a\u5927\u7684\u529f\u80fd\u3002\u901a\u8fc7\u719f\u6089\u5982\u4f55\u64cd\u4f5c\u5e93\u4e2d\u7684\u51fd\u6570\u548c\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8bbe\u8ba1\u51fa\u6ee1\u8db3\u9879\u76ee\u9700\u6c42\u7684PDF\u6587\u6863\uff0c\u65e0\u8bba\u5b83\u4eec\u662f\u7b80\u5355\u7684\u62a5\u8868\u8fd8\u662f\u5185\u5bb9\u4e30\u5bcc\u7684\u7535\u5b50\u4e66\u7c4d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u95ee\u98981\uff1aPHP\u5982\u4f55\u751f\u6210 PDF \u6587\u4ef6\uff1f<\/strong><\/p>\n<p>\u56de\u7b541\uff1a\u751f\u6210 PDF \u6587\u4ef6\u662f PHP \u5f00\u53d1\u4e2d\u5e38\u89c1\u7684\u9700\u6c42\u4e4b\u4e00\u3002\u8981\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u4f8b\u5982fpdf\u6216tcpdf\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u51fd\u6570\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa PDF \u6587\u4ef6\u5e76\u5b9a\u4e49\u5176\u5185\u5bb9\u3001\u683c\u5f0f\u548c\u6837\u5f0f\u3002\u901a\u5e38\u6b65\u9aa4\u5305\u62ec\u521b\u5efa PDF \u5bf9\u8c61\u3001\u8bbe\u7f6e\u9875\u9762\u5c5e\u6027\u3001\u6dfb\u52a0\u5185\u5bb9\u548c\u4fdd\u5b58\u6587\u4ef6\u3002\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u76f8\u5e94\u7684\u65b9\u6cd5\u8bbe\u7f6e\u6807\u9898\u3001\u6bb5\u843d\u3001\u8868\u683c\u3001\u56fe\u50cf\u7b49\u5143\u7d20\uff0c\u5e76\u4e14\u53ef\u4ee5\u8bbe\u5b9a\u5b57\u4f53\u3001\u989c\u8272\u3001\u9875\u9762\u5e03\u5c40\u7b49\u3002\u6700\u540e\uff0c\u901a\u8fc7\u8c03\u7528\u4fdd\u5b58\u65b9\u6cd5\u5c06\u751f\u6210\u7684 PDF \u6587\u4ef6\u4fdd\u5b58\u5230\u670d\u52a1\u5668\u6216\u4e0b\u8f7d\u5230\u672c\u5730\u3002<\/p>\n<p><strong>\u95ee\u98982\uff1a\u6709\u6ca1\u6709\u7b80\u5355\u6613\u7528\u7684 PHP \u5e93\u53ef\u4ee5\u7528\u6765\u751f\u6210 PDF \u6587\u4ef6\uff1f<\/strong><\/p>\n<p>\u56de\u7b542\uff1a\u5f53\u7136\u6709\uff01\u5982\u679c\u4f60\u4e0d\u60f3\u4ece\u5934\u5f00\u59cb\u5b9e\u73b0\u751f\u6210 PDF \u6587\u4ef6\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u4e00\u4e9b\u66f4\u7b80\u5355\u6613\u7528\u7684 PHP \u5e93\uff0c\u4f8b\u5982Dompdf\u6216mPDF\u3002\u8fd9\u4e9b\u5e93\u90fd\u57fa\u4e8e HTML \u548c CSS \u89e3\u6790\u5668\uff0c\u53ef\u4ee5\u76f4\u63a5\u5c06 HTML \u6587\u4ef6\u8f6c\u6362\u4e3a PDF \u6587\u4ef6\u3002\u4f60\u53ea\u9700\u8981\u7f16\u5199\u597d HTML \u5185\u5bb9\uff0c\u7136\u540e\u5c06\u5176\u4f20\u9012\u7ed9\u76f8\u5e94\u5e93\u7684\u8f6c\u6362\u65b9\u6cd5\u5373\u53ef\u3002\u8fd9\u6837\u505a\u4e0d\u4ec5\u7b80\u5316\u4e86\u5f00\u53d1\u8fc7\u7a0b\uff0c\u8fd8\u53ef\u4ee5\u5229\u7528\u719f\u6089\u7684 HTML \u548c CSS \u8bed\u6cd5\u6765\u5b9a\u4e49 PDF \u6587\u4ef6\u7684\u6837\u5f0f\u548c\u5e03\u5c40\u3002<\/p>\n<p><strong>\u95ee\u98983\uff1a\u5982\u4f55\u5728\u751f\u6210\u7684 PDF \u6587\u4ef6\u4e2d\u6dfb\u52a0\u52a8\u6001\u6570\u636e\uff1f<\/strong><\/p>\n<p>\u56de\u7b543\uff1a\u5982\u679c\u4f60\u9700\u8981\u5728\u751f\u6210\u7684 PDF \u6587\u4ef6\u4e2d\u63d2\u5165\u52a8\u6001\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5185\u7f6e\u7684 PHP \u51fd\u6570\u6216\u7b2c\u4e09\u65b9\u5e93\u63d0\u4f9b\u7684\u65b9\u6cd5\u3002\u901a\u5e38\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u6570\u636e\u5e93\u6216\u5176\u4ed6\u6570\u636e\u6e90\u83b7\u53d6\u9700\u8981\u7684\u6570\u636e\uff0c\u7136\u540e\u6839\u636e PDF \u6587\u4ef6\u7684\u7ed3\u6784\u548c\u683c\u5f0f\uff0c\u5728\u76f8\u5e94\u7684\u4f4d\u7f6e\u63d2\u5165\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u636e\u5e93\u67e5\u8be2\u7ed3\u679c\uff0c\u5e76\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u6dfb\u52a0\u4e00\u884c\u8868\u683c\u6216\u6bb5\u843d\u3002\u53e6\u5916\uff0c\u4e00\u4e9b\u5e93\u4e5f\u63d0\u4f9b\u4e86\u7279\u5b9a\u7684\u65b9\u6cd5\u6765\u521b\u5efa\u8868\u683c\u7b49\u590d\u6742\u7ed3\u6784\uff0c\u5e76\u5c06\u6570\u636e\u586b\u5145\u5230\u76f8\u5e94\u7684\u5355\u5143\u683c\u4e2d\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u5b9e\u73b0\u6839\u636e\u5177\u4f53\u9700\u6c42\u751f\u6210\u5305\u542b\u52a8\u6001\u6570\u636e\u7684 PDF \u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728PHP\u4e2d\u751f\u6210PDF\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528\u5e93\u5982TCPDF\u3001FPDF\u6216dompdf\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u7a0b\u5e8f\u5458\u6240\u9700\u7684\u4e00\u5207\u529f [&hellip;]","protected":false},"author":3,"featured_media":223588,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/223573"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=223573"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/223573\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/223588"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=223573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=223573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=223573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}