2010-06-23 100 views
0

RTF模板,我試圖使用RTF文件作爲生成包含每(MySQL的)數據庫行的頁面的新RTF文檔的模板。解析通過PHP

我的方法幾乎適用不同的是,生成的文件只包含一個頁面,從第一行,而不是一個頁面的每一行。 (我的測試中應該有2頁)。

這是主循環。

$today= date('d-m-Y'); 
$tp= new templateParser(LOCALADMIN.'_documents/fiche_individuelle.rtf'); 

foreach($inscriptions as $person) { 
    $tags= array('FIRSTNAME'=>$person['firstname'], 
      'LASTNAME'=>$person['lastname'], 
      'BIRTHDATE'=>$person['birthdate'], 
      'TELEPHONE1'=>$person['mobile_phone'], 
      'MEDICALBACKGROUND'=>$person['medical_background'], 
      'ALLERGIES'=>$person['allergies'] 
    ); 

    $tp->parseTemplate($tags); 
    $content .= $tp->display(); 

    $content .= "\\section\n"; 
    //END foreach 
} 
// create RTF Document 

    $today= date('d-m-Y-hms'); 
    $filename = $season_name.'_'.$today.'.rtf'; 

    header('Content-type: application/msword'); 
    header("Expires: 0"); 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    header("Pragma: public"); 
    header("content-disposition: attachment; filename=\"$filename\""); 
    print $content; 
    exit; 

對於被徹底起見,這裏是我的簡單的模板類代碼:

class templateParser { 
    // member definition 
    var $output; 
    var $log; 
    var $replacedTags; 

    function templateParser($templateFile = 'default_template.htm') 
    { 
     // constructor setting up class initialization 
     $this->log .= "templateParser() called<br />"; 
     if (file_exists($templateFile)) { 
      $this->log .= 'Found template file ' . $templateFile . '<br/>'; 
      $this->output = file_get_contents($templateFile); 
     } else { 
      $this->log .= 'Error:Template file ' . $templateFile . ' not found'; 
      return false; 
     } 
    } 

    function parseTemplate($tags = array()) 
    { 
     $this->log = 'parseTemplate() called. <br/>'; 
     // code for parsing template files 
     if (count($tags) > 0) { 
      foreach($tags as $tag => $data) { 
       $data = (file_exists($data))? $this->parseFile($data) : $data; 
       $this->output = str_replace('%' . $tag . '%', $data, $this->output); 
       $this->log .= 'parseTemplate() replaced <b>' . $tag . '</b> in template<br/>'; 
       $this->replacedTags ++; 
      } 
     } else { 
      $this->log .= 'WARNING: No tags were provided for replacement<br/>'; 
     } 
     return $this->replacedTags; 
    } 

    function parseFile($file) 
    { 
     $this->log .= 'parseFile() called. <br/>'; 
     ob_start(); 
     include($file); 
     $content = ob_get_contents(); 
     ob_end_clean(); 
     return $content; 
    } 

    function display() 
    { 
     // code for displaying the finished parsed page 
     return $this->output; 
    } 
} 
+0

我在尋找同樣的事情。你有沒有找到你的問題的答案? – 2011-02-16 10:43:27

回答

0

除了手動創建文檔,請嘗試使用PHPRtfLite

PHPRtfLite是一個API,使開發人員能夠使用php創建rtf文檔。 PHPRtfLite是按照OOP原則設計的。

+1

phprtflite是即時創建RTF文檔,不使用模板,這是最終用戶想要的。我不指望他能寫php。 – pixeline 2010-06-23 15:14:21