php html转word的最简单方法

1 原理

<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">

2 优点

可以加图片
可以支持class样式
可以支持行内样式
简单,实现简单

3 案例

<?php
/************************************************************
** @Description: 使用PHP将html转word
** @Author: georgehao
** @Date: 2019-01-10 15:47:23
** @Last Modified by: georgehao
** @Last Modified time: 2019-01-11 10:05:57
*************************************************************/
$title = "PPGo在路上";
$html = '<h1>中文的标题,技术无止境,一直在路上</h1>
<p>p是可以分段的. 使用PHP将html转word</p>
<p>再分一段 使用PHP将html转word</p>
<p>还分一段,下面加个图片</p>
<img alt="" class="has" src="http://www.ibp.cas.cn/kyjz/zxdt/201901/W020190103493057285919.jpg">
<style>
.testclass{
font-size:30px;
color:#ff0000;
}
</style>
<div class="testclass">class样式样式是否可以</div>
<div style="color:#999fff">测试行内样式</div>
';
//使用方法-------------------------
echo (cword($html,$title));
//----------------------------------------
function cword($data,$fileName='')
{
if(empty($data)) return '';
$data = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">'.$data.'</html>';
$dir = "./dir/";
if(!file_exists($dir)) mkdir($dir,777,true);
if(empty($fileName)){
$fileName=$dir.date('His').'.doc';
}else{
$fileName =$dir.$fileName.'.doc';
}
$writefile = fopen($fileName,'wb') or die("创建文件失败"); //wb以二进制写入
fwrite($writefile,$data);
fclose($writefile);
return $fileName;
}


相关推荐

  • 生成图片

    from PIL import Image, ImageColor, ImageDraw, ImageFont, ImageFilterdef create_image_with_text(size, color, text, font_path, font_size, text_color, shadow_color, output_path): """ Create a new image of specified size and color with centered text that has a border and shadow. :param size: A tuple con

  • 获取指定目录下的所有图片信息

    1 获取指定目录下的所有图片信息// 获取指定目录下的所有图片信息 public function getImagesInfo($directory) { $images = []; // 创建递归目录迭代器 $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY ); // 遍历目录中的每个文件 foreach (

  • Thinkphp各版本的PHP要求

    ThinkPHP 8.0:运行环境要求PHP8.0+,兼容PHP8.3ThinkPHP 6.1:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 6.0:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 5.1:运行环境要求PHP5.6+,兼容PHP8.0ThinkPHP 5.0:运行环境要求PHP5.4+,兼容PHP7.3