使用 dom pdf 打开时生成的 pdf 已损坏

问题描述 投票:0回答:1

我正在从数据模态传递后变量。我使用的查询已经过检查并且是正确的。我对如何定义变量感到困惑

$html
这是我的代码

<?php
require_once __DIR__ . "/../functions.php";
require_once __DIR__ . "/../dompdf/autoload.inc.php";

use Dompdf\Dompdf;

if (isset($_POST['exprt'])) {

    $html = '<!DOCTYPE html>
<html>
<center>
        <div class="icon-centered">
            <img style="object-fit: contain; margin-left: 40px;"
                src="' . $init->base_url('assets/img/profil_rs/' . $klinik->foto_rs) . '" alt="Logo" width="13%">
        </div>
        <h1>Sensus Harian Rawat Inap <br /> RSPAD Gatot Soebroto</h1>
    </center>

    <!-- Table -->
    <div class="table-responsive datatable-custom">
        <table border="1">
            <thead>';
    foreach ($arrDisplay as $date => $pasien):
        $html .= ' <tr>
                        <td colspan="12" align="center">
                            ' . print($date) . '
                        </td>
                    </tr>
                    <tr>
                        <th>No</th>
                        <th>Nomor Registrasi</th>
                        <th>Nomor Rekam Medis</th>
                        <th>Nama Pasien</th>
                        <th>Umur</th>
                        <th>Poliklinik</th>
                        <th>Dokter</th>
                        <th>Ruang Perawatan</th>
                        <th>Tanggal Masuk</th>
                        <th>Tanggal Keluar</th>
                        <th>Lama Dirawat</th>
                        <th>Cara Keluar</th>
                    </tr>
                </thead>

                <tbody>';
        $no = 1;
        foreach ($pasien as $s):
            $datemasuk = new DateTime($s->tglmasuk);
            $datekeluar = new DateTime($s->tglkeluar);

            $age = $init->findage($s->tglahirP);
            $lamarawat = $init->lamarawat($s->tglmasuk, $s->tglkeluar);

            $html .= '
                        <tr role="row" class="even">
                            <td>
                                ' . $no . '
                            </td>
                            <td>
                                ' . $s->no_register . '
                            </td>
                            <td>
                                ' . $s->norm . '
                            </td>
                            <td>
                                ' . $s->nmpasien . '
                            </td>
                            <td>
                                ' . $age . '
                            </td>
                            <td>                              ' . $s->nmpoli . '             </td>
<td>
                                ' . $s->nmdokter . '
                            </td>
                            <td>
                                ' . $s->nmruang . '
                            </td>
                            <td>
                                ' . $datemasuk->format('d/m/Y H:i:s') . '
                            </td>
                            <td>
                                ' . $datekeluar->format('d/m/Y H:i:s') . '
                            </td>
                            <td>
                                ' . "$lamarawat hari" . '
                            </td>
     
<td>
 ' . $s->nmcrkeluar . '
                            </td>
                        </tr>';
            $no++;
        endforeach;
        $html .= '</tbody>';

    endforeach;

    $html .= '</table>
    </div>
    <!-- End Table -->
</body>
</html>';
    $dompdf = new Dompdf();
    $dompdf->loadHtml($html);
    $savein = 'download/Document/';
    $dompdf->setPaper('A4', 'Landscape');
    $dompdf->render();
    // Download pdf
    $dompdf->stream($filename, ["Attachment" => 0]);
}
?>

我尝试过

ob_start()
,但没用

我的代码有问题吗?

php mysql file pdf dompdf
1个回答
0
投票

你的语法有很多错误,你的开始标签在循环之外,但它们的结束标签在循环内。

不漂亮,但试试这个:

<?php
require_once __DIR__ . "/../functions.php";
require_once __DIR__ . "/../dompdf/autoload.inc.php";

use Dompdf\Dompdf;

if (isset($_POST['exprt'])) {

    $html = '
        <!DOCTYPE html>
        <html>
        <center>
                <div class="icon-centered">
                    <img style="object-fit: contain; margin-left: 40px;"
                        src="' . $init->base_url('assets/img/profil_rs/' . $klinik->foto_rs) . '" alt="Logo" width="13%">
                </div>
                <h1>Sensus Harian Rawat Inap <br /> RSPAD Gatot Soebroto</h1>
            </center>
        
            <!-- Table -->
            <div class="table-responsive datatable-custom">
                <table border="1">';

    if (!empty($arrDisplay) && is_array($arrDisplay))
        foreach ($arrDisplay as $date => $pasien):
            $html .= ' 
            <thead>
                    <tr>
                        <td colspan="12" align="center">
                            ' . print($date) . '
                        </td>
                    </tr>
                    <tr>
                        <th>No</th>
                        <th>Nomor Registrasi</th>
                        <th>Nomor Rekam Medis</th>
                        <th>Nama Pasien</th>
                        <th>Umur</th>
                        <th>Poliklinik</th>
                        <th>Dokter</th>
                        <th>Ruang Perawatan</th>
                        <th>Tanggal Masuk</th>
                        <th>Tanggal Keluar</th>
                        <th>Lama Dirawat</th>
                        <th>Cara Keluar</th>
                    </tr>
                </thead>

                <tbody>
                ';
            $no = 1;
            if (!empty($pasien) && is_array($pasien))
                foreach ($pasien as $s):
                    $datemasuk = new DateTime($s->tglmasuk);
                    $datekeluar = new DateTime($s->tglkeluar);

                    $age = $init->findage($s->tglahirP);
                    $lamarawat = $init->lamarawat($s->tglmasuk, $s->tglkeluar);

                    $html .= '
                        <tr role="row" class="even">
                            <td>
                                ' . $no . '
                            </td>
                            <td>
                                ' . $s->no_register . '
                            </td>
                            <td>
                                ' . $s->norm . '
                            </td>
                            <td>
                                ' . $s->nmpasien . '
                            </td>
                            <td>
                                ' . $age . '
                            </td>
                            <td>                              
                                ' . $s->nmpoli . '             </td>
                            <td>
                                ' . $s->nmdokter . '
                            </td>
                            <td>
                                ' . $s->nmruang . '
                            </td>
                            <td>
                                ' . $datemasuk->format('d/m/Y H:i:s') . '
                            </td>
                            <td>
                                ' . $datekeluar->format('d/m/Y H:i:s') . '
                            </td>
                            <td>
                                ' . "$lamarawat hari" . '
                            </td>
     
                            <td>
                                ' . $s->nmcrkeluar . '
                            </td>
                        </tr>
                                ';
                    $no++;
                endforeach;
            $html .= '</tbody>';

        endforeach;

    $html .= '</table>
            </div>
            <!-- End Table -->
        </body>
        </html>
        ';

    $dompdf = new Dompdf();
    $dompdf->loadHtml($html);
    $savein = 'download/Document/';
    $dompdf->setPaper('A4', 'Landscape');
    $dompdf->render();
    // Download pdf
    $dompdf->stream($filename, ["Attachment" => 0]);
}
?>
© www.soinside.com 2019 - 2024. All rights reserved.