dompdf呈现空文件或不呈现数据库行

时间:2019-06-12 16:19:40

标签: php dompdf

所以,如果我只是这样渲染文件:

<?php

// Include autoloader 
require_once '../dompdf/autoload.inc.php'; 

// Reference the Dompdf namespace 
use Dompdf\Dompdf; 

// Instantiate and use the dompdf class 
$dompdf = new Dompdf();

// Load content from html file 
$html = file_get_contents("../orderconfirmation.php"); 
$dompdf->loadHtml($html); 

// (Optional) Setup the paper size and orientation 
$dompdf->setPaper('A4', 'landscape'); 

// Render the HTML as PDF 
$dompdf->render(); 

// Output the generated PDF (1 = download and 0 = preview) 
$dompdf->stream("codexworld", array("Attachment" => 0));

但是这给了我一个pdf文件,没有来自数据库的数据,因此我尝试按照许多不同地方的建议将文件转换为HTML文件。

我做了以下事情:

<?php

ob_start();
include("orderconfirmation.php");
$php_to_html = ob_get_clean();
$html_encoded = htmlentities($php_to_html);  
echo $html_encoded;
?>

但这给我一个空文件。有什么建议吗?

顺便说一句,这是我的orderconfirmation.php文件的代码:

<?php
session_start();
include 'includes/restrictions.inc.php';
logged_in();
$title = 'Order confirmation';
include_once 'includes/header.php';
include_once 'includes/dbh.inc.php';
?>

<div id="" class="section orderconsec content">
<div class="container">
    <div class="pb-4">
        <h3>Thank you for your order</h3>
    </div>

    <div class="border-new border border-dark rounded p-3">
        <div class="pt-2 pr-4 pl-4">
            <b><h3>Order number</h3></b>
            <h3><?php
                    $order_id = $_SESSION["order_id"];
                    $ordernosql = "SELECT * FROM invoice WHERE order_id = ?";
                    $stmt = mysqli_stmt_init($conn);

                    if (!mysqli_stmt_prepare($stmt, $ordernosql)) {
                        echo "SQL statement failed";
                    }
                    else
                    {
                        mysqli_stmt_bind_param($stmt, "s", $order_id);
                        mysqli_stmt_execute($stmt);
                        $result = mysqli_stmt_get_result($stmt);

                        if($row = mysqli_fetch_assoc($result))
                        {
                            echo $row['order_id'];

                ?></h3>
                <form method="post" action="includes/printpdf.inc.php">
                    <button type="submit" name="print-vouchers" class="btn btn-danger mt-3">Print</button>
                </form>
        </div>
        <hr>
        <div>
            <p>We are currently processing your order and we will email you with confirmation shortly.</p>
            <p>For your convenience you may want to save your order confirmation.</p>
        </div>
        <div class="pt-5 pr-4 pl-4">
            <b><h3>Delivery Details</h3></b>
        </div>
        <hr>
        <div style="padding: 10px;">
            <p><b>Delivery for</b></p>
            <p>
            <?php
                echo $row['uidUsers'];
            ?>
            </p>
        </div>
        <div style="padding: 10px;">
            <p><b>Address</b></p>
            <p>
            <?php
                echo $row['user_address'];
            ?>
            </p>
        </div>
        <div style="padding: 10px;">
            <p><b>Delivery method</b></p>
            <p>
            <?php
                echo $row['delivery_method'];
            ?>
            </p>
        </div>
        <div class="pt-5 pr-4 pl-4">
            <b><h3>Order summary</h3></b>
        </div>
        <hr>
        <div style="padding: 10px;">
            <p>
            <?php
                $output = str_replace(',', '<br />', $row['order_summary']);
                echo $output;
            ?>
            </p>
        </div>
        <div class="pt-5 pr-4 pl-4">
            <h3>Payment information</h3>
        </div>
        <hr>
        <div style="padding: 10px;">
            <p><b>Payment type</b></p>
            <p>
            <?php
                echo $row['payment_type'];
            ?>
            </p>
        </div>
        <div style="padding: 10px;">
            <p><b>Billing address</b></p>
            <p>
            <?php
                echo $row['billing_address'];
            }
        }
        ?>
        </p>
        </div>
    </div>
</div>
</div>

<?php
    include_once 'includes/footer.php';
?>

1 个答案:

答案 0 :(得分:0)

您正在通过file_get_contents将PHP文件的内容加载到变量中,然后将该变量传递给Dompdf:

$html = file_get_contents("../orderconfirmation.php"); 
$dompdf->loadHtml($html); 

这行不通。 Dompdf不会在渲染之前解析PHP,这是您需要自己做的事情。试试:

ob_start();
require("../orderconfirmation.php");
$html = ob_get_clean();
ob_end_clearn();
$dompdf->loadHtml($html);