简体   繁体   中英

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). 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.

Here is the 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. Note : when the php with the post is removed the table prints out in the 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'); . This method is like starting a new browser request using the GET method. Any variables set during the current PHP process are not passed through. This means the $_POST array is empty.

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. You can render the content within index.php, capture the output, and feed it to 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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