简体   繁体   English

Dompdf不使用张贴的php表格打印表格

[英]Dompdf is not printing the table with posted php form

When I try to print the table in the file_html.php it prints the static table with some error(that's a different question). 当我尝试在file_html.php中打印表格时,它会打印出带有一些错误的静态表格(这是一个不同的问题)。 However, when the php tags for the posted data are included in the file with the post variables, the pdf generated displays nothing at all except for the anchor tag which is at the bottom of the page. 但是,当发布数据的php标记包含在具有post变量的文件中时,生成的pdf除了页面底部的定位标记之外,什么都没有显示。

Here is the index.php :- 这是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');

And here is the file_html.php which consists the form with the post variable. 这是file_html.php,其中包含带有post变量的表单。 Note : when the php with the post is removed the table prints out in the pdf. 注意:删除带有帖子的php时,该表将在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>

You're loading file_html.php using $dompdf->load_html_file('http://localhost/pdf/file_html.php'); 您正在使用$dompdf->load_html_file('http://localhost/pdf/file_html.php');加载file_html.php $dompdf->load_html_file('http://localhost/pdf/file_html.php'); . This method is like starting a new browser request using the GET method. 此方法类似于使用GET方法启动新的浏览器请求。 Any variables set during the current PHP process are not passed through. 当前PHP过程中设置的任何变量都不会通过。 This means the $_POST array is empty. 这意味着$_POST数组为空。

There are a few ways of addressing the issue, but since your table relies on data from the $_POST array I'd suggest using output buffering. 有几种方法可以解决此问题,但是由于您的表依赖于$_POST数组中的数据,因此建议您使用输出缓冲。 You can render the content within index.php, capture the output, and feed it to dompdf. 您可以在index.php中呈现内容,捕获输出,并将其提供给dompdf。

Using your original sample as a starting point ... 以原始样本为起点...

<?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');
?>

Now when you POST to index.php the $_POST array will be available to file_html.php. 现在,当您发布到index.php时,$ _POST数组将可用于file_html.php。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM