使用 DomPDF 从数据库生成 table 列表

Using DomPDF to generate table list from database

我有以下代码使用 DomPDF.and 将 HTML 内容生成为 PDF 我的问题集中在这一行

$pdf_content='<!DOCTYPE html PUBLIC "-//W3C//DTD...

整个代码

<?php
error_reporting(0);
require_once ('dompdf_config.inc.php');
$pdf_content='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            </head>

            <style type="text/css">                         
                #pdf_header, #pdf_container{ border: 1px solid #CCCCCC; padding:10px; }             
                #pdf_header{ margin:10px auto 0px; border-bottom:none; }                
                table{ width:580px; }               
                #pdf_container{margin:0px auto; }
                .rpt_title{ background:#99CCFF; }                                                           
            </style>

            <body>
            <div id="pdf_header" >
            <table border="0" cellspacing="1" cellpadding="2">
            <tr id="hdRow">
                <td width="20%"><img src="space_age_header.jpg" style="width:250px" ></td>              
                <td width="30%" align="center">Sample File</td>
                <td width="30%" align="left">Marimuthu<br>User Code : 179865420</td>
            </tr>
            </table>
            </div>
            <div id="pdf_container" >
            <table border="0" cellspacing="1" cellpadding="2">
            <tr align="center" bgcolor="pink" style="color:#FFF"><td colspan="3"><b>Your Statement Summery</b></td> </tr>
            <tr bgcolor="#006" style="color:#FFF"><td colspan="3" align="left">Your Heading.</td></tr>
            </table>
            <table> <tr> <td> Name </td><td> Department</td><td>Total </td><td>Grade </td> </tr>
            <tr> <td> Marimuthu </td><td> Admin</td><td>250 </td><td>A </td> </tr>          
            </div></body></html>'
            ;
            $name = date("Ymd").rand().'.pdf';
            $reportPDF=createPDF(12, $pdf_content, 'activity_Report', $name );
    function createPDF($pdf_userid, $pdf_content, $pdf_For, $filename){

    $path='UsersActivityReports/';
    /*$rndNumber=rand();
    $filename=$pdf_userid.date("Ymd").$rndNumber.'.pdf';*/
    $dompdf=new DOMPDF();
    $dompdf->load_html($pdf_content);
    $dompdf->render();
    $output = $dompdf->output();
    file_put_contents($path.$filename, $output);
    return $filename;       
    }   
    echo '<a href="UsersActivityReports/'.$name.'" > Download </a>';
?>

我已将 $pdf_content='<!DOCTYPE html PUBLIC "-//W3C//DTD... 部分提取到另一个页面 page.php 并修改它以允许使用 do while 循环从数据库生成文件。

<?php 

include("../../Connections/dbConfig.php"); 

$query_record = mysqli_query($link,"SELECT * FROM `subjects`") or 
die (mysqli_error($link));
$row_record = mysqli_fetch_assoc($query_record);
$totalRows_record = mysqli_num_rows($query_record);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            </head>

            <style type="text/css">                         
                #pdf_header, #pdf_container{ border: 1px solid #CCCCCC; padding:10px; }             
                #pdf_header{ margin:10px auto 0px; border-bottom:none; }                
                table{ width:580px; }               
                #pdf_container{margin:0px auto; }
                .rpt_title{ background:#99CCFF; }                                                           
            </style>

            <body>
            <div id="pdf_header" >



            <table border="0" cellspacing="1" cellpadding="2">
            <tr id="hdRow">
                <td width="20%"><img src="space_age_header.jpg" style="width:250px" ></td>              
                <td width="30%" align="center">Sample File</td>
                <td width="30%" align="left">Marimuthu<br>User Code : 179865420</td>
            </tr>
            </table>
            </div>
            <div id="pdf_container" >


            <table border="0" cellspacing="1" cellpadding="2">
            <tr align="center" bgcolor="pink" style="color:#FFF"><td colspan="3"><b>Your Statement Summery</b></td> </tr>
            <tr bgcolor="#006" style="color:#FFF"><td colspan="3" align="left">Your Heading.</td></tr>
            </table>
            <table> 
            <tr> <td> Subject </td><td> Code </td><td>Total </td><td>Grade </td> </tr>
            <?php do { ?>
            <tr> <td> <?php echo $row_record['subject']; ?> </td><td>  <?php echo $row_record['code']; ?> </td><td>250 </td><td>A </td> </tr>       

             <?php } while ($row_record = mysqli_fetch_assoc($query_record)); ?>    
             </table>
            </div></body></html>

现在这就是我卡住的地方。我该如何实现。我如何 return 将动态生成的代码返回到

$pdf_content='

并让它发挥作用。

$pdf_content= include('page.pp'); //something like this doesn't seem to work

我欢迎任何其他想法

关于代码更改的主要注意事项是 HTML 现在位于代码块之外。这意味着 PHP 会在处理该代码时将该代码发送到浏览器。要解决此问题,您需要启用输出缓冲以捕获呈现的文本。

ob_start();
include('page.php');
$pdf_content = ob_get_clean();
ob_end_clean();

请注意,某些系统可能会限制输出缓冲区的大小(请参阅 output_buffering 设置)。您可以使用 ini_get('output_buffering'); 进行检查。如果启用限制,请确保渲染的大小 HTML 小于缓冲区的最大大小。