Dompdf 没有打印 table 和发布的 php 表单

Dompdf is not printing the table with posted php form

当我尝试在 file_html.php 中打印 table 时,它会打印静态 table 并出现一些错误(这是一个不同的问题)。但是,当 posted 数据的 php 标签包含在带有 post 变量的文件中时,生成的 pdf 除了底部的锚标签外什么都不显示页面的。

这是 index.php :-

            require_once("dompdf/dompdf_config.inc.php");
            require_once("dompdf/dompdf_config.custom.inc.php");
            spl_autoload_register('DOMPDF_autoload');

            function pdf_create($html, $filename, $paper, $orientation, $stream=TRUE)
                {
                    $dompdf = new DOMPDF();
                    $dompdf->set_paper($paper,$orientation);
                    $dompdf->load_html_file('http://localhost/pdf/file_html.php');
                    $dompdf->render();
                    $dompdf->stream($filename.".pdf");
                }
                $filename = 'billofsale';
                $dompdf = new DOMPDF();
                $html = file_get_contents('http://localhost/pdf/file_html.php'); 
                pdf_create($html,$filename,'A4','portrait');

这里是 file_html.php,它由带有 post 变量的表单组成。注意:当删除带有 post 的 php 时,table 会在 pdf 中打印出来。

 <!DOCTYPE html>
            <html>

            <head>
            <style type="text/css">
            table, caption, tbody, tfoot, thead, tr, th, td {
              margin: 0;
              padding: 0;
              border: 0;
              font-size: 100%;
              font: inherit;

            }
            table {
              border-collapse: collapse;
              border-spacing: 0;
              width:100%;
            }
            body{
                font: normal medium/1.4 sans-serif;
            }

            th{
                text-align: center;
                border: 3px solid #ccd;
            }
            td{
                padding: 0.25rem;
                text-align: left;
                border: 2px solid #ccc;
            }

            tbody tr:nth-child(odd){
                background: #eee;
            }
            tbody:before, thead:after { display: none; }


            </style>
            </head>

            <body>

            <?php 
                if(isset($_POST['submit'])){
                ?>
            <p>
            <table>
                <thead><th colspan="2">Purchaser's Information</th></thead>
                <tbody>
                <tr>
                    <td colspan="2">Purchaser's Name :  <?php echo $_POST['pname']; ?></td>                 
                    </tr>
                    <tr>
                        <td colspan="2">Purchaser's Address : <?php echo $_POST['padd']; ?></td>
                    </tr>
                    <tr>
                        <td>City/Town : <?php echo $_POST['pcity']; ?> </td>
                        <td>Province : <?php echo $_POST['ppro']; ?></td>
                    </tr>

                    <tr>
                        <td>Postal Code :<?php echo $_POST['ppcode']; ?></td>
                        <td>Home Tel No :<?php echo $_POST['ptelno']; ?></td>
                    </tr>

                    <tr>
                        <td>Business Tel :<?php echo $_POST['pbtel']; ?></td>
                        <td>Email :<?php echo $_POST['pemail']; ?></td>
                    </tr>

                    <tr>
                        <td>Driver License : <?php echo $_POST['pdriverlic']; ?></td>
                        <td>Expiry Date :<?php echo $_POST['pdriverexp']; ?></td>
                    </tr>
                    </tbody>
            </table>
            </p>    
            <?php
                }   
            ?>
            <a href="index.php">Print </a>
            </body>

            </html>

您正在使用 $dompdf->load_html_file('http://localhost/pdf/file_html.php'); 加载 file_html.php。该方法类似于使用 GET 方法启动新的浏览器请求。在当前 PHP 过程中设置的任何变量都不会通过。这意味着 $_POST 数组为空。

有几种方法可以解决这个问题,但由于您的 table 依赖于 $_POST 数组中的数据,我建议使用输出缓冲。您可以在 index.php 中呈现内容,捕获输出并将其提供给 dompdf。

以您的原始样本为起点...

<?php
ob_start();
require 'file_html.php'
$html = ob_get_contents();
ob_end_clean();
require_once('dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF();
$dompdf->set_paper('a4','portrait');
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream('billofsale.pdf');
?>

现在,当您 POST 到 index.php 时,$_POST 数组将可用于 file_html.php。