PHP 富文本html代碼生成word文檔,解決默認(rèn)Web板式瀏覽

這段是百度編輯器輸入的html代碼,自己保存下來


image.png

正文


image.png

然后導(dǎo)出word文檔

 public function export($html )
    {
         //傳入html代碼
        $file = time().rand(10000,99999);

        $path = './Public/tmp/'.$file.'.doc';  //自定義路徑
        
        //寫入內(nèi)容
        file_put_contents($path,$html);
        
        //下載文件
       dowmload('/Public/tmp/'.$file.'.doc','管理評審會議紀(jì)要_' . changeDateTime(time()));
      
        //刪除文件
         @unlink($path);
        //return C('dir_url').'/Public/tmp/'.$file.'.doc';
    }

下載文件的方法

function dowmload($file, $newname = '')
{
//    $filename = getFileUrl($file);
    //C('dir_url') 根目錄
    $filename = C('dir_url') . $file;
    if (!file_exists($filename)) {
        echo '<script>alert("文件不存在");history.go(-1);</script>';
        exit();
    }
    $ua = $_SERVER["HTTP_USER_AGENT"];

    $file_disk = pathinfo($filename, PATHINFO_EXTENSION);
    ob_start();
    if ($newname == '') {
        $newname = date('Ymd-H:i:s');
    }
    $file_name = $newname . '.' . $file_disk;

    /**解決中文亂碼**/
    $encoded_filename = urlencode($file_name);
    $encoded_filename = str_replace("+", "%20", $encoded_filename);

    //兼容IE11
    if (preg_match("/MSIE/", $ua) || preg_match("/Trident\/7.0/", $ua)) {
        header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
    } else if (preg_match("/Firefox/", $ua)) {
        header('Content-Disposition: attachment; filename*="utf8\'\'' . $encoded_filename . '"');
    } else {
        header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
    }
    /**解決中文亂碼** end**/


    header('Content-Type: application/octet-stream');
    header('Accept-Ranges : bytes');
//    header("Content-Disposition:attachment;filename={$newname}.{$file_disk}");//聲明作為附件處理和下載后文件的名稱
    $size = readfile($filename);
    header("Accept-Length :" . $size);
}

然后發(fā)現(xiàn)下載的word文檔打開會默認(rèn)web板式瀏覽,改回頁面視圖只要把你富文本的html放在下面html內(nèi)就OK啦!就這么簡單!

          <html xmlns:o="urn:schemas-microsoft-com:office:office"  xmlns:w="urn:schemas-microsoft-com:office:word"  xmlns="http://www.w3.org/TR/REC-html40">
              <head>
                   <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
                   <xml><w:WordDocument><w:View>Print</w:View></xml>
                   <script src="includes/js/ztree/js/jquery-1.4.4.min.js" type="text/javascript"></script>
            </head>

          <html>
image.png

如果想要在特定的模板內(nèi)插入富文本內(nèi)容可以先預(yù)設(shè)好模板的HTML在把富文本html插入預(yù)設(shè)好的html內(nèi)就行了!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容