【问题标题】:PHP, generate PDF file from HTMLPHP,从 HTML 生成 PDF 文件
【发布时间】:2014-09-29 19:21:55
【问题描述】:

我是生成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文件(截图)

应该如下所示

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

有什么想法吗?

【问题讨论】:

  • 是的,mpdf 非常强大,虽然有时很烦人。我最近使用了 5.7.2。我建议您开始使用 css 来定位这些元素或重组您的表格以在单独的单元格中包含复选框。它会让你的生活更轻松。
  • 不完全是答案,但如果目标是使用来自 Web 表单的动态数据填充/写入 PDF,您可以拥有一个原始 PDF(具有通用元素/布局),然后用 PHP 更新它(使用 fpdf)生成自定义文档。

标签: php pdf tcpdf mpdf


【解决方案1】:

HTML2PDF 转换器可能会因该布局而过度紧张。您可能应该考虑具有更强大布局选项的 PDF 生成器。 不利的一面是,您必须用另一种布局语言定义布局,但这将保证令人满意的输出。 您是否考虑过将XSL/FOPDFnow 等合适的生成器一起使用?

【讨论】:

    【解决方案2】:

    是的,您可以这样做。这是完整的代码。 它完美地工作。 您也可以使用 post 方法从另一个页面获取价值。您的选择。

    <?php $student_id = $_GET['student_id']; ?>
    <?php
    include("mpdf/mpdf.php");
    $html .= "
    <html>
    <head>
    <style>
    body {font-family: sans-serif;
        font-size: 10pt;
        background-image: url(\"images/ok.jpg\");
    
        background-repeat: no-repeat;
        padding-top:10pt;
        margin-top: 100px;
        padding-top: 50px;
    }
    td { vertical-align: top; 
        border-left: 0.6mm solid #000000;
        border-right: 0.6mm solid #000000;
        align: center;
    }
    
    p.student_id{
        padding-left : 140px;
        padding-top  : -27px;
    } 
    
    </style>
    </head>
    <body>
    <!--mpdf                                                                          
    
    <p class=\"student_id\">$student_id</p>
    
    
    <sethtmlpageheader name='myheader' value='on' show-this-page='1' />
    <sethtmlpagefooter name='myfooter' value='on' />
    mpdf-->
    
    
    </body>
    </html>
    ";
    
    $mpdf=new mPDF();
    $mpdf->WriteHTML($html);
    $mpdf->SetDisplayMode('fullpage');
    
    $mpdf->Output();
    ?>
    

    【讨论】:

      猜你喜欢
      • 2017-08-04
      • 2011-12-31
      • 2013-07-04
      • 2017-12-12
      • 2010-12-11
      • 2016-12-24
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多