【问题标题】:Using DomPDF to generate table list from database使用 DomPDF 从数据库生成表列表
【发布时间】:2016-11-29 15:56:50
【问题描述】:

我有以下代码用于使用 DomPDF 将 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='&lt;!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>

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

$pdf_content='

并让它发挥作用。

$pdf_content=include('page.pp'); //这样的东西似乎不起作用

我欢迎任何关于相同的替代想法

【问题讨论】:

    标签: php html pdf-generation dompdf


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-23
      • 2011-03-18
      • 2011-08-17
      • 2011-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多