100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > html转word出现小插插 记录利用phpword插件实现word文档转html的方法示例

html转word出现小插插 记录利用phpword插件实现word文档转html的方法示例

时间:2024-01-28 03:15:14

相关推荐

html转word出现小插插 记录利用phpword插件实现word文档转html的方法示例

最近一段时间一直在研究利用php脚本让word文档转html的方法,下面就记录了一个利用php的插件phpword实现word文档转html的方法。

安装phpword

通过下面的命令来安装 phpword插件composerrequirephpoffice/phpword

phpword实现 word 转 html 的方法示例$info='ee.docx';

//加载word文件并通过getSections获取word文档的全部元素

$sections=\PhpOffice\PhpWord\IOFactory::load($info)->getSections();

//定义html变量用于存储word文本内容

$html='';

//循环所有元素

foreach($sectionsas$section){

//获取当前元素的所有子元素

$elements=$section->getElements();

//循环当前子元素

foreach($elementsas$eky=>$evl){

$html.='

';

if($evlinstanceofPhpOffice\PhpWord\Element\TextRun){//判断是否普通文本

$content_elements=$evl->getElements();

foreach($content_elementsas$eky2=>$evl2){

$html.=elementHandler($evl2,$evl);

}

}elseif($evlinstanceofPhpOffice\PhpWord\Element\PreserveText){//判断是否保留元素(如自动生成链接的网址元素)

$data=$evl->getText();

$find=array('{','HYPERLINK','}','','"','f','g');

$replace='';

$resText=str_replace($find,$replace,$data);

if(isset($resText)){

$html.=$resText[0];

}

}elseif($evlinstanceofPhpOffice\PhpWord\Element\Table){

$all_table_elements=$evl->getRows();

$html.='

foreach($all_table_elementsas$tky=>$tvl){

$html.='

';

$all_table_cells=$tvl->getCells();

foreach($all_table_cellsas$cky=>$cvl){

$cell_elements=$cvl->getElements();

//获取表格宽度(返回单位为:缇)

$td_width=$cvl->getWidth();

$td_width_px=round($cvl->getWidth()/15,0);

$html.='

';

foreach($cell_elementsas$cl){

//判断当存在elements属性时执行

if(property_exists($cl,'elements')){

$content_elements=$cl->getElements();

foreach($content_elementsas$eky2=>$evl2){

$html.=elementHandler($evl2,$cl);

}

}

}

$html.='

';

}

$html.='

';

}

$html.='

';

}

$html.='

';

}

}

//保存为html文件

file_put_contents('2.html',$html);

//输出html内容到浏览器

echo$html;

functionelementHandler($end_element,$parent_element)

{

$html='';

if($end_elementinstanceofPhpOffice\PhpWord\Element\Text){//判断是否普通文本

$style=$end_element->getFontStyle();

//$fontFamily=mb_convert_encoding($style->getName(),'GBK','UTF-8');

$fontFamily=$style->getName();

$fontSize=$style->getSize()?($style->getSize()/72)*96:'';

$isBold=$style->isBold();

$fontcolor=$style->getColor();

$styleString='';

$fontFamily&&$styleString.="font-family:{$fontFamily};";

$fontSize&&$styleString.="font-size:{$fontSize}px;";

$isBold&&$styleString.="font-weight:bold;";

$fontcolor&&$styleString.="color:{$fontcolor};";

$html.=sprintf('%s',

$styleString,$end_element->getText()

//mb_convert_encoding($evl2->getText(),'GBK','UTF-8')

);//dump($end_element->getText());

}elseif($end_elementinstanceofPhpOffice\PhpWord\Element\Link){//判断是否链接

$style=$end_element->getFontStyle();

//$fontFamily=mb_convert_encoding($style->getName(),'GBK','UTF-8');

$fontFamily=$style->getName();

$fontSize=$style->getSize()?($style->getSize()/72)*96:'';

$isBold=$style->isBold();

$fontcolor=$style->getColor();

$styleString='';

$fontFamily&&$styleString.="font-family:{$fontFamily};";

$fontSize&&$styleString.="font-size:{$fontSize}px;";

$isBold&&$styleString.="font-weight:bold;";

$fontcolor&&$styleString.="color:{$fontcolor};";

$html.=sprintf('%s',

$end_element->getSource(),$styleString,$end_element->getText()

//mb_convert_encoding($evl2->getText(),'GBK','UTF-8')

);

}elseif($end_elementinstanceofPhpOffice\PhpWord\Element\Image){//判断是否图片

//可以在这里执行自定义方法将图片上传到OSS或者图片服务器哈

$imageDataTmp=$end_element->getImageStringData(true);

$imageType=$end_element->getImageType()?$end_element->getImageType():'image/jpg';

$imageData='data:'.$imageType.';base64,'.str_replace(array("\r\n","\r","\n"),"",$imageDataTmp);

//保存文件

//$imageSrc='./uploads/'.md5($end_element->getSource()).'.'.$end_element->getImageExtension();

//file_put_contents($imageSrc,base64_decode(explode(',',$imageData)[1]));

$html.='';

}

return$html;

}

PS:上面的代码只是大致的把逻辑思路,各位如果使用可自行修改代码!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。