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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么