使用dompdf在php中打印pdf时出错


error print pdf in php with dompdf

我正在尝试使用dompdf创建按钮保存post.pdf,但结果是一个错误:

Fatal error: Uncaught exception 'DOMPDF_Exception' with message
'Requested HTML document contains no data.' in
E:'xampp'htdocs'forum_seo'pages'posts'dompdf'include'frame_tree.cls.php:114
Stack trace: #0
E:'xampp'htdocs'forum_seo'pages'posts'dompdf'include'dompdf.cls.php(533):
Frame_Tree->build_tree() #1
E:'xampp'htdocs'forum_seo'pages'posts'dompdf'include'dompdf.cls.php(696):
DOMPDF->_process_html() #2
E:'xampp'htdocs'forum_seo'pages'posts'post_download.php(43):
DOMPDF->render() #3 {main} thrown in
E:'xampp'htdocs'forum_seo'pages'posts'dompdf'include'frame_tree.cls.php
on line 114

文件post_download.php

<html>
<head>
<meta charset="utf8" />
<link rel="stylesheet" type="text/css" href="../../css/style1.css"/>
</head>
<body>
<?php
require_once '../connect.php';
$sql_head_post="SELECT *,seo_members.id as idp ,seo_articles.id as id_post,seo_articles.num_like as postlike,seo_members.num_like as userlike,DATE_FORMAT(dateattend,'%d-%m-%Y') as date_join,  DATE_FORMAT( date_post,  '%d-%m-%Y' ) AS _date, DATE_FORMAT( date_post,  '%h:%i %p' ) AS _time
                    FROM seo_articles
                    INNER JOIN seo_subcate ON seo_articles.id_cate = seo_subcate.id
                    INNER JOIN seo_members ON seo_articles.id_user = seo_members.id
                    WHERE seo_articles.id =$_GET[dl_post]
                    LIMIT 1";
    $q_post_det= mysqli_query($con,$sql_head_post);
    $r_post_det=mysqli_fetch_array($q_post_det); 
?>
<div class='title' style='display: block;'>
        <div style='float:left;width:1000px;text-overflow: ellipsis;word-wrap: break-word;' >
            <b class='title-post'><?php echo "$r_post_det[article]";?></b>
            <p>cate: <?php echo "$r_post_det[subcate], post by $r_post_det[username], $r_post_det[_date], $r_post_det[_time]";?></p> 
        </div>
    </div>
    <div class='detaild' >
        <hr style='clear:both; border:1px solid #555;border-style: dashed' />
        <p><?php echo "$r_post_det[content]";?></p>
    </div>
</body>
</html>
<?php
require_once("dompdf/dompdf_config.inc.php");
ob_start();
$content = ob_get_clean();
$dompdf = new DOMPDF();
$dompdf->load_html($content);
$dompdf->render();
$dompdf->stream("$r_post_det[article].pdf");
?>

如果上面显示的PHP页面是生成要转换为PDF的HTML的页面,那么您可能需要移动'ob_start();'脚本顶部的语句:

<?php
ob_start();
?>
<html>
<head>
...

当前在ob_start()$content = ob_get_clean();之间没有捕获输出,因此$content将始终为空。