ApiSystem文档管理系统交流社区

 找回密码
 立即注册
搜索
热搜: 活动 交友
查看: 41244|回复: 0

PHP生成word文档快速解决方法

[复制链接]

6

主题

9

帖子

121

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
121
发表于 2017-3-31 09:39:14 | 显示全部楼层 |阅读模式
使用MhtFileMaker类,这个函数的主要功能其实就是分析HTML代码中的所有图片地址,并且依次下载下来。获取到了图片的内容以后,调用MhtFileMaker类,将图片添加到mht文件中。具体的添加细节,封装在MhtFileMaker类中了。

  1. header("Cache-Control: no-cache, must-revalidate");
  2. header("Pragma: no-cache");
  3. $wordStr = '这里是内容,可以是html模板';
  4. $fileContent = getWordDocument($wordStr);
  5. $fileName = iconv("utf-8", "GBK", ‘jb51' . '_'. $intro . '_' . rand(100, 999));
  6. header("Content-Type: application/doc");
  7. header("Content-Disposition: attachment; filename=" . $fileName . ".doc");
  8. echo $fileContent;
复制代码
MhtFileMaker类文件

  1. <?php
  2. /***********************************************************************
  3. Class:  Mht File Maker
  4. Version:  1.2 beta
  5. Date:   02/11/2007
  6. Author:  Wudi
  7. Description: The class can make .mht file.
  8. ***********************************************************************/
  9.   
  10. class MhtFileMaker{
  11. var $config = array();
  12. var $headers = array();
  13. var $headers_exists = array();
  14. var $files = array();
  15. var $boundary;
  16. var $dir_base;
  17. var $page_first;
  18.   
  19. function MhtFile($config = array()){
  20.   
  21. }
  22.   
  23. function SetHeader($header){
  24.   $this->headers[] = $header;
  25.   $key = strtolower(substr($header, 0, strpos($header, ':')));
  26.   $this->headers_exists[$key] = TRUE;
  27. }
  28.   
  29. function SetFrom($from){
  30.   $this->SetHeader("From: $from");
  31. }
  32.   
  33. function SetSubject($subject){
  34.   $this->SetHeader("Subject: $subject");
  35. }
  36.   
  37. function SetDate($date = NULL, $istimestamp = FALSE){
  38.   if ($date == NULL) {
  39.    $date = time();
  40.   }
  41.   if ($istimestamp == TRUE) {
  42.    $date = date('D, d M Y H:i:s O', $date);
  43.   }
  44.   $this->SetHeader("Date: $date");
  45. }
  46.   
  47. function SetBoundary($boundary = NULL){
  48.   if ($boundary == NULL) {
  49.    $this->boundary = '--' . strtoupper(md5(mt_rand())) . '_MULTIPART_MIXED';
  50.   } else {
  51.    $this->boundary = $boundary;
  52.   }
  53. }
  54.   
  55. function SetBaseDir($dir){
  56.   $this->dir_base = str_replace("\", "/", realpath($dir));
  57. }
  58.   
  59. function SetFirstPage($filename){
  60.   $this->page_first = str_replace("\", "/", realpath("{$this->dir_base}/$filename"));
  61. }
  62.   
  63. function AutoAddFiles(){
  64.   if (!isset($this->page_first)) {
  65.    exit ('Not set the first page.');
  66.   }
  67.   $filepath = str_replace($this->dir_base, '', $this->page_first);
  68.   $filepath = 'http://mhtfile' . $filepath;
  69.   $this->AddFile($this->page_first, $filepath, NULL);
  70.   $this->AddDir($this->dir_base);
  71. }
  72.   
  73. function AddDir($dir){
  74.   $handle_dir = opendir($dir);
  75.   while ($filename = readdir($handle_dir)) {
  76.    if (($filename!='.') && ($filename!='..') && ("$dir/$filename"!=$this->page_first)) {
  77.     if (is_dir("$dir/$filename")) {
  78.      $this->AddDir("$dir/$filename");
  79.     } elseif (is_file("$dir/$filename")) {
  80.      $filepath = str_replace($this->dir_base, '', "$dir/$filename");
  81.      $filepath = 'http://mhtfile' . $filepath;
  82.      $this->AddFile("$dir/$filename", $filepath, NULL);
  83.     }
  84.    }
  85.   }
  86.   closedir($handle_dir);
  87. }
  88.   
  89. function AddFile($filename, $filepath = NULL, $encoding = NULL){
  90.   if ($filepath == NULL) {
  91.    $filepath = $filename;
  92.   }
  93.   $mimetype = $this->GetMimeType($filename);
  94.   $filecont = file_get_contents($filename);
  95.   $this->AddContents($filepath, $mimetype, $filecont, $encoding);
  96. }
  97.   
  98. function AddContents($filepath, $mimetype, $filecont, $encoding = NULL){
  99.   if ($encoding == NULL) {
  100.    $filecont = chunk_split(base64_encode($filecont), 76);
  101.    $encoding = 'base64';
  102.   }
  103.   $this->files[] = array('filepath' => $filepath,
  104.         'mimetype' => $mimetype,
  105.         'filecont' => $filecont,
  106.         'encoding' => $encoding);
  107. }
  108.   
  109. function CheckHeaders(){
  110.   if (!array_key_exists('date', $this->headers_exists)) {
  111.    $this->SetDate(NULL, TRUE);
  112.   }
  113.   if ($this->boundary == NULL) {
  114.    $this->SetBoundary();
  115.   }
  116. }
  117.   
  118. function CheckFiles(){
  119.   if (count($this->files) == 0) {
  120.    return FALSE;
  121.   } else {
  122.    return TRUE;
  123.   }
  124. }
  125.   
  126. function GetFile(){
  127.   $this->CheckHeaders();
  128.   if (!$this->CheckFiles()) {
  129.    exit ('No file was added.');
  130.   }
  131.   $contents = implode("\r\n", $this->headers);
  132.   $contents .= "\r\n";
  133.   $contents .= "MIME-Version: 1.0\r\n";
  134.   $contents .= "Content-Type: multipart/related;\r\n";
  135.   $contents .= "\tboundary="{$this->boundary}";\r\n";
  136.   $contents .= "\ttype="" . $this->files[0]['mimetype'] . ""\r\n";
  137.   $contents .= "X-MimeOLE: Produced By Mht File Maker v1.0 beta\r\n";
  138.   $contents .= "\r\n";
  139.   $contents .= "This is a multi-part message in MIME format.\r\n";
  140.   $contents .= "\r\n";
  141.   foreach ($this->files as $file) {
  142.    $contents .= "--{$this->boundary}\r\n";
  143.    $contents .= "Content-Type: $file[mimetype]\r\n";
  144.    $contents .= "Content-Transfer-Encoding: $file[encoding]\r\n";
  145.    $contents .= "Content-Location: $file[filepath]\r\n";
  146.    $contents .= "\r\n";
  147.    $contents .= $file['filecont'];
  148.    $contents .= "\r\n";
  149.   }
  150.   $contents .= "--{$this->boundary}--\r\n";
  151.   return $contents;
  152. }
  153.   
  154. function MakeFile($filename){
  155.   $contents = $this->GetFile();
  156.   $fp = fopen($filename, 'w');
  157.   fwrite($fp, $contents);
  158.   fclose($fp);
  159. }
  160.   
  161. function GetMimeType($filename){
  162.   $pathinfo = pathinfo($filename);
  163.   switch ($pathinfo['extension']) {
  164.    case 'htm': $mimetype = 'text/html'; break;
  165.    case 'html': $mimetype = 'text/html'; break;
  166.    case 'txt': $mimetype = 'text/plain'; break;
  167.    case 'cgi': $mimetype = 'text/plain'; break;
  168.    case 'php': $mimetype = 'text/plain'; break;
  169.    case 'css': $mimetype = 'text/css'; break;
  170.    case 'jpg': $mimetype = 'image/jpeg'; break;
  171.    case 'jpeg': $mimetype = 'image/jpeg'; break;
  172.    case 'jpe': $mimetype = 'image/jpeg'; break;
  173.    case 'gif': $mimetype = 'image/gif'; break;
  174.    case 'png': $mimetype = 'image/png'; break;
  175.    default: $mimetype = 'application/octet-stream'; break;
  176.   }
  177.   return $mimetype;
  178. }
  179. }
  180. ?>
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|ApiSystem文档管理系统社区  

GMT+8, 2021-10-28 10:49 , Processed in 0.060802 second(s), 14 queries , File On.

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表