php将内容转为word
第一步,先对内容进行处理,html的内容不适合word,在使用phpword转换时,只接受p和span元素,其它元素都要清除掉$modifiedHtml 为内容
$relust = check_table($modifiedHtml,'table'); // 判断是否存在table元素
if($relust == 1){
$modifiedHtml = table_to_div($modifiedHtml); //将内容中所有的table元素替换为p元素
}
$modifiedHtml = cleanAndReplaceHtmlWithBr($modifiedHtml);// 将内容中所有的段落标签都替换为p标签,并清除其它标签,只保留<p><img><a>这三种标签。
$modifiedHtml = imgadddomain($modifiedHtml,'https://syjx.ihwrm.com');// 给本地图片的路径前面加上指定的域名
$modifiedHtml = replaceAllImagesWithPlaceholder($modifiedHtml);//替换无效图片
$modifiedHtml = replaceInvalidImages($modifiedHtml);//判断内容中图片是否为'jpg', 'jpeg', 'gif', 'png'
$modifiedHtml = removeImgAttributesExceptSrc($modifiedHtml);//1 顺序不能错 移除 <img> 标签中的所有属性,除了 src 属性
$modifiedHtml = modifyHtmlImages($modifiedHtml);//2 判断图片宽是否超过600,超过的就限制在600,没有600的就不用管
$modifiedHtml = removeNestedPElements($modifiedHtml);//3 移除多余的p标签
$modifiedHtml = removeUnmatchedClosingParagraphTags($modifiedHtml);//4 清除没有对应的p元素
$modifiedHtml = extractBodyContent($modifiedHtml);//5 提取 <body> 和 </body> 之间的内容
$modifiedHtml = ensureImgTagsAreClosed($modifiedHtml);//6 用php判断文章内容中所有的img标签是否闭合,没有闭合的添加一下闭合
第二步
$title =标题;
$content = 内容;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\Html;
$phpWord = new PhpWord();
$phpWord->addFontStyle('cStyle', array('size' => 12,'name' => '宋体'));//内容样式
$phpWord->addFontStyle('cStyle1', array('size' => 16,'name' => '宋体','color' => '#333'));//内容样式
$phpWord->addFontStyle('cStyle2', array('size' => 12,'name' => '宋体','color' => '#ff0000'));//链接样式
$phpWord->addFontStyle('bStyle', array('size' => 12, 'bold' => true, 'name' => '宋体'));//加粗样式
$phpWord->addFontStyle('titlestyle', array('bold' => true,'size' => 18,'name' => '宋体'));//标题的样式
$section = $phpWord->addSection();
$section->addTextBreak(2);
$section->addText($title,'titlestyle', ['alignment' => 'center']);
$section->addTextBreak(2);
//$section->addText($content,'cStyle1', ['alignment' => 'left']);
Html::addHtml($section, $content, false, false);
$section->addTextBreak(5);
$section->addText($datetime,'bStyle', ['alignment' => 'right']);
$section->addTextBreak(5);
$linkText = '本文链接:'.$link;
$linkUrl = $link;
$section->addLink($linkUrl, $linkText,'cStyle2');
header('pragma:public');
header('Content-type:application/vnd.ms-word;charset=utf-8;name="'.$file_name.'"');
header("Content-Disposition:attachment;filename='.$file_name.'");//attachment新窗口打印inline本窗口打印
header( 'Content-Type: image/jpeg');
ob_clean();
flush();
$writer = IOFactory::createWriter($phpWord, 'Word2007');
$writer->save($_SERVER['DOCUMENT_ROOT'].'/'.$file_path.'/'.$file_name);
//向浏览器输出下载
$writer->save('php://output');
来源:https://www.cnblogs.com/zhaoying/p/18529536
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页:
[1]