生成PDF时出现MPDF错误

时间:2017-10-10 12:09:04

标签: mpdf fuelphp

我在生成HTML / PHP代码到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;
?>

我现在就把#34; Hello World&#34;但我实际上想把我的整个代码,我也得到这个错误: Output has already been sent from the script - PDF file generation aborted. 我怎样才能解决这个问题?我正在使用FuelPhp,我不知道如何将其添加到PDF中,以及它如何工作的任何想法。因为现在它甚至不让我把#Hello;&#34;

1 个答案:

答案 0 :(得分: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

相关问题