簡體   English   中英

Dompdf不使用張貼的php表格打印表格

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

當我嘗試在file_html.php中打印表格時,它會打印出帶有一些錯誤的靜態表格(這是一個不同的問題)。 但是,當發布數據的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變量的表單。 注意:刪除帶有帖子的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>

您正在使用$dompdf->load_html_file('http://localhost/pdf/file_html.php');加載file_html.php $dompdf->load_html_file('http://localhost/pdf/file_html.php'); 此方法類似於使用GET方法啟動新的瀏覽器請求。 當前PHP過程中設置的任何變量都不會通過。 這意味着$_POST數組為空。

有幾種方法可以解決此問題,但是由於您的表依賴於$_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');
?>

現在,當您發布到index.php時,$ _POST數組將可用於file_html.php。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM