簡體   English   中英

使用 CodeIgniter 生成 PDF

[英]Generating a PDF using CodeIgniter

我正在使用dompdf從 html 文件中創建一個 pdf 文件,該文件是即時創建的,其唯一目的是用作 pdf 生成器的輸入,但是我在執行此操作時遇到了麻煩,我在實現了代碼線程和一切正常(我可以 output 一個簡單的 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');
        }

您應該使用 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.

不確定確切的問題,但請檢查:

1) 如 CI 手冊所述,load->view 的第二個參數應該是關聯數組或對象,通過提取轉換為變量。 這可能會產生一些生成 $html 的問題。

2)嘗試使 $path_url 相對於 application/views 目錄,如 CI 手冊中所述。

你可以這樣試試

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM