首頁  >  文章  >  後端開發  >  php如何實作html轉換word?

php如何實作html轉換word?

coldplay.xixi
coldplay.xixi原創
2020-07-11 15:22:264674瀏覽

php實作html轉換word的方法:1、透過mnt介質,產生word,程式碼為【composer require cshaptx4869/html2word】;2、html檔案直接寫入word,且將圖片轉為base64格式。

php如何實作html轉換word?

php實作html轉換word的方法:

1、透過mnt這個介質,產生word

composer require cshaptx4869/html2word
<?php
/**
 * @desc 方法一、生成word文档
 * @param $content
 * @param string $fileName
 */
function createWord($content = &#39;&#39;, $fileName = &#39;&#39;)
{
    if (empty($content)) {
        return;
    }
    $content=&#39;<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">
            <meta charset="UTF-8" />&#39;.$content.&#39;</html>&#39;;
    if (empty($fileName)) {
        $fileName = date(&#39;YmdHis&#39;).&#39;.doc&#39;;
    }
    file_put_contents($fileName, $content);
}

2、html檔案直接寫入word

注意:如果有圖片的話,轉為base64格式

<?php/**
 * @desc 方法二、生成word文档并下载
 * @param $content
 * @param string $fileName
 */
function downloadWord($content, $fileName=&#39;&#39;){
    if(empty($content)){
        return;
    }
    if (empty($fileName)) {
        $fileName = date(&#39;YmdHis&#39;).&#39;.doc&#39;;
    }    // header("location:xxx.doc");
    header("Cache-Control: no-cache, must-revalidate");
    header("Pragma: no-cache");
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename={$fileName}");
    $html = &#39;<html xmlns:v="urn:schemas-microsoft-com:vml"
         xmlns:o="urn:schemas-microsoft-com:office:office"
         xmlns:w="urn:schemas-microsoft-com:office:word" 
         xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" 
         xmlns="http://www.w3.org/TR/REC-html40">&#39;;
    $html .= &#39;<head><meta http-equiv="Content-Type" content="text/html;charset="UTF-8" /></head>&#39;;
    echo $html . &#39;<body>&#39;.$content .&#39;</body></html>&#39;;
}
createWord(file_get_contents(&#39;html2word.html&#39;));
downloadWord(file_get_contents(&#39;html2word.html&#39;));

相關學習推薦:PHP程式設計從入門到精通

以上是php如何實作html轉換word?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn