2012-02-16 72 views
0

我正在使用OpenTbs,http://www.tinybutstrong.com/plugins/opentbs/tbs_plugin_opentbs.htmlOpenTbs將html標籤轉換爲MS Word標籤

我有一個template.docx,我可以用內容替換字段,但如果內容有html代碼,它將顯示在模板創建的文檔中。

First list <br /> Second Line 

我曾嘗試使用:

$TBS->LoadTemplate('document.docx', OPENTBS_ALREADY_XML); 

思考,這將讓我代替我與MS Office的標籤HTML標籤,但它只是顯示在文檔而不是在MS Office的標籤:

First Line<w:br/> Second Line 

如何將HTML標記轉換爲MS Office XML等效項。

+0

我在這裏拉我的頭髮,這應該是非常普遍的文本,我用[b。文本]將有HTML樣式,我想變成MS Word樣式像大膽和斜體,我已經找到了MS Word的等價物,但不能讓他們作爲代碼,他們只是作爲文本輸出到模板中的功能.. 。任何幫助將非常感激。 – 2012-02-17 08:58:47

回答

1

由於您已將HTML轉換爲DOCX,因此您可以使用自定義PHP函數和參數「onformat」在OpenTBS中實現它。

下面的函數只轉換換行符:

function f_html2docx($FieldName, &$CurrVal) { 
    $CurrVal= str_replace('<br />', '<w:br/>', $CurrVal); 
} 

在DOCX模板中使用:

[b.thetext;onformat=f_html2docx] 

有關轉換的HTML DOCX:

一個格式化的文本轉換成另格式化文本往往是一場噩夢。這就是爲什麼存儲純數據而非格式化數據是明智之舉。

將HTML轉換爲DOCX是一個真正的噩夢,因爲格式化的構造方式不同。

例如,在HTML標籤我可以嵌套,就像這樣:

<i> hello <b> this is important </b> to know </i> 

在DOCX這將作爲交叉,就像這樣:

<w:r> 
    <w:rPr><w:b/></w:rPr> 
    <w:t>hello</w:t> 
    </w:r> 

    <w:r> 
    <w:rPr><w:b/><w:i/></w:rPr> 
    <w:t>this is important</w:t> 
    </w:r> 

    <w:r> 
    <w:rPr><w:i/></w:rPr> 
    <w:t>to know</w:t> 
    </w:r> 

我對轉換標籤無解現在除了換行符之外。對不起。 我認爲編寫代碼會非常困難。

+0

我不知道你是否最終找到了解決方案?我正在尋找類似的東西。我只會在HTML中進行基本的格式設置,如粗體,下劃線和斜體等。 – sluggerdog 2012-10-09 09:10:06

+0

我沒有參與過這樣的解決方案,我只是在報告其他解決方案。 – Skrol29 2012-10-09 14:01:50

+0

謝謝,我想我會用PDF代替它,因爲我知道這會起作用。謝謝 – sluggerdog 2012-10-09 20:54:35

1

感謝Skrol在你所有的openTBS問題上的投入,只是注意到你是它的創建者,它是一個很好的課程,上面說的是真實的,經過一天的學習,我學習了MS Word格式腦波,我現在能夠生成上面指定的格式,並且可以使用粗體斜體和下劃線,這是我所需要的,我希望這可以爲您提供改進的基礎。

我基本上注意到,在這個例子中,你只需要一個樣式數組,當你找到一個結束標籤時,你將從樣式數組中刪除。每當你找到一個標籤,你需要關閉<w:r>並創建一個新的標籤,我已經測試過它,它的功能非常好。

class printClass { 
    private static $currentStyles = array();  

    public function __construct() {} 

    public function format($string) { 
      if($string !=""){ 
      return preg_replace_callback("#<b>|<u>|<i>|</b>|</u>|</i>#", 
             'printClass::replaceTags', 
             $string); 
     }else{ 
      return false; 
     } 
    } 


    private static function applyStyles() { 

     if(count(self::$currentStyles) > 0) { 

      foreach(self::$currentStyles as $value) { 

       if($value == "b") { 
        $styles .= "<w:b/>"; 
       } 

       if($value == "u") { 
        $styles .= "<w:u w:val=\"single\"/>"; 
       } 

       if($value == "i") { 
        $styles .= "<w:i/>"; 
       } 
      } 

      return "<w:rPr>" . $styles . "</w:rPr>"; 
     }else{ 
      return false; 
     } 
    } 



    private static function replaceTags($matches) { 

     if($matches[0] == "<b>") { 
      array_push(self::$currentStyles, "b"); 
     } 

     if($matches[0] == "<u>") { 
      array_push(self::$currentStyles, "u"); 
     } 

     if($matches[0] == "<i>") { 
      array_push(self::$currentStyles, "i"); 
     } 

     if($matches[0] == "</b>") { 
      self::$currentStyles = array_diff(self::$currentStyles, array("b")); 
     } 

     if($matches[0] == "</u>") { 
      self::$currentStyles = array_diff(self::$currentStyles, array("u")); 
     } 

     if($matches[0] == "</i>") { 
      self::$currentStyles = array_diff(self::$currentStyles, array("i")); 
     } 

     return "</w:t></w:r><w:r>" . self::applyStyles() . "<w:t xml:space=\"preserve\">"; 
    } 
}