服务端生成word并压缩打包下载

所需工具

  • phpwrod 库
  • php_zip 扩展

下载phpword库,放到类加载路径。

安装php_zip扩展

下载地址 

http://pecl.php.net/package/zip
linux下载targz包,windows下下载dll
 
linux下
 1 wget http://pecl.php.net/get/zip-1.12.5.tgz
 2 cd zip-1.12.5
 3 phpize
 4 ./configure
 5 make
 6 cp zip.so /alidata/server/php-5.4.23/lib/php/extensions/no-debug-non-zts-20100525/
 7 cd /alidata/server/php-5.4.23/etc/
 8 cp php.ini php.ini.bak
 9 echo 'extension=zip.so' >> php.ini
10 tail php.ini
11 service httpd restart

服务端生成word并压缩打包下载_第1张图片

PHPword生成脚本

首先要在word模板文件中定义好变量,定义的方式为

1 ${变量名}
 1 protected function proccessWord($templeteFilename,$values,$defaultValues,$baseFloder,$outFilename = 'word'){
 2     $values = array_merge($defaultValues,$values);
 3     $template = new TemplateProcessor($templeteFilename);
 4     foreach ($values as $k=>$v)
 5     {
 6         if(!is_array($v)){
 7             $template->setValue($k, strval($v));
 8         }
 9     }
10     if ( ! preg_match("|.+?\.docx$|", $outFilename))
11     {
12         $outFilename .= '.docx$';
13     }
14     $outFille = $baseFloder.'/'.$outFilename;
15     $template->saveAs($outFille);
16     return $outFille;
17 }

PHP压缩打包并响应

 1 public function postDocs(){
 2   $query = Ownerdebt::whereNotNull('id')->with('owner.block')->where('status','active');
 3   if(Input::get('type') == 'all'){
 4   }else{
 5      $selected = Input::get('selected');
 6      $ids = array_unique(explode(",",$selected));
 7      $query = $query->whereIn('id',$ids);
 8   }
 9   $ownerdebts = $query->get();
10 
11   $user = User::with('property')->find(Auth::user()->id);
12   $defaultValues = array(
13      'name'     =>'姓名',
14      'property' =>'某公司',
15      'block'    =>'某小区',
16      'room'     =>'某房',
17      'start_time'=>'2015年X月X日',
18      'end_time' =>'2015年X月X日',
19      'money'    =>'1000',  
20      'extra'    =>'400',             
21      'total'    =>'1400',
22      'last_time'=>'2015年X月X日',//    最后期限
23      'create_date'=>'2015年X月X日',
24   );
25   $baseFloder = base_path().'/../uploads/docs/';
26   $allDocs = [];
27   $property = $user->property;
28   if($property){
29      foreach($ownerdebts as $ot){
30         $values = $ot->toArray();
31         $values['name']         =  $ot->owner->name;
32         $values['property']     =  $property->name;
33         $values['block']        =  $ot->owner->block->name;
34         $values['room']         =  $values['owner']['room'];
35         $values['start_time']   =  date('Y 年 m 月 d',strtotime($values['start_time']));
36         $values['end_time']     =  date('Y 年 m 月 d',strtotime($values['end_time']));
37         $values['total']        =  $values['money'] + $values['extra'];
38         $values['last_time']    =  date('Y 年 m 月 d',strtotime('+30 day'));
39         $values['create_date']  =  date('Y 年 m 月 d');
40 
41         //SiteHelpers::P($values);
42         $fileName = $values['room'].'-'.date("Y-m-d").'.docx';
43         $allDocs [] = $fileName;
44         $this->proccessWord(
45            app_path().'/template/lawyer.docx',
46            $values,
47            $defaultValues,
48            $baseFloder,
49            $fileName
50         );
51      }
52      // 打包
53      $zip_file = dirname(base_path())."/uploads/zip/lawyer-".date('Y-m-d-H-i-s').".zip";
54      $zipHelpers = new ZipHelpers();
55      //$zipTool = new ZipArchive();
56      //if(file_exists($zip_file)) unlink($zip_file);
57      //$open = $zipTool->open($zip_file,ZipArchive::CREATE);
58      foreach($allDocs as $docs){
59         $zipHelpers->add_data('docs/'.$docs, file_get_contents($baseFloder.'/'.$docs));
60         //if($open) $zipTool->addFile($docs,$baseFloder.'/'.$docs);
61      }
62      //if($open) $zipTool->close();
63 
64      $flag = $zipHelpers->archive($zip_file);
65      $zipHelpers->clear_data();
66 
67      if($flag) {
68         header("Content-type: application/zip");
69         header("Content-Disposition: attachment; filename=\"" .basename($zip_file) . "\"");
70 //       header("Content-Length: " . filesize($zip_file)); // 这里不能响应长度,否则会造成压缩包损坏
71         readfile($zip_file);
72      }else{
73 
74      }
75   }
76   exit;
77 }

 

 

你可能感兴趣的:(服务端生成word并压缩打包下载)