dtll2016 2016-05-30 06:24
浏览 86

Dompdf没有使用发布的php表单打印表格

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>
  • 写回答

1条回答 默认 最新

  • doubanduo7620 2016-05-31 14:00
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)