php将网页内容存到word,PHP把网页保存为word文件的三种方法_PHP教程

2023-11-05 18:30

本文主要是介绍php将网页内容存到word,PHP把网页保存为word文件的三种方法_PHP教程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

个人建议:com实例后的方法都需要查找官方文档才知道什么意思,编辑器没有代码提示,非常不方便,另外这个效率也不是很高,不推荐使用

三、利用PHP将内容写入doc文件之中

这个方法又可以分为两种方法

1.生成mht格式(和HTML很相似)写入word

2.纯HTML格式写入word

1)、生成mht格式(和HTML很相似)写入word

复制代码 代码如下:

这个函数的主要功能其实就是分析HTML代码中的所有图片地址,并且依次下载下来。获取到了图片的内容以后,调用MhtFileMaker类,将图片添加到mht文件中。具体的添加细节,封装在MhtFileMaker类中了。

使用方法1:远程调用

复制代码 代码如下:

$url= http://www.***.com;

$content = file_get_contents($url);

$fileContent = getWordDocument($content,"http://www.yoursite.com/Music/etc/");

$fp = fopen("test.doc", 'w');

fwrite($fp, $fileContent);

fclose($fp);

其中,$content变量应该是HTML源代码,后面的链接应该是能填补HTML代码中图片相对路径的URL地址

其中,$content变量应该是HTML源代码,后面的链接应该是能填补HTML代码中图片相对路径的URL地址

使用方法2:本地生成调用

复制代码 代码如下:

header("Cache-Control: no-cache, must-revalidate");

header("Pragma: no-cache");

$wordStr = 'PHP教程网站--jb51.net';

$fileContent = getWordDocument($wordStr);

$fileName = iconv("utf-8", "GBK", ‘PHP教程' . '_'. $intro . '_' . rand(100, 999));

header("Content-Type: application/doc");

header("Content-Disposition: attachment; filename=" . $fileName . ".doc");

echo $fileContent;

注意,在使用这个函数之前,您需要先包含类MhtFileMaker,这个类可以帮助我们生成Mht文档。

复制代码 代码如下:

Description: The class can make .mht file.

***********************************************************************/

class MhtFileMaker{

var $config = array();

var $headers = array();

var $headers_exists = array();

var $files = array();

var $boundary;

var $dir_base;

var $page_first;

function MhtFile($config = array()){

}

function SetHeader($header){

$this->headers[] = $header;

$key = strtolower(substr($header, 0, strpos($header, ':')));

$this->headers_exists[$key] = TRUE;

}

function SetFrom($from){

$this->SetHeader("From: $from");

}

function SetSubject($subject){

$this->SetHeader("Subject: $subject");

}

function SetDate($date = NULL, $istimestamp = FALSE){

if ($date == NULL) {

$date = time();

}

if ($istimestamp == TRUE) {

$date = date('D, d M Y H:i:s O', $date);

}

$this->SetHeader("Date: $date");

}

function SetBoundary($boundary = NULL){

if ($boundary == NULL) {

$this->boundary = '--' . strtoupper(md5(mt_rand())) . '_MULTIPART_MIXED';

} else {

$this->boundary = $boundary;

}

}

function SetBaseDir($dir){

$this->dir_base = str_replace("\\", "/", realpath($dir));

}

function SetFirstPage($filename){

$this->page_first = str_replace("\\", "/", realpath("{$this->dir_base}/$filename"));

}

function AutoAddFiles(){

if (!isset($this->page_first)) {

exit ('Not set the first page.');

}

$filepath = str_replace($this->dir_base, '', $this->page_first);

$filepath = 'http://mhtfile' . $filepath;

$this->AddFile($this->page_first, $filepath, NULL);

$this->AddDir($this->dir_base);

}

function AddDir($dir){

$handle_dir = opendir($dir);

while ($filename = readdir($handle_dir)) {

if (($filename!='.') && ($filename!='..') && ("$dir/$filename"!=$this->page_first)) {

if (is_dir("$dir/$filename")) {

$this->AddDir("$dir/$filename");

} elseif (is_file("$dir/$filename")) {

$filepath = str_replace($this->dir_base, '', "$dir/$filename");

$filepath = 'http://mhtfile' . $filepath;

$this->AddFile("$dir/$filename", $filepath, NULL);

}

}

}

closedir($handle_dir);

}

function AddFile($filename, $filepath = NULL, $encoding = NULL){

if ($filepath == NULL) {

$filepath = $filename;

}

$mimetype = $this->GetMimeType($filename);

$filecont = file_get_contents($filename);

$this->AddContents($filepath, $mimetype, $filecont, $encoding);

}

function AddContents($filepath, $mimetype, $filecont, $encoding = NULL){

if ($encoding == NULL) {

$filecont = chunk_split(base64_encode($filecont), 76);

$encoding = 'base64';

}

$this->files[] = array('filepath' => $filepath,

'mimetype' => $mimetype,

'filecont' => $filecont,

'encoding' => $encoding);

}

function CheckHeaders(){

if (!array_key_exists('date', $this->headers_exists)) {

$this->SetDate(NULL, TRUE);

}

if ($this->boundary == NULL) {

$this->SetBoundary();

}

}

function CheckFiles(){

if (count($this->files) == 0) {

return FALSE;

} else {

return TRUE;

}

}

function GetFile(){

$this->CheckHeaders();

if (!$this->CheckFiles()) {

exit ('No file was added.');

}

$contents = implode("\r\n", $this->headers);

$contents .= "\r\n";

$contents .= "MIME-Version: 1.0\r\n";

$contents .= "Content-Type: multipart/related;\r\n";

$contents .= "\tboundary=\"{$this->boundary}\";\r\n";

$contents .= "\ttype=\"" . $this->files[0]['mimetype'] . "\"\r\n";

$contents .= "X-MimeOLE: Produced By Mht File Maker v1.0 beta\r\n";

$contents .= "\r\n";

$contents .= "This is a multi-part message in MIME format.\r\n";

$contents .= "\r\n";

foreach ($this->files as $file) {

$contents .= "--{$this->boundary}\r\n";

$contents .= "Content-Type: $file[mimetype]\r\n";

$contents .= "Content-Transfer-Encoding: $file[encoding]\r\n";

$contents .= "Content-Location: $file[filepath]\r\n";

$contents .= "\r\n";

$contents .= $file['filecont'];

$contents .= "\r\n";

}

$contents .= "--{$this->boundary}--\r\n";

return $contents;

}

function MakeFile($filename){

$contents = $this->GetFile();

$fp = fopen($filename, 'w');

fwrite($fp, $contents);

fclose($fp);

}

function GetMimeType($filename){

$pathinfo = pathinfo($filename);

switch ($pathinfo['extension']) {

case 'htm': $mimetype = 'text/html'; break;

case 'html': $mimetype = 'text/html'; break;

case 'txt': $mimetype = 'text/plain'; break;

case 'cgi': $mimetype = 'text/plain'; break;

case 'php': $mimetype = 'text/plain'; break;

case 'css': $mimetype = 'text/css'; break;

case 'jpg': $mimetype = 'image/jpeg'; break;

case 'jpeg': $mimetype = 'image/jpeg'; break;

case 'jpe': $mimetype = 'image/jpeg'; break;

case 'gif': $mimetype = 'image/gif'; break;

case 'png': $mimetype = 'image/png'; break;

default: $mimetype = 'application/octet-stream'; break;

}

return $mimetype;

}

}

?>

点评:这种方法的缺点是不支持批量生成下载,因为一个页面只能有一个header,(无论远程使用还是本地生成声明header页面只能输出一个header),即使你循环生成,结果还是只有一个word生成(当然你可以修改上面的方式来实现)

2.纯HTML格式写入word

原理:

利用ob_start把html页面先存储起来(解决一下页面多个header问题,可以批量生成),然后在写入doc文档内容利用

代码:

复制代码 代码如下:

复制代码 代码如下:

$html = '

PHP10086

最靠谱的PHP技术分享网站

logo.gif

';

//批量生成

for($i=1;$i<=3;$i++){

$word = new word();

$word->start();

//$html = "aaa".$i;

$wordname = 'PHP教程网站--jb51.net'.$i.".doc";

echo $html;

$word->save($wordname);

ob_flush();//每次执行前刷新缓存

flush();

}

个人点评:这种方法效果最好,原因有三个:

第一代码比较简洁,很容易理解

第二是支持批量生成word(这个很重要)

第三是支持完整的html代码

http://www.bkjia.com/PHPjc/748163.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/748163.htmlTechArticle一、PHP生成word的两种思路或原理 1.利用windows下面的 com组件 2.利用PHP将内容写入doc文件之中 具体实现方法如下。 二、利用windows下面的com组...

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

这篇关于php将网页内容存到word,PHP把网页保存为word文件的三种方法_PHP教程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/351706

相关文章

SpringBoot实现接口数据加解密的三种实战方案

《SpringBoot实现接口数据加解密的三种实战方案》在金融支付、用户隐私信息传输等场景中,接口数据若以明文传输,极易被中间人攻击窃取,SpringBoot提供了多种优雅的加解密实现方案,本文将从原... 目录一、为什么需要接口数据加解密?二、核心加解密算法选择1. 对称加密(AES)2. 非对称加密(R

基于Go语言实现Base62编码的三种方式以及对比分析

《基于Go语言实现Base62编码的三种方式以及对比分析》Base62编码是一种在字符编码中使用62个字符的编码方式,在计算机科学中,,Go语言是一种静态类型、编译型语言,它由Google开发并开源,... 目录一、标准库现状与解决方案1. 标准库对比表2. 解决方案完整实现代码(含边界处理)二、关键实现细

MySQL精准控制Binlog日志数量的三种方案

《MySQL精准控制Binlog日志数量的三种方案》作为数据库管理员,你是否经常为服务器磁盘爆满而抓狂?Binlog就像数据库的“黑匣子”,默默记录着每一次数据变动,但若放任不管,几天内这些日志文件就... 目录 一招修改配置文件:永久生效的控制术1.定位my.cnf文件2.添加核心参数不重启热更新:高手应

ubuntu20.0.4系统中安装Anaconda的超详细图文教程

《ubuntu20.0.4系统中安装Anaconda的超详细图文教程》:本文主要介绍了在Ubuntu系统中如何下载和安装Anaconda,提供了两种方法,详细内容请阅读本文,希望能对你有所帮助... 本文介绍了在Ubuntu系统中如何下载和安装Anaconda。提供了两种方法,包括通过网页手动下载和使用wg

如何在Ubuntu上安装NVIDIA显卡驱动? Ubuntu安装英伟达显卡驱动教程

《如何在Ubuntu上安装NVIDIA显卡驱动?Ubuntu安装英伟达显卡驱动教程》Windows系统不同,Linux系统通常不会自动安装专有显卡驱动,今天我们就来看看Ubuntu系统安装英伟达显卡... 对于使用NVIDIA显卡的Ubuntu用户来说,正确安装显卡驱动是获得最佳图形性能的关键。与Windo

Oracle 通过 ROWID 批量更新表的方法

《Oracle通过ROWID批量更新表的方法》在Oracle数据库中,使用ROWID进行批量更新是一种高效的更新方法,因为它直接定位到物理行位置,避免了通过索引查找的开销,下面给大家介绍Orac... 目录oracle 通过 ROWID 批量更新表ROWID 基本概念性能优化建议性能UoTrFPH优化建议注

Pandas进行周期与时间戳转换的方法

《Pandas进行周期与时间戳转换的方法》本教程将深入讲解如何在pandas中使用to_period()和to_timestamp()方法,完成时间戳与周期之间的转换,并结合实际应用场景展示这些方法的... 目录to_period() 时间戳转周期基本操作应用示例to_timestamp() 周期转时间戳基

在 PyQt 加载 UI 三种常见方法

《在PyQt加载UI三种常见方法》在PyQt中,加载UI文件通常指的是使用QtDesigner设计的.ui文件,并将其转换为Python代码,以便在PyQt应用程序中使用,这篇文章给大家介绍在... 目录方法一:使用 uic 模块动态加载 (不推荐用于大型项目)方法二:将 UI 文件编译为 python 模

Python将字库文件打包成可执行文件的常见方法

《Python将字库文件打包成可执行文件的常见方法》在Python打包时,如果你想将字库文件一起打包成一个可执行文件,有几种常见的方法,具体取决于你使用的打包工具,下面就跟随小编一起了解下具体的实现方... 目录使用 PyInstaller基本方法 - 使用 --add-data 参数使用 spec 文件(

Python的pip在命令行无法使用问题的解决方法

《Python的pip在命令行无法使用问题的解决方法》PIP是通用的Python包管理工具,提供了对Python包的查找、下载、安装、卸载、更新等功能,安装诸如Pygame、Pymysql等Pyt... 目录前言一. pip是什么?二. 为什么无法使用?1. 当我们在命令行输入指令并回车时,一般主要是出现以