This commit is contained in:
2025-12-16 14:48:18 +08:00
parent 0dc9ca86f7
commit e8a38f493e
+33 -45
View File
@@ -264,10 +264,10 @@ class SkuController extends BaseController
return ''; return '';
} }
// 1. 加载 docx // 1. 读取 docx
$phpWord = WordIOFactory::load($docxPath, 'Word2007'); $phpWord = WordIOFactory::load($docxPath);
// 2. 临时目录(非常重要) // 2. 导出 HTML
$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,40 +275,40 @@ class SkuController extends BaseController
$tmpHtml = $tmpDir . '/index.html'; $tmpHtml = $tmpDir . '/index.html';
// 3. 导出 HTML
$writer = WordIOFactory::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(关键) // 3. 只取 body 内容(关键)
if (preg_match('/<body[^>]*>(.*?)<\/body>/is', $html, $m)) {
$html = $m[1];
}
// 4. 清洗 Word 垃圾样式(关键)
$html = $this->cleanWordHtml($html); $html = $this->cleanWordHtml($html);
// 5. 处理图片并上传 OSS
$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 ($matches) use ($tmpDir, $uploadService) {
$src = $matches[1]; $src = $matches[1];
// 只处理本地图片 // Word 导出的是相对路径
if (preg_match('#^(http|https|data:)#i', $src)) { if (!str_starts_with($src, 'http')) {
return $matches[0]; $localImg = $tmpDir . '/' . ltrim($src, '/');
if (is_file($localImg)) {
$ossUrl = $uploadService->uploadLocalFile([
'path' => $localImg,
'scene' => 'desc'
]);
return preg_replace('/src="[^"]+"/', 'src="' . $ossUrl . '"', $matches[0]);
}
} }
$localImg = realpath($tmpDir . '/' . $src); return $matches[0];
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
); );
@@ -319,44 +319,32 @@ class SkuController extends BaseController
return trim($html); return trim($html);
} }
function deleteDir(string $dir) function deleteDir(string $dir): void
{ {
if (!is_dir($dir)) return; if (!is_dir($dir)) return;
foreach (scandir($dir) as $file) { foreach (scandir($dir) as $file) {
if ($file === '.' || $file === '..') continue; if ($file === '.' || $file === '..') continue;
$path = $dir . '/' . $file; $path = $dir . '/' . $file;
is_dir($path) ? $this->deleteDir($path) : unlink($path); is_dir($path) ? deleteDir($path) : @unlink($path);
} }
rmdir($dir); @rmdir($dir);
} }
function cleanWordHtml(string $html): string function cleanWordHtml(string $html): string
{ {
// 删除 Word 注释 // 移除 Office 命名空间、样式
$html = preg_replace('/<!--(.|\s)*?-->/', '', $html);
// 删除 style / meta / link
$html = preg_replace('/<(style|meta|link)[^>]*>.*?<\/\1>/is', '', $html);
// 删除 xml 标签
$html = preg_replace('/<xml>.*?<\/xml>/is', '', $html);
// 删除 mso-xxx 样式
$html = preg_replace('/\s*mso-[^:]+:[^;"]+;?/i', '', $html); $html = preg_replace('/\s*mso-[^:]+:[^;"]+;?/i', '', $html);
$html = preg_replace('/\s*class="[^"]*"/i', '', $html);
$html = preg_replace('/\s*style="[^"]*"/i', '', $html);
// 清理 class="Mso..." // 移除多余标签
$html = preg_replace('/class="Mso[^"]*"/i', '', $html); $html = preg_replace('/<(\/?)(html|head|meta|link|span|font|xml|o:p)[^>]*>/i', '', $html);
// 删除空 span // 清理空段落
$html = preg_replace('/<span[^>]*>\s*<\/span>/i', '', $html); $html = preg_replace('/<p>\s*<\/p>/i', '', $html);
// 删除 Office 条件注释 // 规范段落
$html = preg_replace('/<!\[if.*?\]>.*?<!\[endif\]>/is', '', $html); $html = str_replace(['<p>', '</p>'], ["<p>", "</p>\n"], $html);
// 只保留 body 内容
if (preg_match('/<body[^>]*>(.*?)<\/body>/is', $html, $m)) {
$html = $m[1];
}
return $html; return $html;
} }