惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
S
SegmentFault 最新的问题
V
Visual Studio Blog
J
Java Code Geeks
宝玉的分享
宝玉的分享
美团技术团队
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hackread – Cybersecurity News, Data Breaches, AI and More
有赞技术团队
有赞技术团队
量子位
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
Google DeepMind News
Google DeepMind News
Jina AI
Jina AI
博客园 - 叶小钗
月光博客
月光博客
P
Proofpoint News Feed
D
DataBreaches.Net
Blog — PlanetScale
Blog — PlanetScale
博客园_首页
腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
Stack Overflow Blog
Stack Overflow Blog

博客园 - 赵瑛

如何把一个js的代码变成一个.min.js的文件 thinkphp8有一个木马文件链接 thinkphp 可以居中的分页css样式 thinkphp8无法显示验证码的原因之一 vue点击复制 vue简单分页 vue无限加载 mpdf增加字体并进行使用 jquery导出word css清除浮动最好的方法 Elasticsearch同时实现and与or查询 Elasticsearch查询指定字段是否有重复数据 查询并删除Elasticsearch库中指定字段重复的数据 elasticsearch进行and,or多条件组合查询 elasticsearch中的and和or php FTP操作类( 拷贝、移动、删除文件/创建目录 ) mysql重置自增id js 双指放大、拖动图片 php操作elasticsearch数据库的方法
php将内容转为word
赵瑛 · 2024-11-06 · via 博客园 - 赵瑛

第一步,先对内容进行处理,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,'http://www.cdlyh.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');