使用 CodeIgniter 生成 PDF

发布于 2024-10-17 16:47:46 字数 967 浏览 8 评论 0原文

我正在使用 dompdf 从 html 文件创建一个 pdf 文件,该文件是动态创建的它的目的是作为 pdf 生成器的输入,但是我在执行此操作时遇到了麻烦,我在 这个线程中实现了代码 一切正常(我可以输出一个简单的 pdf),但是当我尝试给它一个更具体的 url 时,我收到此错误:

遇到错误,无法 加载请求的文件

这是有问题的代码:

function printPDF(){

            //write_file() usa un helper (file)
            $this->load->library('table');
            $this->load->plugin('to_pdf');
             // page info here, db calls, etc.
             $query = $this->db->get('producto');
             $data['table'] = $this->table->generate($query);
             $path_url = base_url().'print/existencias.html';
             write_file($path_url, $data['table']);
             $html = $this->load->view($path_url, 'consulta', true);
             pdf_create($html, 'consulta');
        }

I am using dompdf to create a pdf file out of an html file that gets created on-the-fly for the sole purpose of it serving as input for the pdf generator, however I am having trouble doing this, I implemented the code in this thread and everything works fine (I could output a simple pdf) however when I try to give it a more specific url I get this error:

An Error Was Encountered Unable to
load the requested file

here's the code that has the problem:

function printPDF(){

            //write_file() usa un helper (file)
            $this->load->library('table');
            $this->load->plugin('to_pdf');
             // page info here, db calls, etc.
             $query = $this->db->get('producto');
             $data['table'] = $this->table->generate($query);
             $path_url = base_url().'print/existencias.html';
             write_file($path_url, $data['table']);
             $html = $this->load->view($path_url, 'consulta', true);
             pdf_create($html, 'consulta');
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮云落日 2024-10-24 16:47:46

不确定确切的问题,但请检查一下:

1)如CI手册中所述,load->view的第二个参数应该是关联数组或对象,通过extract转换为vars。这可能会在生成 $html 时产生一些问题。

2) 尝试使 $path_url 相对于 application/views 目录,如 CI 手册中所述。

Not sure about the exact problem, but please check this:

1) as stated in CI's manual, load->view's second parameter should be an associative array or an objet, translated to vars via extract. That may generate some problem generating $html.

2) try making $path_url relative to application/views directory, as read in CI's manual.

动次打次papapa 2024-10-24 16:47:46

您应该使用 tcpdf 创建 pdf。
//创建控制器例如:

public function create_pdf() 
    {
     $this->load->library("Pdf");
     $data['results'] = // your data
     $this->load->view('pdfview',$data);

    }


//pdfview is the page having tcpdf code and your pdf code.

you should use tcpdf to create a pdf.
//create controller for example :

public function create_pdf() 
    {
     $this->load->library("Pdf");
     $data['results'] = // your data
     $this->load->view('pdfview',$data);

    }


//pdfview is the page having tcpdf code and your pdf code.
折戟 2024-10-24 16:47:46

你可以这样尝试

public function export_pdf() {
    $filename = 'FILENAME.pdf';
    header("Content-Description: Advertise Report");
    header("Content-Disposition: attachment; filename=$filename");
    header("Content-Type: application/pdf; ");
    $file = fopen('php://output', 'w');
    $header = array("Question", "Answer", "Name", "Email", "Phone", "Date");
    fputpdf($file, $header);
    fputpdf($file, 'YOUR DATA');
    fclose($file);
    exit;
}

You can try it like this

public function export_pdf() {
    $filename = 'FILENAME.pdf';
    header("Content-Description: Advertise Report");
    header("Content-Disposition: attachment; filename=$filename");
    header("Content-Type: application/pdf; ");
    $file = fopen('php://output', 'w');
    $header = array("Question", "Answer", "Name", "Email", "Phone", "Date");
    fputpdf($file, $header);
    fputpdf($file, 'YOUR DATA');
    fclose($file);
    exit;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文