2017-10-10 318 views
1

我遇到了生成HTML/PHP代码为PDF的问题。使用MPDF 这里林是我的代码:当生成为PDF时MPDF错误

<table id="packing_slip"> 

<?php 

foreach($sheets as $s): 
    echo "<tr>"; 
    echo "<td>"; 
    $name= explode(' ',$s['customer_name']) 
    ?> 
    <div style="text-align: center"> 
    </div> 
    <h2 style="text-align: center">Hi <?php echo $name[0] ?>!</h2> 
    <div align="center"> 
     <br> Here is your delivery for: <br> 
     <?php echo date('l, F dS', strtotime($s['delivery_week']));?> 
     <br> 
     ORDER NO. <?php echo $s['order_id'] ?>&nbsp; | &nbsp; CUSTOMER NO.<?php echo $s['client_id'] ?> 
     <?php $query="select * from orders left join deliveries on orders.id = deliveries.order_id left join deliveries_meals on deliveries.id = deliveries_meals.delivery_id where orders.id = '{$s['order_id']}'"; 
     $result_main = mysqli_query($conn,$query); 
     $row_main = mysqli_fetch_assoc($result_main); 

     $query_meals = "select label, subtitle, meals.name from meal_options left join meals on meal_options.meal_id = meals.id where meal_options.id ='{$row_main['meal_option_id']}' "; 
     $result_meals = mysqli_query($conn,$query_meals); 
     $row_meals = mysqli_fetch_assoc($result_meals); 

     ?> 
     <hr style="height: 5px;border-top:1px solid #000 !important;margin-bottom:0px"> 
     <hr style="height: 5px;border-top:1px solid #000 !important;margin-top: 5px !important;"> 
    </div> 
    <?php foreach($s['meals'] as $m):?> 
    <h3><?php echo $row_meals['name'];?> </h3> 
    <h5><i><?php 
      if($row_meals['label'] != '' && $row_meals['subtitle'] != ''){ echo $row_meals['subtitle'] . " & " . $row_meals['label'];} 
      elseif ($row_meals['label'] != '') {echo "with " . $row_meals['label'];} 
      else{ echo $row_meals['subtitle'];}?></i> </h5> 
    <?php echo $m['count']?> &nbsp; SERVING SHIPPED<br><br> 
    COMPONENTS 
    <hr align = "left" style="width: 115px; border-top:1px solid #000 !important;margin-top: 5px !important;margin-bottom:0px"> 
    <br> 
    <?php if (@$m['components'][0] != ''){?> 
     <?php foreach($m['components'] as $c):?> 
      <?php echo $c?><br> 
     <?php endforeach; 
    } else {echo "Everything is included in package. <br>";} 
    ?> 
    <br> 
    <?php if($m['instructions'][0] != '' || $m['instructions'][1] !=''){?> 
     INSTRUCTIONS 
     <hr align = "left" style="width: 115px; border-top:1px solid #000 !important;margin-top: 5px !important;margin-bottom:0px"> 
     <br> 
     <?php if($m['instructions'][0] != ''){echo "Microwave : ".$m['instructions'][0];}?><br><br> 
     <?php if($m['instructions'][1] != ''){ echo "Stovetop : ".$m['instructions'][1];}?><br> 
    <?php }?> 
    <hr align="left" style="width: 400px; border-top:1px solid #000 !important;margin-top: 5px !important;margin-bottom:0px" > 
<?php endforeach;?> 
    <hr align="left" style="width: 600px; border-top:1px solid #000 !important;margin-top: 5px !important;margin-bottom:0px" > 
    <div align="center"><h3> Thank you,<?php echo $name[0] ?>!</h3> 
     <hr style="width: 600px; border-top:1px solid #000 !important;margin-top: 5px !important;margin-bottom:0px" > 
    </div> 
    <br> 
    <br> 
    <br> 
    <br> 
    <br> 
    </td> 
    </tr> 
<?php endforeach; ?> 

</table> 

<?php 

include('mpdf60/mpdf.php'); 

$mpdf=new mPDF('c','A4','','',32,25,27,25,16,13); 
$mpdf->debug = true; 

$mpdf->SetDisplayMode('fullpage'); 

$mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first level of a list 

$mpdf->WriteHTML('<p style="color:red;">Hello World<br/>Fisrt sentencee</p>'); 

$mpdf->Output(); 
exit; 
?> 

我把现在的“Hello World”,但事实上,我希望把我整个的代码,也是我得到这个错误: Output has already been sent from the script - PDF file generation aborted. 我该如何解决这个问题?我正在使用FuelPhp,我不知道如何将它添加到PDF,关于它如何工作的任何想法。因为现在它甚至不让我把“Hello World”

回答

0

你现在正在尝试做两件相互排斥的事情:在浏览器中显示HTML代码在浏览器中显示PDF文档。

显示PDF(不带参数调用Output方法)会尝试设置正确的HTTP Content-type标题,但不能这样做,因为已经有输出 - 如错误消息所述。

请确保您禁用了您的框架提供的所有布局,因为它会产生可能导致此错误的完全相同的输出。

您可以通过与ob_处理周围的HTML代码,并使用捕捉输出

<?php 

ob_start(); 

?> ... HTML code ... <?php 

$html = ob_get_clean(); 

... 

$mpdf->writeHtml($html); 

或通过生成的HTML代码为PHP字符串完全解决你的问题:

<?php 

$html = '<table id="packing_slip">'; 

foreach($sheets as $s): 
$html .= "<tr>"; 
$html .= "<td>"; 

参见https://mpdf.github.io/troubleshooting/error-messages.html

+0

我尝试了'ob_start();'的第一个选项,但是一个pdf下载,但是当我打开它时,我得到“无法加载PDF文档。 “你知道为什么会发生这种情况吗? – john

+0

也做'ob_start(); '必须在页面的开头? – john

+0

如果您发送PDF进行下载或内嵌以在浏览器中显示,您不必*自己打印任何内容,甚至在*调用'输出'方法之后*。所以是的,ob_start应该在你的代码的开头,并且在调用'Output'方法后不能有任何输出(print/echo/etc)。 – Finwe