• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php如何实现html转换word?

php 搞代码 4年前 (2022-01-04) 36次浏览 已收录 0个评论

php实现html转换word的方法:1、通过mnt介质,生成word,代码为【composer require cshaptx4869/html2word】;2、html文件直接写入word,且将图片转为base64格式。

php实现html转换word的方法:

1、通过mnt这个介质,生成word

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

2、html文件直接写入word

注意:如果有图片的话,转为base64格式

<?php/**
 * @desc 方法二、生成word文档并下载
 * @param $content
 * @param string $fileName
 */
function downloadWord($content, $fileName=''){
    if(empty($content)){
        return;
    }
    if (empty($fileName)) {
        $fileName = date('YmdHis').'.doc';
    }    // header("location:xxx.doc");
    header("Cache-Control: no-cache, must-rev<mark style="color:transparent">来源gaodaimacom搞#代%码网</mark>alidate");
    header("Pragma: no-cache");
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename={$fileName}");
    $html = '<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">';
    $html .= '<head><meta http-equiv="Content-Type" content="text/html;charset="UTF-8" /></head>';
    echo $html . '<body>'.$content .'</body></html>';
}
createWord(file_get_contents('html2word.html'));
downloadWord(file_get_contents('html2word.html'));

相关学习推荐:PHP编程从入门到精通

以上就是php如何实现html转换word?的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php如何实现html转换word?
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址