Codeigniter - mPDF 导出空白页

Posted

技术标签:

【中文标题】Codeigniter - mPDF 导出空白页【英文标题】:Codeigniter - mPDF exports Blank Page 【发布时间】:2018-09-25 20:58:12 【问题描述】:

我使用 mPDF 来导出 PDF 文档。但是当我使用 mPDF 导出到 PDF 时出现空白页。我想根据属性Id 显示一些属性信息,但它会创建空白页。 mPDF 适用于welcome_message.php,但我无法使用我的页面制作 PDF。我哪里做错了?

控制器:

public function getReportProperties($cusId)

    

        $this->lang->load('content', $this->session->userdata('people_lang'));

        $viewData['customers'] = $this->report_model->get_customers();

        $viewData['estateList'] = $this->report_model->getEstatesByCustomer($cusId);

        $viewData['byCustomer'] = $this->db->where("cusId", $cusId)->get("customer")->row();

        $data = [];
        //load the view and saved it into $html variable
        $html = $this->load->view('reportProperties', $viewData, $data, true);

        $html = mb_convert_encoding($html, 'UTF-8', 'UTF-8');

        //this the the PDF filename that user will get to download
        $pdfFilePath = "properties.pdf";

        //load mPDF library
        $this->load->library('m_pdf');

        //generate the PDF from the given html
        $this->m_pdf->pdf->WriteHTML($html);

        //download it.
         $this->m_pdf->pdf->Output($pdfFilePath, "D");

    

查看:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html <?php if($this->session->userdata('people_lang') == "ar")  echo "dir='rtl'";  else  echo "";  ?>">
<head>
    <?php $this->load->view("common/head"); ?>
    <!-- Includes -->
    <?php $this->load->view("reports/includes"); ?>
</head>
<body>

    <!-- Page content -->
    <div class="page-content">

        <!-- Main content -->
        <div class="content-wrapper">

            <!-- Content area -->
            <div class="content">

                <!-- Inner container -->
                <div class="d-flex align-items-start flex-column flex-md-row">

                    <!-- Left content -->
                    <div class="w-100 overflow-auto order-2 order-md-1">

                        <!-- Task overview -->
                        <div class="card">
                            <div class="card-header header-elements-md-inline">
                                <h5 class="card-title"><?php echo $byCustomer->cosName. ' ' .$byCustomer->cosSurname; ?> | All Property List</h5>
                                <div class="header-elements">
                                    <a href="#" class="btn bg-teal-400 btn-sm btn-labeled btn-labeled-right"><?php echo date("d.m.Y"); ?> <b><i class="icon-alarm-check"></i></b></a>
                                </div>
                            </div>

                            <div class="col-sm-6 col-xl-12">

                                <!-- Bar chart in colored card -->
                                <div class="card bg-grey-200 has-bg-image text-center">
                                    <div class="card-body">
                                        <img src="<?php echo base_url().'upload/report/logo.png' ?>"  >
                                    </div>

                                    <div class="container-fluid">
                                        <div id="chart_bar_color"></div>
                                    </div>
                                </div>
                                <!-- /bar chart in colored card -->

                            </div>
                            <?php $i = 1; foreach($estateList as $estate)  ?>
                                <div class="card-body">
                                    <h6 class="font-weight-semibold"><span class="btn rounded-circle bg-grey"><?php echo $i++ ?></span> Property Type: <?php echo $estate->estateTypeEng; ?></h6>
                                    <hr>
                                    <div class="row container-fluid">
                                        <div class="col-md-6">
                                            <div class="mb-4">
                                                <dl>
                                                    <dt class="font-size-sm text-primary text-uppercase">City</dt>
                                                    <dd><?php if($estate->cityName == '')  echo '-';  else  echo $estate->cityName;  ?></dd>

                                                    <dt class="font-size-sm text-primary text-uppercase">Property Address: </dt>
                                                    <dd><?php if($estate->estateAddress == '')  echo '-';  else  echo $estate->estateAddress;  ?></dd>

                                                    <dt class="font-size-sm text-primary text-uppercase">Property Acreage m²: </dt>
                                                    <dd><?php if($estate->estateCentare == '')  echo '-';  else  echo $estate->estateCentare;  ?></dd>

                                                    <dt class="font-size-sm text-primary text-uppercase">Heating Type: </dt>
                                                    <dd><?php if($estate->heatingNameEn == '')  echo '-';  else  echo $estate->heatingNameEn;  ?></dd>

                                                    <dt class="font-size-sm text-primary text-uppercase">Garden Acreage m²: : </dt>
                                                    <dd><?php if($estate->estateGardenArea == '')  echo '-';  else  echo $estate->estateGardenArea;  ?></dd>
                                                </dl>
                                            </div>
                                        </div>

                                        <div class="col-md-6">
                                            <div class="mb-4">
                                                <dl>
                                                    <dt class="font-size-sm text-primary text-uppercase">Number of Room: </dt>
                                                    <dd><?php if($estate->estateRoom == '')  echo '-';  else  echo $estate->estateRoom;  ?></dd>

                                                    <dt class="font-size-sm text-primary text-uppercase">Number of Bathroom: </dt>
                                                    <dd><?php if($estate->estateBathroom == '')  echo '-';  else  echo $estate->estateBathroom;  ?></dd>

                                                    <dt class="font-size-sm text-primary text-uppercase">Number of Livining Room: </dt>
                                                    <dd><?php if($estate->estateSalon == '')  echo '-';  else  echo $estate->estateSalon;  ?></dd>

                                                    <dt class="font-size-sm text-primary text-uppercase">Number of Balcony: </dt>
                                                    <dd><?php if($estate->estateBalcony == '')  echo '-';  else  echo $estate->estateBalcony;  ?></dd>
                                                </dl>
                                            </div>
                                        </div>
                                    </div>

                                    <h6 class="font-weight-semibold" hidden>Requirements</h6>
                                    <p class="mb-3" hidden>So slit more darn hey well wore submissive savage this shark aardvark fumed thoughtfully much drank when angelfish so outgrew some alas vigorously therefore warthog superb less oh groundhog less alas gibbered barked some hey despicably with aesthetic hamster jay by luckily.</p>

                                    <div class="card card-table table-responsive shadow-0" hidden>
                                        <table class="table">
                                            <thead>
                                            <tr>
                                                <th>#</th>
                                                <th>Task</th>
                                                <th>Due date</th>
                                                <th>Description</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <tr>
                                                <td>1</td>
                                                <td><span class="font-weight-semibold">Design mockup</span></td>
                                                <td>
                                                    <div class="d-inline-flex align-items-center">
                                                        <i class="icon-calendar2 mr-2"></i>
                                                        <input type="text" class="form-control datepicker p-0 border-0 bg-transparent" value="21 January, 15">
                                                    </div>
                                                </td>
                                                <td>Create design mockups for a new app, must be delivered before 1st of March</td>
                                            </tr>
                                            <tr>
                                                <td>2</td>
                                                <td><span class="font-weight-semibold">User interface research</span></td>
                                                <td>
                                                    <div class="d-inline-flex align-items-center">
                                                        <i class="icon-calendar2 mr-2"></i>
                                                        <input type="text" class="form-control datepicker p-0 border-0 bg-transparent" value="24 January, 15">
                                                    </div>
                                                </td>
                                                <td>Create a focus group with random people, provide a research statement</td>
                                            </tr>
                                            <tr>
                                                <td>3</td>
                                                <td><span class="font-weight-semibold">New icons set</span></td>
                                                <td>
                                                    <div class="d-inline-flex align-items-center">
                                                        <i class="icon-calendar2 mr-2"></i>
                                                        <input type="text" class="form-control datepicker p-0 border-0 bg-transparent" value="28 January, 15">
                                                    </div>
                                                </td>
                                                <td>Create a full set of icons required for the ios application, send them to team lead for review</td>
                                            </tr>
                                            <tr>
                                                <td>4</td>
                                                <td><span class="font-weight-semibold">Loading optimization</span></td>
                                                <td>
                                                    <div class="d-inline-flex align-items-center">
                                                        <i class="icon-calendar2 mr-2"></i>
                                                        <input type="text" class="form-control datepicker p-0 border-0 bg-transparent" value="1 February, 15">
                                                    </div>
                                                </td>
                                                <td>Review image sizes, compress them as much as possible, make sure page loading time is less than 1 second</td>
                                            </tr>
                                            </tbody>
                                        </table>
                                    </div>

                                    <h6 class="font-weight-semibold" hidden>Uploaded files</h6>
                                    <p hidden>A much goodness between destructive that save stupid firefly destructively dog goldfinch continually alas pinched for outside flailed inescapably hey brought rid crud and awakened sobbed extraordinarily wherever deer before tenable yet into dalmatian opposite save close ahead next independent mindful but far.</p>

                                    <div class="row" hidden>
                                        <div class="col-xl-3 col-sm-6">
                                            <div class="card">
                                                <div class="card-img-actions mx-1 mt-1">
                                                    <img class="card-img img-fluid" src="../../../../global_assets/images/demo/flat/9.png" >
                                                    <div class="card-img-actions-overlay card-img">
                                                        <a href="../../../../global_assets/images/demo/flat/9.png" class="btn btn-outline bg-white text-white border-white border-2 btn-icon rounded-round" data-popup="lightbox" rel="group">
                                                            <i class="icon-zoomin3"></i>
                                                        </a>

                                                        <a href="#" class="btn btn-outline bg-white text-white border-white border-2 btn-icon rounded-round ml-2">
                                                            <i class="icon-download"></i>
                                                        </a>
                                                    </div>
                                                </div>

                                                <div class="card-body">
                                                    <div class="d-flex align-items-start flex-wrap">
                                                        <div class="font-weight-semibold">dashboard_draft.png</div>
                                                        <span class="font-size-sm text-muted ml-auto">378Kb</span>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="col-xl-3 col-sm-6">
                                            <div class="card">
                                                <div class="card-img-actions mx-1 mt-1">
                                                    <img class="card-img img-fluid" src="../../../../global_assets/images/demo/flat/8.png" >
                                                    <div class="card-img-actions-overlay card-img">
                                                        <a href="../../../../global_assets/images/demo/flat/9.png" class="btn btn-outline bg-white text-white border-white border-2 btn-icon rounded-round" data-popup="lightbox" rel="group">
                                                            <i class="icon-zoomin3"></i>
                                                        </a>

                                                        <a href="#" class="btn btn-outline bg-white text-white border-white border-2 btn-icon rounded-round ml-2">
                                                            <i class="icon-download"></i>
                                                        </a>
                                                    </div>
                                                </div>

                                                <div class="card-body">
                                                    <div class="d-flex align-items-start flex-wrap">
                                                        <div class="font-weight-semibold">profile_page.png</div>
                                                        <span class="font-size-sm text-muted ml-auto">1.2Mb</span>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="col-xl-3 col-sm-6">
                                            <div class="card">
                                                <div class="card-img-actions mx-1 mt-1">
                                                    <img class="card-img img-fluid" src="../../../../global_assets/images/demo/flat/6.png" >
                                                    <div class="card-img-actions-overlay card-img">
                                                        <a href="../../../../global_assets/images/demo/flat/9.png" class="btn btn-outline bg-white text-white border-white border-2 btn-icon rounded-round" data-popup="lightbox" rel="group">
                                                            <i class="icon-zoomin3"></i>
                                                        </a>

                                                        <a href="#" class="btn btn-outline bg-white text-white border-white border-2 btn-icon rounded-round ml-2">
                                                            <i class="icon-download"></i>
                                                        </a>
                                                    </div>
                                                </div>

                                                <div class="card-body">
                                                    <div class="d-flex align-items-start flex-wrap">
                                                        <div class="font-weight-semibold">shopping_cart.png</div>
                                                        <span class="font-size-sm text-muted ml-auto">1.8Mb</span>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="col-xl-3 col-sm-6">
                                            <div class="card">
                                                <div class="card-img-actions mx-1 mt-1">
                                                    <img class="card-img img-fluid" src="../../../../global_assets/images/demo/flat/12.png" >
                                                    <div class="card-img-actions-overlay card-img">
                                                        <a href="../../../../global_assets/images/demo/flat/9.png" class="btn btn-outline bg-white text-white border-white border-2 btn-icon rounded-round" data-popup="lightbox" rel="group">
                                                            <i class="icon-zoomin3"></i>
                                                        </a>

                                                        <a href="#" class="btn btn-outline bg-white text-white border-white border-2 btn-icon rounded-round ml-2">
                                                            <i class="icon-download"></i>
                                                        </a>
                                                    </div>
                                                </div>

                                                <div class="card-body">
                                                    <div class="d-flex align-items-start flex-wrap">
                                                        <div class="font-weight-semibold">sales_statistics.png</div>
                                                        <span class="font-size-sm text-muted ml-auto">2.0Mb</span>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                            <?php  ?>

                            <div class="card-footer d-sm-flex justify-content-sm-between align-items-sm-center">
                                <span class="d-flex align-items-center">
                                    <span class="badge badge-mark border-green mr-2"></span>
                                    <?php echo $byCustomer->cosName. ' ' .$byCustomer->cosSurname; ?> has <?php
                                    $cusId = $byCustomer->cusId;
                                    $this->db->select('*');
                                    $this->db->where('cusId', $cusId);
                                    $query = $this->db->get('estate');
                                    $num = $query->num_rows();
                                    echo $num; ?> <?php if($num > 1)  echo 'Properties.';  elseif($num = 1)  echo 'Property.';  elseif($num = 0)  echo 'no any Property yet.'; ?>
                                </span>

                                <ul class="list-inline list-inline-condensed mb-0 mt-2 mt-sm-0">
                        <span class="d-flex align-items-right">
                            <?php echo date('Y'); ?>
                        </span>
                                </ul>
                            </div>
                        </div>
                        <!-- /task overview -->

                    </div>
                    <!-- /left content -->

                </div>
                <!-- /inner container -->

            </div>
            <!-- /content area -->

        </div>
        <!-- /main content -->
    </div>
    <!-- /page content -->
</body>
</html>

【问题讨论】:

有人帮忙吗? 【参考方案1】:

我对@9​​87654321@ 不是很熟悉,但我确实看到了一个错误:

$html = $this-&gt;load-&gt;view('reportProperties', $viewData, $data, true);

view 只接受 3 个参数:第一个视图页面,第二个数据数组,第三个返回视图的布尔值。 $data 应该被删除。

$html = $this-&gt;load-&gt;view('reportProperties', $viewData, true);

【讨论】:

我试过 $html = $this->load->view('reportProperties', $viewData, true);但它给了我 pdf 的空白页。 检查错误日志,然后回显 $html 并注释掉之后的所有内容。这是你所期望的吗?有错误吗? 没有给出错误。只需创建空白 pdf 页面 195 页。【参考方案2】:

为了获取视图内容,您需要传递第三个可选参数,TRUE (boolean) - please see: Returning views as data

代替

//load the view and saved it into $html variable
$html = $this->load->view('reportProperties', $viewData, $data, true);

你需要使用:

//load the view and saved it into $html variable
$html = $this->load->view('reportProperties', $viewData, true);

另外,请添加 reportProperties 视图文件的内容。

EDIT 1 - repostProperties 上传后

您的视图不适用于使用 mpdf。 首先,图像 url 需要是绝对的,而不是相对的。 此外,for 循环,我认为这是一个问题,因为你会得到很多页面。

首先从简单的视图文件开始,然后,逐步添加另一个数据。

请查看mPDF documentation 了解支持的内容以及如何对内容进行分页。

【讨论】:

我在帖子中添加了查看页面。 如果我这样做了:$html = $this->load->view('reportProperties', $viewData, true);等了很久之后它给了我195个空白页pdf。【参考方案3】:

加载视图时发生错误。它应该是三个参数,但提供了四个。

我发现 Mpdf 只是在发生错误时才显示空白页。

【讨论】:

以上是关于Codeigniter - mPDF 导出空白页的主要内容,如果未能解决你的问题,请参考以下文章

MPDF 无输出(空白页)

在 codeigniter 中使用 MPDF 设置格式视图导出 PDF

分页数在 mpdf 中未正确显示

如何删除mPdf中的空白页

CodeIgniter 简单表单验证返回空白页

Codeigniter 显示空白页而不是错误消息