站长图库向大家介绍了百度编辑器,word文件转html等相关知识,希望对您有所帮助
对于很多网络编辑小白来讲,经常会遇到网站富媒体编辑器,比如百度编辑器,wangeditor编辑器等不会使用,但是word用的很溜的情况,这篇教程教大家二次开发百度编辑器上传word文件转为html的方法,希望对大家有所帮助!
首先:后台上传word转html到编辑器中,编辑器是使用的百度编辑器,先在页面合适的位置写个上传框
<button type="button" name="fileword" id="upload-fileword">上传word文件</button>
前端用的LayUI框架,不需要写提示框,图个方便,大家自行选择
<script>layui.use('upload', function () { var upload = layui.upload; upload.render({ elem:'#upload-fileword', accept:'file', url: '".url('upload/uploadword',['file'=>'file'])."', exts: 'docx', done: function(data){ if(data.status == 1){ UE.getEditor('".$field."').setContent(data.html); layer.msg(data.msg,{icon:1,time:1500,shade: 0.1,}); }else{ layer.msg(data.msg,{icon:2,time:1500,shade: 0.1,}); } } });});</script>php上传代码
$upload = new \tool\WordUpload(input('file'));$html = $upload->getHtml();if ($html === false) { return json(['status' => 2, 'html' => $html, 'msg' => '解析失败']);}else{ return json(['status' => 1, 'html' => $html, 'msg' => '解析成功']);}WordUpload文件 需要phpword包 自己下载
<?phpnamespace tool;use PhpOffice\PhpWord\PhpWord;use PhpOffice\PhpWord\IOFactory;use PhpOffice\PhpWord\Style\Font;use PhpOffice\PhpWord\Shared\ZipArchive;use PhpOffice\PhpWord\Settings;use PhpOffice\PhpWord\Shared\Converter;use PhpOffice\PhpWord\Style\TablePosition;use think\facade\Env;use think\facade\Config;use think\File;use think\Db; /*** Class WordUpload* [url=home.php?mod=space&uid=1507498]@Package[/url] app\common\util* word 文档上传*/class WordUpload{ private $file; private $size; private $ext; private $savePath = '/upload/word/'; private $errorMsg; private $delTmp = true; /** * WordUpload constructor. * [url=home.php?mod=space&uid=952169]@Param[/url] $file [上传的文件] * @param int $size [文件大小] * @param string $ext [允许的后缀] */ public function __construct($file, $size = 1024, $ext = 'docx') { $this->file = $file; $this->size = $size; $this->ext = $ext; } public function getHtml(){ $file = request()->file($this->file); if (!$file instanceof File) { $this->errorMsg = '请上传文件'; return false; } //上传文件 根据站点选择目录 $info = $file->validate(['size'=>$this->size,'ext'=>$this->ext])->move(Env::get('ROOT_PATH').'public/static'. $this->savePath); if(!$info){ // 上传失败获取错误信息 $this->errorMsg = $file->getError(); return false; } try { //获取文件路径 $tempDocx = Env::get('ROOT_PATH').'public/static'.$this->savePath.$info->getSaveName(); $objWriter = IOFactory::createWriter(IOFactory::load($tempDocx), 'HTML'); $tempHtml = Env::get('ROOT_PATH') . 'public/static'.$this->savePath .substr($info->getSaveName(), 0, strpos($info->getSaveName(), '.')) . '.html'; $objWriter->save($tempHtml); $html = file_get_contents($tempHtml); if ($this->delTmp) { //删除临时文件 register_shutdown_function(function () use ($tempHtml, $tempDocx){ unlink($tempHtml); unlink($tempDocx); }); } $html = $this->getHtmlBody($html); if ($html == '') { throw new \Exception('上传文件内容为空');

