2016-07-25 82 views
-1

我正在開發一個使用PHP Codeigniter的Web應用程序,我想要一個代碼來顯示和從網頁下載數據到PDF格式。如何將網頁中的數據轉換並下載到PDF

+5

你試了一下自己? StackOverflow不是一個編程服務。 –

+0

您想要動態生成PDF或只想下載已存儲的PDF。 –

+0

我在我的數據庫中有數據,使用該數據我必須創建pdf文件並下載它。 –

回答

0

下載mPDF並將其解壓到文件夾application/third_party/

創建application/libraries/名稱的新文件,它M_pdf.php

M_pdf.php文件

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

include_once APPPATH.'/third_party/mpdf/mpdf.php'; 

class M_pdf { 

    public $param; 
    public $pdf; 

    public function __construct($param = '"en-GB-x","A4","","",10,10,10,10,6,3') 
    { 
     $this->param =$param; 
     $this->pdf = new mPDF($this->param); 
    } 
} 

CI中那麼成功整合MPDF。

如何使用 負荷控制器的MPDF庫如下控制器,

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

class Cont_letter_pdf extends CI_Controller { 

    public function __construct() { 
     parent::__construct(); 

     $this->load->library('m_pdf'); //load mPDF library  
    } 

    public function pdf_download() 
    { 
     $data = []; 
     //load the view and saved it into $html variable 
     $html=$this->load->view('welcome_message', $data, true); 

     //this the the PDF filename that user will get to download 
     $pdfFilePath = "output_pdf_name.pdf"; 

     //load mPDF library 
     $this->load->library('m_pdf'); 

     //generate the PDF from the given html 
     $this->m_pdf->pdf->WriteHTML($html); 

     //download it. 
     $this->m_pdf->pdf->Output($pdfFilePath, "D"); 

    } 

我學習這從以下網址。您也可以從這裏查看教程。

https://arjunphp.com/generating-a-pdf-in-codeigniter-using-mpdf/