* Created by PhpStorm.
* User: parker
* Date: 2020/10/18
* Time: 16:09*/namespace common\services;class WordService extendsBaseService
{public static function importWord($info)
{$word = self::getWord($info['path']);
dd($word);
}/**
* 获取word文档内容
* @param string $path
* @return array*/
public static function getWord($path = '')
{//加载word文档,使用phpword处理
$phpWord = \PhpOffice\PhpWord\IOFactory::load($path);return self::getNodeContent($phpWord);
}/**
* 根据word主节点获取分节点内容
* @param $word
* @return array*/
public static function getNodeContent($word)
{$return =[];//分解部分
foreach ($word->getSections() as $section)
{if ($sectioninstanceof \PhpOffice\PhpWord\Element\Section) {//分解元素
foreach ($section->getElements() as $element)
{//文本元素
if ($elementinstanceof \PhpOffice\PhpWord\Element\TextRun) {$text = '';foreach ($element->getElements() as $ele) {$text .= self::getTextNode($ele);
}$return[] = $text;
}//表格元素
else if ($elementinstanceof \PhpOffice\PhpWord\Element\Table) {foreach ($element->getRows() as $ele)
{$return[] = self::getTableNode($ele);
}
}
}
}
}return $return;
}/**
* 获取文档节点内容
* @param $node
* @return string*/
public static function getTextNode($node)
{$return = '';//处理文本
if ($nodeinstanceof \PhpOffice\PhpWord\Element\Text)
{$return .= $node->getText();
}//处理图片
else if ($nodeinstanceof \PhpOffice\PhpWord\Element\Image)
{$return .= self::pic2text($node);
}//处理文本元素
else if ($nodeinstanceof \PhpOffice\PhpWord\Element\TextRun) {foreach ($node->getElements() as $ele) {$return .= self::getTextNode($ele);
}
}return $return;
}/**
* 获取表格节点内容
* @param $node
* @return string*/
public static function getTableNode($node)
{$return = '';//处理行
if ($nodeinstanceof \PhpOffice\PhpWord\Element\Row) {foreach ($node->getCells() as $ele)
{$return .= self::getTableNode($ele);
}
}//处理列
else if ($nodeinstanceof \PhpOffice\PhpWord\Element\Cell) {foreach ($node->getElements() as $ele)
{$return .= self::getTextNode($ele);
}
}return $return;
}/**
* 处理word文档中base64格式图片
* @param $node
* @return string*/
public static function pic2text($node)
{//获取图片编码
$imageData = $node->getImageStringData(true);//添加图片html显示标头
$imageData = 'data:' . $node->getImageType() . ';base64,' . $imageData;$return = '';return $return;
}/**
* 处理word文档中base64格式图片
* @param $node
* @return string*/
public static function pic2file($node)
{//图片地址(一般为word文档地址+在word中的锚点位置)
$imageSrc = 'images/' . md5($node->getSource()) . '.' . $node->getImageExtension();$imageData = $node->getImageStringData(true);//将图片保存在本地
file_put_contents($imageSrc, base64_decode($imageData));return $imageSrc;
}/**
* 将word转化为html(转换存储html文件后展示)
* @param $path
* @throws \PhpOffice\PhpWord\Exception\Exception*/
public static function word2html($path)
{$phpWord = FileImportService::getOne($path);//转为html处理
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, "HTML");$path = pathinfo($path);$fileName = $path['dirname'] . '/' . $path['filename'] . '.html';$xmlWriter->save($fileName);$html = file_get_contents($fileName);echo $html;die;
}
}