使用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]);  //下载
 

 

posted @ 2022-11-19 10:56  carol2014  阅读(154)  评论(0编辑  收藏  举报