简体   繁体   English

如何在 Codeigniter 3 中使用 Dompdf

[英]How to use Dompdf in Codeigniter 3

I am trying to create/generate pdf from my application, I have planned to use dompdf but I am facing some challenges.我正在尝试从我的应用程序创建/生成 pdf,我计划使用 dompdf,但我面临一些挑战。

I have downloaded the dompdf from here https://github.com/dompdf/dompdf我已经从这里下载了dompdf https://github.com/dompdf/dompdf

I have created a file in /application/libraries directory Pdf.php我在/application/libraries目录Pdf.php中创建了一个文件

Pdf.php

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

require_once APPPATH.'third_party/dompdf/dompdf_config.inc.php';

use Dompdf\Dompdf;
class Pdf extends DOMPDF
{
    protected function ci()
    {
        return get_instance();
    }

    public function load_view($view, $data = array())
    {
        $dompdf = new Dompdf();
        $html = $this->ci()->load->view($view, $data, TRUE);

        $dompdf->loadHtml($html);

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

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

        // Output the generated PDF to Browser
        $dompdf->stream("welcome-". $time);
    }
}

Note: dompdf is in my /application/thirdparty directory注意: dompdf 在我的/application/thirdparty目录中

And in My Controller在我的Controller

    function pdf_test(){
        $this->load->library("pdf");
        $this->pdf->load_view('invoice_template');
    }

I am getting the below error Message: syntax error, unexpected '='我收到以下错误Message: syntax error, unexpected '='

When I fix above issue i get file_exists(): open_basedir restriction in effect.当我解决上述问题时,我得到file_exists(): open_basedir restriction in effect.

Use composer to get the library then in your class add this.使用 composer 获取库,然后在您的 class 中添加此库。

require 'vendor/autoload.php'; 

Path to your autoload.php may differ so check it to be sure.您的 autoload.php 的路径可能不同,因此请检查以确保。 Also libraries should be placed within the libraries dir.库也应该放在库目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM