欢迎您光临自学哈网,只为分享网络知识教程,供大家学习参考!

「自学哈网」百度编辑器上传word转html

作者 : 自学哈 本文共5327个字,预计阅读时间需要14分钟 2022-10-8 共139人阅读
也想出现在这里? 联系我们

后台上传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 toolWordUpload(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包 自己下载

<?php
namespace tool;
use PhpOfficePhpWordPhpWord;
use PhpOfficePhpWordIOFactory;
use PhpOfficePhpWordStyleFont;
use PhpOfficePhpWordSharedZipArchive;
use PhpOfficePhpWordSettings;
use PhpOfficePhpWordSharedConverter;
use PhpOfficePhpWordStyleTablePosition;
use thinkfacadeEnv;
use thinkfacadeConfig;
use thinkFile;
use thinkDb;
/**
* Class WordUpload
* [url=home.php?mod=space&uid=1507498]@Package[/url] appcommonutil
* 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('上传文件内容为空');
}
$html = $this->saveImage($html);
$html = $this->clearStyle($html);
$html = $this->clearSpan($html);
} catch (Exception $e) {
$this->errorMsg = $e->getMessage();
return false;
}
return $html;
}
/**
* @param $html
* [url=home.php?mod=space&uid=155549]@Return[/url] string
* 匹配出body的内容
*/
private function getHtmlBody($html) {
preg_match('/<body>([sS]*)</body>/', $html,$matches);
return isset($matches[1]) ? $matches[1] : '';
}
/**
* @param $html
* @return mixed
* 图片处理
*/
private function saveImage($html){
//匹配图片
ini_set('pcre.backtrack_limit', 9999999);
preg_match_all('/<img[^>]*src="([sS]*?)"/>/', $html,$imageMatches);
if (!$imageMatches[1]) {
return $html;
}
//print_r($imageMatches[1]);exit;
$imageUrl = [];
foreach ($imageMatches[1] as $image) {
$imageUrl[] = $this->saveLocalBase64Image($image);
}
return str_replace($imageMatches[1], $imageUrl, $html);
}
/**
* @param $base64_image_content
* @return string
* 保存图片到本地
*/
private function saveLocalBase64Image($base64_image_content){
$imge_web_url = '';
if (preg_match('/^(data:s*image/(w+);base64,)/', $base64_image_content, $result)){
//图片后缀
$type = $result[2];
//保存位置--图片名
$image_name = date('His') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT). '.' . $type; //图片名称
$image_file_path = $this->savePath .'image/'.date('Ymd'); //static/upload/word/image/20200522
$image_file = Env::get('ROOT_PATH') .'public/static'. $image_file_path;
$imge_real_url = $image_file . DIRECTORY_SEPARATOR . $image_name;
$imge_web_url = '/static'.$image_file_path .  DIRECTORY_SEPARATOR . $image_name;
if (!file_exists($image_file)){
mkdir($image_file, 0700, true);
}
//解码
$decode = base64_decode(str_replace($result[1], '', $base64_image_content));
$res = file_put_contents($imge_real_url, $decode);
return $imge_web_url;
}
return $imge_web_url;
}
/**
* @param $content
* @return null|string|string[]
* 清除p,span标签的样式
*/
private function clearStyle($content)
{
$patterns = array (
'/<(p|span)[sS]*?>/i',
);
return preg_replace($patterns, '<$1>', $content);
}
/**
* @param $content
* @return null|string|string[]
* 清除span标签
*/
private function clearSpan($content)
{
$patterns = array (
'/<span[sS]*?>/i',
'/</span>/i',
);
return preg_replace($patterns, '', $content);
}
/**
* @return mixed
*/
public function getErrorMsg()
{
return $this->errorMsg;
}
}

相关专题

本站声明:
本站所有资源来源于网络,分享目的仅供大家学习和交流!如若本站内容侵犯了原著者的合法权益,可联系邮箱976157886@qq.com进行删除。
自学哈专注于免费提供最新的分享知识、网络教程、网络技术的资源分享平台,好资源不私藏,大家一起分享!

自学哈网 » 「自学哈网」百度编辑器上传word转html
也想出现在这里? 联系我们
© 2022 Theme by - 自学哈网 & WordPress Theme. All rights reserved 浙ICP备2022016594号