在codeigniter中使用dompdf库

时间:2017-07-18 10:41:20

标签: php codeigniter dompdf

任何人都可以帮助我,我正在尝试使用带有codeigniter的dompdf库。当我在localhost上做它的工作。但是当我的网站上传时,它无法正常工作。这是库代码

    <?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * CodeIgniter PDF Library
 *
 * Generate PDF's in your CodeIgniter applications.
 *
 * @package         CodeIgniter
 * @subpackage      Libraries
 * @category        Libraries
 * @author          Chris Harvey
 * @license         MIT License
 * @link            https://github.com/chrisnharvey/CodeIgniter-PDF-Generator-Library
 */
require_once(dirname(__FILE__) . '/dompdf-0.6.2/dompdf_config.inc.php');
class Pdf extends DOMPDF
{
    /**
     * Get an instance of CodeIgniter
     *
     * @access  protected
     * @return  void
     */
    protected function ci()
    {
        return get_instance();
    }
    /**
     * Load a CodeIgniter view into domPDF
     *
     * @access  public
     * @param   string  $view The view to load
     * @param   array   $data The view data
     * @return  void
     */
    public function load_view($view, $data = array())
    {
        $html = $this->ci()->load->view($view, $data, TRUE);
        $this->load_html($html);
    }
}

此代码调用创建pdf,我尝试仅调用视图并且它可以工作但是当它被称为“pdf”函数时,屏幕是空白的。

    $this->load->library('pdf');
    $this->pdf->load_view('mydocument', $data);
    $this->pdf->render();
    $this->pdf->stream("mydocument.pdf");

这是我文件的结构

enter image description here

3 个答案:

答案 0 :(得分:0)

如评论中所述,@ jagad89关于包含DOMPDF的自动加载文件是正确的。

不要将它与库包装一起使用,而是尝试使用Composer

进行安装
composer require dompdf/dompdf

然后在应用程序的$config['composer_autoload'] = '/path/to/vendor/autoload.php';中设置config.php,你最好使用这样的DOMPDF:

$dompdf = new Dompdf\Dompdf();
$dompdf->loadHtml('hello world');

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

答案 1 :(得分:0)

在控制器中使用以下代码,它应该适合您。

$this->load->library('pdf');
$this->pdf->load_view('mydocument', $data);
$this->pdf->setPaper('A4', 'portrait');
$this->pdf->render();
$this->pdf->stream("mydocument", array("Attachment" => 0));

答案 2 :(得分:0)

下载dompdf档案,并将其解压缩到dompdf将驻留的目录中

  

库/ PDF类:

 Number Direction   Street  PostType                  City  
  1234         [('S', 'StreetNamePreDirectional') [('Berverly  ','StreetName')]  ....