php如何实现html转换word?

coldplay.xixi
发布: 2020-07-11 15:22:26
原创
5104人浏览过
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 = '', $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免费学习笔记(深入)”;

<?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-revalidate");
    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?的详细内容,更多请关注php中文网其它相关文章!

WPS零基础入门到精通全套教程!
WPS零基础入门到精通全套教程!

全网最新最细最实用WPS零基础入门到精通全套教程!带你真正掌握WPS办公! 内含Excel基础操作、函数设计、数据透视表等

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号