网页内容写入word文档
来源:互联网 发布:mac ftpserver工具 编辑:程序博客网 时间:2024/05/03 17:13
/**
* 根据HTML代码获取word文档内容
* 创建一个本质为mht的文档,该函数会分析文件内容并从远程下载页面中的图片资源
* 该函数依赖于类WordMake
* 该函数会分析img标签,提取src的属性值。但是,src的属性值必须被引号包围,否则不能提取
*
* @param string $content HTML内容
* @param string $absolutePath 网页的绝对路径。如果HTML内容里的图片路径为相对路径,那么就需要填写这个参数,来让该函数自动填补成绝对路径。这个参数最后需要以/结束
* @param bool $isEraseLink 是否去掉HTML内容中的链接
*/
function
WordMake(
$content
,
$absolutePath
=
""
,
$isEraseLink
= true )
{
import(
"@.Util.Wordmaker"
);
$mht
=
new
Wordmaker();
if
(
$isEraseLink
){
$content
= preg_replace(
'/<a\s*.*?\s*>(\s*.*?\s*)<\/a>/i'
,
'$1'
,
$content
);
//去掉链接
}
$images
=
array
();
$files
=
array
();
$matches
=
array
();
//这个算法要求src后的属性值必须使用引号括起来
if
( preg_match_all(
'/<img[.\n]*?src\s*?=\s*?[\"\'](.*?)[\"\'](.*?)\/>/i'
,
$content
,
$matches
) ){
$arrPath
=
$matches
[1];
for
(
$i
=0;
$i
<
count
(
$arrPath
);
$i
++)
{
$path
=
$arrPath
[
$i
];
$imgPath
= trim(
$path
);
if
(
$imgPath
!=
""
)
{
$files
[] =
$imgPath
;
if
(
substr
(
$imgPath
,0,7) ==
'http://'
)
{
//绝对链接,不加前缀
}
else
{
$imgPath
=
$absolutePath
.
$imgPath
;
}
$images
[] =
$imgPath
;
}
}
}
$mht
->AddContents(
"tmp.html"
,
$mht
->GetMimeType(
"tmp.html"
),
$content
);
for
(
$i
=0;
$i
<
count
(
$images
);
$i
++)
{
$image
=
$images
[
$i
];
if
( @
fopen
(
$image
,
'r'
) )
{
$imgcontent
= @
file_get_contents
(
$image
);
if
(
$content
)
$mht
->AddContents(
$files
[
$i
],
$mht
->GetMimeType(
$image
),
$imgcontent
);
}
else
{
echo
"file:"
.
$image
.
" not exist!<br />"
;
}
}
return
$mht
->GetFile();
}
//逻辑代码
$data
=
$this
->db->where(
array
(
'id'
=>1))->find();
//从数据库查询一条数据
$this
->assign(
$data
);
//把获取的数据传递的模板,替换模板里面的变量
$content
=
$this
->fetch(
'word'
);
//获取模板内容信息word是模板的名称
$fileContent
= WordMake(
$content
);
//生成word内容
$name
= iconv(
"utf-8"
,
"GBK"
,
$data
[
'username'
]);
//转换好生成的word文件名编码
$fp
=
fopen
(
$_path
.
$name
.
'['
.
$data
[
'number'
].
"].doc"
,
'w'
);
//打开生成的文档
fwrite(
$fp
,
$fileContent
);
//写入包保存文件
fclose(
$fp
);
0 0
- 网页内容写入word文档
- 将html内容写入word文档
- 网页内容导出word文档jquery插件
- 如何把数据库的内容写入到Word文档中
- 如何把数据库的内容写入到Word文档中?
- 实现Freemarker生成word文档,内容可二次写入
- 将网页内容导出到word文档中
- ASP.NET C#如何读取word,写入word,复制内容到另一个word文档,批量修改文件名
- 把word文档写入Sqlserver
- 向服务器端写入word文档
- 将网页内容写入本地
- 将内容写入xml文档
- 将网页内容转换成word或excel文档的方法(c#.net)
- 将网页内容转换成word或excel文档的方法(c#.net)
- php读取word\pdf等文档的内容,并将其保存到网页中
- 使用dom4j将动态生成内容的网页导出为word文档
- c#.net 将网页内容转换成word或excel文档
- c# 抽取word文档内容
- Floodlight源码阅读之网络拓扑
- 第三十九讲 项目3 求出100万以内的n,9*n刚好是它的反序数。
- CSS中不为人知Zoom属性的使用介绍(IE浏览器不得不说的坑)
- linux 安装jdk
- 【lotus Notes/Source Insight/Notepad++】软件开发常用工具设置技巧,提高工作编码效率
- 网页内容写入word文档
- Spark Streaming性能优化系列-如何获得和持续使用足够的集群计算资源?
- Android Handler消息机制原理及总结
- mysql在线备份遇到的问题总结
- 设计模式之建造者模式(C++实现)
- 使用latex撰写elsevier论文,latex表格,插图以及调用的安装包
- JAVA I/O知识梳理
- SwipeRefreshLoadLayout + RecyclerView 实现下列刷新,上拉加载
- hjr教程-JAVA(二):JAVA连接MySql