PHP,从 HTML 生成 PDF 文件

标签 php pdf tcpdf mpdf

在生成 pdf 文件方面我是新手

我尝试过 tcpdf 和 mpdf,但没有得到我想要的格式。

这是我的 mpdf 代码

include('./mpdf.php');
    $mpdf=new mPDF();
    $html = '<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="cssForTable.css">
</head>
<body>
<table>
    <thead>
    <tr>
        <th width="25%">Client Details</th>
        <th colspan="2" width="50%"><h2 style="padding-top: 0; margin-bottom: 0; margin-top: 0;text-align: center"><strong>ROAD TRAFFIC ACCIDENT</strong></h2></th>
        <th width="25%">Legal HQ</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td width="50%" colspan="2">
            <strong>Instructed Another Solicito?</strong> &nbsp; &nbsp; &nbsp; &nbsp;
            Yes &nbsp; <input type="checkbox" /> &nbsp; &nbsp; No &nbsp; <input type="checkbox" />
        </td>
        <th width="50%" colspan="2" rowspan="3">
            Claiming For:<br />
            Personal Injury &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Yes &nbsp; <input type="checkbox" style="margin-bottom: 7px;" />
                            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;No &nbsp; <input type="checkbox" style="margin-bottom: 7px;" /><br />
            Vehicle Damage &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Yes &nbsp; <input type="checkbox" style="margin-bottom: 7px;" />
                             &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;No &nbsp; <input type="checkbox" style="margin-bottom: 7px;" /><br />
            Loss of Earnings &nbsp; &nbsp; &nbsp; &nbsp; Yes &nbsp; <input type="checkbox" />
                             &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;No &nbsp; <input type="checkbox" />
        </th>
    </tr>
    <tr><th colspan="2"><h3 style="padding-top: 0; margin-bottom: 0; margin-top: 0;">ACCIDENT DETAILS</h3></th></tr>
    <tr>
        <th width="25%">Date of Accident</th>
        <td width="25%">&nbsp;</td>
    </tr>
    <tr>
        <th width="25%">Time of Accident</th>
        <td width="25%">&nbsp;</td>
        <th width="25%">No of Occupants (inc driver)</th>
        <td width="25%">&nbsp;</td>
    </tr>
    <tr>
        <th width="25%">Location of Accident</th>
        <td width="25%">&nbsp;</td>
        <td width="50%" colspan="2" rowspan="2" valign="top" >
            <strong>Were Police Involved? &nbsp; &nbsp; &nbsp; &nbsp;
                Yes &nbsp; <input type="checkbox" /> &nbsp; &nbsp; 
                No &nbsp; <input type="checkbox" />
            <br />
            <br />If so, Police No/Station: </strong>
        </td>
    </tr>
    <tr>
        <td width="25%" colspan="2" rowspan="2" valign="top"><strong>Accident Circumstances</strong><br /><br /></td>
    </tr>
    <tr>
        <th width="25%" colspan="2">
            Is Client:<br />
            Owner <input type="checkbox" style="margin-left: 15em; margin-bottom: 7px;" /><br />
            Driver &nbsp;<input type="checkbox" style="margin-left: 15em; margin-bottom: 7px;" /><br />
            Passenger &nbsp;<input type="checkbox" style="margin-left: 13em;" />
        </th>
    </tr>
    <tr>
        <th width="25%">Weather Conditions</th>
        <th width="75%" colspan="3">        
            <label style="margin-left: 2em; margin-bottom: 7px;" > Sunny &nbsp; &nbsp; <input type="checkbox" /></label>
            <label style="margin-left: 8em; margin-bottom: 7px;" > Rain &nbsp; &nbsp; <input type="checkbox" /></label>
            <label style="margin-left: 8em; margin-bottom: 7px;" > Snow &nbsp; &nbsp; <input type="checkbox" /></label><br />
            <label style="margin-left: 2em; margin-bottom: 7px;" > Ice &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="checkbox" /></label>
            <label style="margin-left: 8em; margin-bottom: 7px;" > Fog &nbsp; &nbsp;  &nbsp;<input type="checkbox" /></label>
            <label style="margin-left: 8em; margin-bottom: 7px;" > Dark &nbsp; &nbsp;  &nbsp; <input type="checkbox" /></label>
        </th>
    </tr>
    </tbody>
</table>
</body>
</html>';


    $mpdf->WriteHTML($html);
    $mpdf->Output();
    exit;

上述代码生成以下 PDF 文件(屏幕截图)

enter image description here

它应该如下所示

enter image description here

我的问题是,有没有办法像生成 HTML 一样生成 PDF,我的意思是有没有办法打印 html+PHP 在客户端生成的任何内容?

有什么想法吗?

最佳答案

HTML2PDF 转换器可能因该布局而不堪重负。您可能应该考虑具有更强大的布局选项的 PDF 生成器。 缺点是,您必须用另一种布局语言定义布局,但这将保证令人满意的输出。 您是否考虑过使用 XSL/FO使用合适的生成器,如 PDFnow

关于PHP,从 HTML 生成 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25167472/

相关文章:

php - 为什么 mime_content_type 上的一些 mp3 返回 application/octet-stream

php - 如何在 Laravel 5.4.18 中使用特征?

php - 使用 TCPDF 签署 pdf 的问题

c# - 无法使用 RestSharp 下载 pdf?

php - 使用 TCPDF 创建大型 PDF 的最有效方法是什么?

php - 根据另一个查找/映射数组替换数组中的键

javascript - 下载 PDF base64 - Internet Explorer - 不要求保存

Php 报告 - 制作 PDF 的最佳方式

php - 带有特殊字符的 TCPDF HTML 显示空 PDF 文件