0

我正在使用 dompdf 从 php 文件生成 PDF。这是文件的完整代码:

<?php 

// INCLUDEM CONFIGUL SI INITIALIZAM CLASELE DE SISTEM
require_once 'config.php';
require_once 'initialize.php';
require_once("/dompdf/dompdf_config.inc.php");

// Database
// Conectarea LA BAZA DE DATE !!!
$db = new MySQLDatabase();

// Session
// SESIUNEA CARE TINE UTILIZATORUL LOGAT !
$session=new Session();      
?>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="main.css" media="all" rel="stylesheet" type="text/css">
    <link href='http://fonts.googleapis.com/css?family=Allura' rel='stylesheet' type='text/css'>
    <script type="text/javascript" src="<?php echo HTTP_FRONT; ?>js/jquery-1.8.3.js"></script>
    <title>Sanovita</title>
</head>
<body>
    <script>
    $(document).ready(function(){
        $('.AdaugaCategoriile p#1').hide();
        $('.AdaugaCategoriile p#2').hide();
        $('.AdaugaCategoriile p#3').hide();
        $('.AdaugaCategoriile p#4').hide();

        $('#categorie').change(function(){

            var categorie = $(this).val();

            $('.AdaugaCategoriile p#1').hide();
            $('.AdaugaCategoriile p#2').hide();
            $('.AdaugaCategoriile p#3').hide();
            $('.AdaugaCategoriile p#4').hide();

            $('.AdaugaCategoriile p#'+categorie).show();
        });

    });
    </script>
    <div id="Body">
        <div id="Header">
            <div id="HeaderLogo">
                <a href="index.php"><img src="images/logo.png"></a>
            </div>
            <div id="Menu">
                <div class="clear"></div>
            </div>
        </div>
        <div id="Content">
            <div class="Categorii">  
                <?php 

                // daca nu exista id-ul produsului
                if(isset($_GET['id']))
                {
                    $res = $db->query("SELECT * FROM `vanzari` WHERE `id_client`=".$_GET['id']." AND `finalizat`=0");   

                    $categoriile = '<table>
                            <th width="2%">Nr.</th>
                            <th width="15%">Nume Client</th>
                            <th width="15%">Nume Angajat</th>
                            <th width="15%">Produs</th>
                            <th>Data</th>
                            <th>Cantitate</th>
                            <th width="10%">Total pret</th>
                            <th width="15%">Stare</th>
                            <th>Actiuni</th>';

                    $x = 1;
                    // AFISEZ PRODUSELE
                    for($i=0; $i<$res->num_rows; $i++)
                    {
                        $client = $db->query("SELECT * FROM `clienti` WHERE `id`=".$res->rows[$i]['id_client']."");
                        $produs_vanzare = $db->query("SELECT * FROM `produse` WHERE `id`=".$res->rows[$i]['id_produs']."");
                        $angajat = $db->query("SELECT * FROM `utilizatori` WHERE `id`=".$res->rows[$i]['id_angajat']."");

                        if($res->rows[$i]['finalizat'] == 0)
                        {
                            $status = 'In curs de finalizare';
                        }
                        else
                        {
                            $status = 'Finalizat';
                        }

                        // daca produsul e par
                        if(($i%2)==0)
                        {
                            $categoriile .= "
                            <tr class='odd'>
                                <td><p>".$x."</p></td>
                                <td><p>".$client->row['nume_client']."</p></td>
                                <td><p>".$angajat->row['firstname']. ' ' . $angajat->row['lastname']."</p></td>
                                <td><p>".$produs_vanzare->row['name']."</p></td>
                                <td><p>".$res->rows[$i]['data']."</p></td>
                                <td><p>".$res->rows[$i]['bucati']."</p></td>
                                <td><p>".$res->rows[$i]['total_pret']." RON</p></td>
                                <td><p>".$status."</p></td>
                                <td><a href='".HTTP_FRONT."vanzari.php?id_vanzare=".$res->rows[$i]['id']."&id=".$client->row['id']."'><p>Sterge</p></a></td>     
                            </tr>";
                        }
                        else
                        {
                            $categoriile .= "
                            <tr class='even'>
                                <td><p>".$x."</p></td>
                                <td><p>".$client->row['nume']."</p></td>
                                <td><p>".$angajat->row['firstname']. ' ' . $angajat->row['lastname']."</p></td>
                                <td><p>".$produs_vanzare->row['name']."</p></td>
                                <td><p>".$res->rows[$i]['data']."</p></td>
                                <td><p>".$res->rows[$i]['bucati']."</p></td>
                                <td><p>".$res->rows[$i]['total_pret']." RON</p></td>
                                <td><p>".$status."</p></td>
                                <td><a href='".HTTP_FRONT."vanzari.php?id_vanzare=".$res->rows[$i]['id']."&id=".$client->row['id']."'><p>Sterge</p></a></td>  
                            </tr>";
                        }

                        $x++;
                    }                        
                    $categoriile .= "</table><div class='clear'></div>";

                    if(!isset($session->data['finalizat']))
                    {
                        echo $categoriile;
                    }
                }

                ?>
            </div>

        </div>
        <div class="clear"></div>
        <div id="Footer">
            <p>Copyright &copy; 2013</p>
        </div>
    </div>
</body>
</html>

<?php
$html = ob_get_clean();
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("factura.pdf");

?>

dompdf 只生成了 HTML 部分,但没有显示 php 部分。在配置中,DOM_ENABLE_PHP 设置为 true。可能是什么问题?

4

2 回答 2

1

看起来您正在尝试捕获所有输出(至少,这是我认为从您的ob_get_clean();使用中找到的。但是您从不调用ob_start,因此您可能只是将所有数据推送到浏览器/输出,而一无所获那里。

只需在字符串中构建您的 html,然后将其用于输入。

尝试这个

<?php 
require_once 'config.php';
require_once 'initialize.php';
require_once("/dompdf/dompdf_config.inc.php");

$db = new MySQLDatabase();
$session=new Session();      

$html =  <<<HTML
<html>
<head>
</head>
<body>
<div>
<p>
test
</p>
</div>
</body>
</html>
HTML;

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("factura.pdf");

?>
于 2013-07-02T14:15:51.950 回答
0

您将所有 PHP 生成的输出存储到$categoriile. 然后您正在检查是否未设置$session->data['finalizat']...也许您不满足此条件。检查$session->data['finalizat']。您也没有 ob_start (根据 Nanne 的通知)

于 2013-07-02T14:15:32.493 回答