2013-08-01 30 views
0

我覺得我有一小步,我很想念其他相關問題上的每個人都能理解。如何讓domPDF正確顯示我的代碼視圖

我創建了一個簡單的CI 2視圖,控制器和模型,如下圖所示:

我已經安裝了DOMPDF到助手文件夾,如下所示:

applications/helpers/dompdf 

applications/helpers/dompdf/dompdf_help.php 

我想什麼當用戶點擊視圖頁面上的提交按鈕時,發送表單數據到數據庫,然後獲得填寫表單的PDF。

在獲取未定義的var錯誤或什麼也沒有,除了數據去db,我看不到我失蹤。

有人請指導我嗎?我不在這裏?

查看

<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>test pdf</title> 
    </head> 
    <body> 
<?php // Change the css classes to suit your needs  

$attributes = array('class' => '', 'id' => ''); 
echo form_open('quicksubmit', $attributes); ?> 

<p> 
     <label for="title">Title <span class="required">*</span></label> 
     <?php echo form_error('title'); ?> 

     <?php // Change the values in this array to populate your dropdown as required ?> 
     <?php $options = array(
        '' => 'Please Select', 
        'Mrs' => 'Mrs', 
        'Miss' => 'Miss', 
        'Ms' => 'Ms', 
        'Mr' => 'Mr', 
       ); ?> 

     <br /><?php echo form_dropdown('title', $options, set_value('title'))?> 
</p>            

<p> 
     <label for="first_name">First Name</label> 
     <?php echo form_error('first_name'); ?> 
     <br /><input id="first_name" type="text" name="first_name" maxlength="100" value="<?php echo set_value('first_name'); ?>" /> 
</p> 

<p> 
     <label for="last_name">Last Name <span class="required">*</span></label> 
     <?php echo form_error('last_name'); ?> 
     <br /><input id="last_name" type="text" name="last_name" maxlength="100" value="<?php echo set_value('last_name'); ?>" /> 
</p> 

<p> 
     <label for="branch">Branch</label> 
     <?php echo form_error('branch'); ?> 

     <?php // Change the values in this array to populate your dropdown as required ?> 
     <?php $options = array(
        '' => 'Please Select', 
        'Branch 1' => 'Branch One', 
        'Branch 2' => 'Branch Two', 
       ); ?> 

     <br /><?php echo form_dropdown('branch', $options, set_value('branch'))?> 
</p>            

<p> 
     <label for="zip">Zip</label> 
     <?php echo form_error('zip'); ?> 
     <br /><input id="zip" type="text" name="zip" maxlength="7" value="<?php echo set_value('zip'); ?>" /> 
</p> 


<p> 
     <?php echo form_submit('submit', 'Submit'); ?> 
</p> 

<?php echo form_close(); ?> 

    </body> 
</html> 

控制器

<?php 

class Quicksubmit extends CI_Controller { 

    function __construct() 
    { 
     parent::__construct(); 
     $this->load->library('form_validation'); 
     $this->load->database(); 
     $this->load->helper('form'); 
     $this->load->helper('url'); 
     $this->load->model('quicksubmit_model'); 
    } 
    function index() 
    {   
     $this->form_validation->set_rules('title', 'Title', 'required|trim|xss_clean|max_length[50]');   
     $this->form_validation->set_rules('first_name', 'First Name', 'trim|xss_clean|max_length[100]');    
     $this->form_validation->set_rules('last_name', 'Last Name', 'required|trim|xss_clean|max_length[100]');   
     $this->form_validation->set_rules('branch', 'Branch', 'trim|xss_clean|max_length[100]');    
     $this->form_validation->set_rules('zip', 'Zip', 'trim|xss_clean|is_numeric|max_length[7]'); 

     $this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>'); 

     if ($this->form_validation->run() == FALSE) // validation hasn't been passed 
     { 
      $this->load->view('quicksubmit_view'); 
     } 
     else // passed validation proceed to post success logic 
     { 
      // build array for the model 
        $this->pdf($output); 

         $form_data = array(
            'title' => set_value('title'), 
            'first_name' => set_value('first_name'), 
            'last_name' => set_value('last_name'), 
            'branch' => set_value('branch'), 
            'zip' => set_value('zip') 
            ); 

      // run insert model to write data to db 

      if ($this->quicksubmit_model->SaveForm($form_data) == TRUE) // the information has therefore been successfully saved in the db 
      { 
       redirect('quicksubmit/success'); // or whatever logic needs to occur 
      } 
      else 
      { 
      echo 'An error occurred saving your information. Please try again later'; 
      // Or whatever error handling is necessary 
      } 
     } 
    } 
    function success() 
    { 
      redirect(base_url(),'refresh');  
      /*echo 'this form has been successfully submitted with all validation being passed. All messages or logic here. Please note 
      sessions have not been used and would need to be added in to suit your app';*/ 
    } 
     function pdf() 
      { 
       $this->load->helper(array('dompdf', 'file')); 
       // page info here, db calls, etc.  
       $html = $this->load->view('quicksubmit_view', $data, true); 
       pdf_create($html, 'filename'); 
       /*or 
       $data = pdf_create($html, '', false); 
       write_file('name', $data);*/ 
       //if you want to write it to disk and/or send it as an attachment  
      } 
} 
?> 

型號

<?php 

class Quicksubmit_model extends CI_Model { 

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

    // -------------------------------------------------------------------- 

     /** 
     * function SaveForm() 
     * 
     * insert form data 
     * @param $form_data - array 
     * @return Bool - TRUE or FALSE 
     */ 

    function SaveForm($form_data) 
    { 
     $this->db->insert('quicksubmit', $form_data); 

     if ($this->db->affected_rows() == '1') 
     { 
      return TRUE; 
     } 

     return FALSE; 
    } 
} 
?> 

domp df_help.php文件

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

function pdf_create($html, $filename='', $stream=TRUE) 
{ 
    require_once("dompdf/dompdf_config.inc.php"); 

     $dompdf = new DOMPDF(); 
     $dompdf->load_html($html); 
     $dompdf->render(); 
     if ($stream) { 
      $dompdf->stream($filename.".pdf"); 
     } else { 
      return $dompdf->output(); 
     } 
} 
?> 
+0

我不是CI的使用者,所以請原諒我的無知。您可以調用'$ this-> pdf($ output)',但不要設置$ output,並且'pdf'方法不接受任何參數。只是一個代碼清潔點。在'pdf'方法中,您加載一個視圖,並將'$ data'變量傳遞給它。是由CI設置的變量,因爲它沒有在我能看到的其他任何地方定義?你可能想要去掉一些你不需要的簡化代碼和解決錯誤(你應該在這裏發佈,因爲它們可能是相關的)。 – BrianS

回答

1

你快到了!

將dompdf存儲在third_party文件夾中可能會更好,並且它不是代碼點火器助手。 - 請參閱我將它存儲在構造函數中的路徑。然後它總是可用的。

而且,它可能是更好的做模型的程序的「工作」,所以這包括PDF文件等

  • 在你的代碼的末尾不使用?>。

我改變了你的代碼工作,並驗證它確實工作。它只是保存一個名爲tmp/name.pdf的文件。我相信你可以解決其餘的問題。我沒有評論數據庫加載器,因爲我不需要測試代碼。

see enc。

<?php 

class Quicksubmit extends CI_Controller { 

function __construct() 
{ 
    parent::__construct(); 
    $this->load->library('form_validation'); 
    //$this->load->database(); 
    $this->load->helper('form'); 
    $this->load->helper('url'); 
    $this->load->helper('file'); 
    $this->load->model('quicksubmit_model'); 

    global $_dompdf_show_warnings;global $_dompdf_debug;global $_DOMPDF_DEBUG_TYPES;global $_dompdf_warnings;$_dompdf_show_warnings = FALSE; 
    require_once(realpath(APPPATH."third_party/dompdf")."/dompdf_config.inc.php"); // remember that the constant DOMPDF_TEMP_DIR may need to be changed. 
    spl_autoload_register('DOMPDF_autoload'); 
} 
function index() 
{   
    $this->form_validation->set_rules('title', 'Title', 'required|trim|xss_clean|max_length[50]');   
    $this->form_validation->set_rules('first_name', 'First Name', 'trim|xss_clean|max_length[100]');    
    $this->form_validation->set_rules('last_name', 'Last Name', 'required|trim|xss_clean|max_length[100]');   
    $this->form_validation->set_rules('branch', 'Branch', 'trim|xss_clean|max_length[100]');    
    $this->form_validation->set_rules('zip', 'Zip', 'trim|xss_clean|is_numeric|max_length[7]'); 

    $this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>'); 

    if ($this->form_validation->run() == FALSE) // validation hasn't been passed 
    { 
     $this->load->view('quicksubmit_view'); 
    } 
    else // passed validation proceed to post success logic 
    { 
     // build array for the model 


     $form_data = array(
        'title' => set_value('title'), 
        'first_name' => set_value('first_name'), 
        'last_name' => set_value('last_name'), 
        'branch' => set_value('branch'), 
        'zip' => set_value('zip') 
        ); 
     $this->pdf($form_data); 
     // run insert model to write data to db 

     if ($this->quicksubmit_model->SaveForm($form_data) == TRUE) // the information has therefore been successfully saved in the db 
     { 
      redirect('quicksubmit/success'); // or whatever logic needs to occur 
     } 
     else 
     { 
      echo 'An error occurred saving your information. Please try again later'; 
     // Or whatever error handling is necessary 
     } 
    } 
} 
function success() 
{ 
     redirect(base_url(),'refresh');  
     /*echo 'this form has been successfully submitted with all validation being passed. All messages or logic here. Please note 
     sessions have not been used and would need to be added in to suit your app';*/ 
} 
function pdf($data) 
{ 
    $dompdf = new DOMPDF(); 
    $html = $this->load->view('quicksubmit_view', $data, true); 
    $dompdf->set_paper('a4','portrait'); 
    $dompdf->load_html($html); 
    $dompdf->render(); 
    $pdf = $dompdf->output(); 
    write_file('tmp/name.pdf', $pdf); 

} 
} 
相關問題