导入
This commit is contained in:
@@ -264,10 +264,8 @@ class SkuController extends BaseController
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. 加载 docx
|
$phpWord = \PhpOffice\PhpWord\IOFactory::load($docxPath);
|
||||||
$phpWord = WordIOFactory::load($docxPath, 'Word2007');
|
|
||||||
|
|
||||||
// 2. 临时目录(非常重要)
|
|
||||||
$tmpDir = runtime_path('tmp/' . uniqid());
|
$tmpDir = runtime_path('tmp/' . uniqid());
|
||||||
if (!is_dir($tmpDir)) {
|
if (!is_dir($tmpDir)) {
|
||||||
mkdir($tmpDir, 0755, true);
|
mkdir($tmpDir, 0755, true);
|
||||||
@@ -275,50 +273,77 @@ class SkuController extends BaseController
|
|||||||
|
|
||||||
$tmpHtml = $tmpDir . '/index.html';
|
$tmpHtml = $tmpDir . '/index.html';
|
||||||
|
|
||||||
// 3. 导出 HTML
|
$writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
|
||||||
$writer = WordIOFactory::createWriter($phpWord, 'HTML');
|
|
||||||
$writer->save($tmpHtml);
|
$writer->save($tmpHtml);
|
||||||
|
|
||||||
$html = file_get_contents($tmpHtml);
|
$html = file_get_contents($tmpHtml);
|
||||||
|
|
||||||
// 4. 清洗 Word 垃圾 HTML(关键)
|
// 1️⃣ 上传图片到 OSS
|
||||||
$html = $this->cleanWordHtml($html);
|
|
||||||
|
|
||||||
$uploadService = new UploadService();
|
$uploadService = new UploadService();
|
||||||
|
|
||||||
// 5. 处理图片
|
|
||||||
$html = preg_replace_callback(
|
$html = preg_replace_callback(
|
||||||
'/<img[^>]+src="([^"]+)"[^>]*>/i',
|
'/<img[^>]+src="([^"]+)"[^>]*>/i',
|
||||||
function ($matches) use ($tmpDir, $uploadService) {
|
function ($m) use ($tmpDir, $uploadService) {
|
||||||
$src = $matches[1];
|
$src = $m[1];
|
||||||
|
|
||||||
// 只处理本地图片
|
if (!str_starts_with($src, 'http')) {
|
||||||
if (preg_match('#^(http|https|data:)#i', $src)) {
|
$localImg = $tmpDir . '/' . ltrim($src, './');
|
||||||
return $matches[0];
|
if (is_file($localImg)) {
|
||||||
|
$ossUrl = $uploadService->uploadLocalFile([
|
||||||
|
'path' => $localImg,
|
||||||
|
'scene' => 'desc'
|
||||||
|
]);
|
||||||
|
return preg_replace('/src="[^"]+"/', 'src="'.$ossUrl.'"', $m[0]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return $m[0];
|
||||||
$localImg = realpath($tmpDir . '/' . $src);
|
|
||||||
if (!$localImg || !is_file($localImg)) {
|
|
||||||
return $matches[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
// 上传 OSS
|
|
||||||
$ossUrl = $uploadService->uploadLocalFile([
|
|
||||||
'path' => $localImg,
|
|
||||||
'scene' => 'desc'
|
|
||||||
]);
|
|
||||||
|
|
||||||
return str_replace($src, $ossUrl, $matches[0]);
|
|
||||||
},
|
},
|
||||||
$html
|
$html
|
||||||
);
|
);
|
||||||
|
|
||||||
// 6. 清理临时目录
|
// 2️⃣ 关键:HTML 清洗(适配 wangEditor)
|
||||||
|
$html = $this->cleanWordHtmlForWangEditor($html);
|
||||||
|
|
||||||
$this->deleteDir($tmpDir);
|
$this->deleteDir($tmpDir);
|
||||||
|
|
||||||
|
return $html;
|
||||||
|
}
|
||||||
|
|
||||||
|
function cleanWordHtmlForWangEditor(string $html): string
|
||||||
|
{
|
||||||
|
// 1. 移除 Word 专属标签
|
||||||
|
$html = preg_replace('/<o:p>.*?<\/o:p>/is', '', $html);
|
||||||
|
$html = preg_replace('/<\/?xml[^>]*>/i', '', $html);
|
||||||
|
$html = preg_replace('/<\/?v:[^>]*>/i', '', $html);
|
||||||
|
|
||||||
|
// 2. 移除 class / style
|
||||||
|
$html = preg_replace('/\sclass="[^"]*"/i', '', $html);
|
||||||
|
$html = preg_replace('/\sstyle="[^"]*"/i', '', $html);
|
||||||
|
|
||||||
|
// 3. 清理 span(保留内容)
|
||||||
|
while (preg_match('/<span[^>]*>(.*?)<\/span>/is', $html)) {
|
||||||
|
$html = preg_replace('/<span[^>]*>(.*?)<\/span>/is', '$1', $html);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 转换 div → p
|
||||||
|
$html = preg_replace('/<div[^>]*>/i', '<p>', $html);
|
||||||
|
$html = preg_replace('/<\/div>/i', '</p>', $html);
|
||||||
|
|
||||||
|
// 5. img 只保留 src
|
||||||
|
$html = preg_replace('/<img[^>]*src="([^"]+)"[^>]*>/i', '<img src="$1">', $html);
|
||||||
|
|
||||||
|
// 6. table 清洗
|
||||||
|
$html = preg_replace('/<table[^>]*>/i', '<table>', $html);
|
||||||
|
$html = preg_replace('/<td[^>]*>/i', '<td>', $html);
|
||||||
|
$html = preg_replace('/<th[^>]*>/i', '<th>', $html);
|
||||||
|
|
||||||
|
// 7. 去除多余空白
|
||||||
|
$html = preg_replace('/\s+/u', ' ', $html);
|
||||||
|
$html = str_replace(['<p></p>', '<p> </p>'], '', $html);
|
||||||
|
|
||||||
return trim($html);
|
return trim($html);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function cleanWordHtml(string $html): string
|
function cleanWordHtml(string $html): string
|
||||||
{
|
{
|
||||||
// 删除 Word 注释
|
// 删除 Word 注释
|
||||||
|
|||||||
Reference in New Issue
Block a user