无法传输pdf:标头已经发送了codeigniter

时间:2017-01-28 19:10:32

标签: php mysql codeigniter dompdf

当我想将文件导出为pdf

时出现此错误

无法传输已发送的pdf:标头

这是我在控制器中的函数export_to_pdf

function export_to_pdf() {
        $this->load->helper('warsito_pdf_helper');
        $this->load->model('gedung/gedung_model');
        $data['row'] = $this->gedung_model->laporan_perawatan_keseluruhan();
        $object = $this->load->view('manage/pdf_report_all', $data);
        $filename = 'Report.pdf';
        generate_pdf($object, $filename, true);
    }

我有这样的名字warsito_pdf_helper的帮手

function generate_pdf($object, $filename, $direct_download = true) {
    require_once 'dompdf/dompdf_config.inc.php';
    $dompdf = new DOMPDF();
    $dompdf->load_html($object);
    $dompdf->render();
    if($direct_download == true) {
        $dompdf->stream($filename);
    } else {
        return $dompdf->output();
    }
}

最后是pdf的视图

pdf_report_all.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">
        <!-- Favicons-->
        <link rel="apple-touch-icon-precomposed" href="<?php echo base_url();?>assets/home/assets/img/favicon/apple-touch-icon-152x152.png">
        <meta name="msapplication-TileColor" content="#FFFFFF">
        <meta name="msapplication-TileImage" content="<?php echo base_url();?>assets/home/assets/img/favicon/mstile-144x144.png">
        <link rel="icon" href="<?php echo base_url();?>assets/home/assets/img/favicon/favicon-32x32.png" sizes="32x32">
        <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
        <!-- Materialize core CSS -->
        <link href="<?php echo base_url();?>assets/home/materialize/css/materialize.css" rel="stylesheet" type="text/css">
        <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!--[if lt IE 9]>
            <script src="assets/js/html5shiv.js"></script>
            <script src="assets/js/respond.min.js"></script>
        <![endif]-->
        <link href="<?php echo base_url();?>assets/home/template.css" rel="stylesheet" type="text/css">
    </head>
<body>
    <center><b><h5>Laporan Pembayaran Keseluruhan</h5></b></center>
    <!--
    <div class="container">
        <div class="row">
        <div class="col s12 m12"> -->
            <table class="bordered" align="center">
            <tr>
                <th>No</th>
                <th>No Ref</th>
                <th>Jenis Perawatan</th>
                <th>Tanggal Pembayaran Tagihan</th>
                <th>Jumlah Pembayaran</th>
            </tr>
            <?php foreach($row as $row): $no = 1; $total_pembayaran = 0;?>
                <tr>
                    <td><?php echo $no++?></td>
                    <td><?php echo $row['NO_ID']?></td>
                    <td><?php echo $row['NAMA_PERAWATAN']?></td>
                    <td><?php echo $row['TANGGAL_PEMBAYARAN']?></td>
                    <td><?php echo "Rp. ".number_format($row['BIAYA'])?></td>
                </tr>
                <?php $total_pembayaran = $row['BIAYA'] + $total_pembayaran?>
            <?php endforeach;?>
            </table>
            <table style="display: inline-block;">
                <tr>
                    <td><b>Total Pembayaran Keseluruhan: </b></td>
                    <td><b><?php echo "Rp. ".number_format($total_pembayaran)?></b></td>
                </tr>
            </table>
        <!--
        </div>
        </div>
    </div> -->
    <script src="<?php echo base_url();?>assets/home/assets/js/jquery.min.js"></script>
    <script src="<?php echo base_url();?>assets/home/materialize/js/materialize.js"></script>
    <script src="<?php echo base_url();?>assets/home/index.js"></script>
</body>
</html>

之前我在google搜索我的问题,大多数情况下说这个错误发生是因为我的文件中有额外的行我已经尝试过此链接中的解决方案DOMPDF: Unable to stream pdf: headers already sent 并在stream();

之后添加以下代码
$f;
$l;
if(headers_sent($f,$l))
{
    echo $f,'<br/>',$l,'<br/>';
    die('now detect line');
}

但错误仍然存​​在,所以任何人都可以告诉我我的方法或代码有什么问题,谢谢

1 个答案:

答案 0 :(得分:2)

在您的控制器中将$object = $this->load->view('manage/pdf_report_all', $data); 更改为$object = $this->load->view('manage/pdf_report_all', $data, TRUE);

并将您的观点更改为

`

<center><b><h5>Laporan Pembayaran Keseluruhan</h5></b></center>

            <table class="bordered" align="center">
            <tr>
                <th>No</th>
                <th>No Ref</th>
                <th>Jenis Perawatan</th>
                <th>Tanggal Pembayaran Tagihan</th>
                <th>Jumlah Pembayaran</th>
            </tr>
            <?php foreach($row as $row): $no = 1; $total_pembayaran = 0;?>
                <tr>
                    <td><?php echo $no++?></td>
                    <td><?php echo $row['NO_ID']?></td>
                    <td><?php echo $row['NAMA_PERAWATAN']?></td>
                    <td><?php echo $row['TANGGAL_PEMBAYARAN']?></td>
                    <td><?php echo "Rp. ".number_format($row['BIAYA'])?></td>
                </tr>
                <?php $total_pembayaran = $row['BIAYA'] + $total_pembayaran?>
            <?php endforeach;?>
            </table>
            <table style="display: inline-block;">
                <tr>
                    <td><b>Total Pembayaran Keseluruhan: </b></td>
                    <td><b><?php echo "Rp. ".number_format($total_pembayaran)?></b></td>
                </tr>
        </table>