dpa55065 2016-07-26 06:27
浏览 45
已采纳

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

I have the following code for generating HTML content to PDF using DomPDF.and my problem focuses on this line

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

The entire code

<?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>';
?>

I have extracted the $pdf_content='<!DOCTYPE html PUBLIC "-//W3C//DTD... part to another page page.php and modified it to allow generating files from database using a do while loop.

<?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>

Now this is where i am stuck. How do i implement this. How do i return the dynamically generated code back to the

$pdf_content='

and make it work.

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

I welcome any alternative idea on the same

  • 写回答

1条回答 默认 最新

  • doutan8506 2016-07-26 17:39
    关注

    The main thing to note about your code changes is that the HTML is now outside of a code block. This means that PHP will send that code to the browser as it's processed. To get around this you would need to enable output buffering to capture the rendered text.

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

    Note that some systems may limit the size of the output buffer (see the output_buffering setting). You can check that using ini_get('output_buffering');. If a limit is enabled make sure the size of your rendered HTML is less than the maximum size of the buffer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大