使用dompdf等php包将html生成为pdf文件
<?php require_once './libs/tcpdf/vendor/autoload.php'; require_once './libs/mpdf/vendor/autoload.php'; require_once './libs/dompdf/vendor/autoload.php'; $content = '<div style="width: 80%;padding-right: 3rem;padding-left:3rem;margin-right:3rem;margin-left:3rem;"> <table border="1" style="width: 100%; margin-bottom: 1rem; color: #212529;vertical-align: top;border-color: #dee2e6;border-collapse: collapse;border: 1px solid black;"> <thead> <tr style="height: 4rem;"> <th>#</th> <th>First</th> <th>Last</th> <th>Handle</th> </tr> </thead> <tbody> <tr style="height: 3rem;"> <th scope="row"><input type="checkbox"></th> <td>Mark</td> <td>Otto</td> <td>@mdo</td> </tr> <tr style="height: 3rem;"> <th scope="row"><input type="checkbox"></th> <td>Jacob</td> <td>Thornton</td> <td>@fat</td> </tr> <tr style="height: 3rem;"> <th><input type="checkbox"></th> <td colspan="2">Larry the Bird</td> <td>@twitter</td> </tr> </tbody> </table> </div>'; // ob_start(); // $a = 123; // require('./pdf-temp2.php'); // $content = ob_get_contents(); // ob_end_clean(); // $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // $pdf->SetFont('msyh', '', 8); // $pdf->setPrintHeader(false); // $pdf->AddPage(); // $pdf->writeHTML($content, true, false, false, false, ''); // $pdf->Output('example.pdf', 'I'); // $mpdf = new \Mpdf\Mpdf(['UTF-8', 'A4', '', '', 32, 25, 27, 25, 16, 13]); // $mpdf->writeHTML($content); // $mpdf->Output(); $dompdf = new \Dompdf\Dompdf(); $dompdf->loadHtml($content); $dompdf->setPaper('A4', 'portrait'); $dompdf->render(); $dompdf->output(); // $dompdf->stream(); //输出到浏览器 $dompdf->stream('my.pdf', ['Attachment' => 0]); //下载
分类:
php
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!