PHP HTML生成word

2023-06-14,,

/**

  • 根据HTML代码获取word文档内容
  • 创建一个本质为mht的文档,该函数会分析文件内容并从远程下载页面中的图片资源
  • 该函数依赖于类WordMake
  • 该函数会分析img标签,提取src的属性值。但是,src的属性值必须被引号包围,否则不能提取
  • @param string $content HTML内容
  • @param string $absolutePath 网页的绝对路径。如果HTML内容里的图片路径为相对路径,那么就需要填写这个参数,来让该函数自动填补成绝对路径。这个参数最后需要以/结束
  • @param bool $isEraseLink 是否去掉HTML内容中的链接
    */
    // 手动下载下Wordmaker

function WordMake( $content , $absolutePath = "" , $isEraseLink = true )
{
import("ORG.Util.Wordmaker");
$mht = new \MhtFileMaker();
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();
}

function GenerateWord($content){
$_path='Uploads/word/'.date('Y-m-d',time()).'/';
if (!file_exists($_path)){ mkdir ($_path); }
$content = iconv("utf-8", "GBK",$content);
$rand=rand(1000,9999);

$fileContent = WordMake($content,$url);//生成word内容

$fp = fopen($_path.$name.time().$rand.".doc", 'w');//打开生成的文档
fwrite($fp, $fileContent);//写入包保存文件
fclose($fp);
return $_path.$name.time().$rand.".doc";

}