Search code examples
phpmysqlcodeigniterdompdf

Unable to stream pdf: headers already sent codeigniter


i had this error when i want to export my file to pdf

Unable to stream pdf: headers already sent

this is my function export_to_pdf inside the controller

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);
    }

and i have helper with name warsito_pdf_helper like this

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();
    }
}

and finally the view for the 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>

i have googling my problem before, mostly says this error occurs because i have an extra lines in my file i already try the solution from this link DOMPDF: Unable to stream pdf: headers already sent and add the following code after the stream();

$f;
$l;
if(headers_sent($f,$l))
{
    echo $f,'<br/>',$l,'<br/>';
    die('now detect line');
}

but the error still happens, so anyone can tell me whats wrong with my method or my code, thanks


Solution

  • In your controller change $object = $this->load->view('manage/pdf_report_all', $data); to $object = $this->load->view('manage/pdf_report_all', $data, TRUE);

    And also change your view to

    `

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