在查看文件Codeignitor 3和dompdf中插入下载链接

时间:2018-11-11 18:12:29

标签: php codeigniter-3 dompdf

我安装了codeigniter 3和dompdf。我遵循了一个教程,什么是完美的。目前,每次我加载视图(例如localhost / codeigniter / welcome)时,都会直接加载PDF并提供下载。 取而代之的是,我希望评分者想在视图页面中包含一个可点击的链接,用户可以在其中查看页面并可以决定是否要以pdf格式下载页面。我无法解决如何创建链接。我需要在控制器中进行哪些更改,以便客户可以在将页面下载为pdf之前查看以及如何创建链接。

任何帮助都会得到解决

控制器Welcome.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function index()
    {
        $this->load->view('welcome_message');

             // Get output html
             $html = $this->output->get_output();

             // Load pdf library
             $this->load->library('pdf');

             // Load HTML content
             $this->dompdf->loadHtml($html);

             // (Optional) Setup the paper size and orientation
             $this->dompdf->setPaper('A4', 'landscape');

             // Render the HTML as PDF
             $this->dompdf->render();

             // Output the generated PDF (1 = download and 0 = preview)
             $this->dompdf->stream("welcome.pdf", array("Attachment"=>0));
    }
}

库PHP

 <?php defined('BASEPATH') OR exit('No direct script access allowed');

// reference the Dompdf namespace
use Dompdf\Dompdf;

class Pdf
{
    public function __construct(){

        // include autoloader
    require_once dirname(__FILE__).'/dompdf/autoload.inc.php';

        // instantiate and use the dompdf class
        $pdf = new DOMPDF();

        $CI =& get_instance();
        $CI->dompdf = $pdf;

    }
}
?>

查看文件

<body>

<div id="container">
    <a target="_blank" class="pdfLink" href="<?php echo base_url('welcome'); ?>">download pdf</a>

    <h1>Welcome to CodeIgniter!</h1>

    <div id="body">
        <p>The page you are looking at is being generated dynamically by CodeIgniter.</p>

        <p>If you would like to edit this page you'll find it located at:</p>
        <code>application/views/welcome_message.php</code>

        <p>The corresponding controller for this page is found at:</p>
        <code>application/controllers/Welcome.php</code>

        <p>If you are exploring CodeIgniter for the very first time, you should start by reading the <a href="user_guide/">User Guide</a>.</p>
    </div>

    <p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo  (ENVIRONMENT === 'development') ?  'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>
</div>

</body>
</html>

0 个答案:

没有答案