php批量导出pdf文件的脚本(html-PDf)

背景:突然有大量的文件需要导出成PDF文件,写一个批量导出pdf的脚本,同时文件的命名也需要有一定的规则

导出方式:向服务器中上传csv文件,csv文件中包含文件的地址和相对应的文件命名。

如下格式:(地址是已经加密的)

758cm1q8t3NMzbEcGhXLm03oiEelzomItxYT4PuGD3xoJkcRnZ5VrFmeOt_Kucw5vyPUJDiOv0ipLxY1jL_RUjAraovb-jYVMObT1fru0DViCy0ZDmXzTLcEoQFVSdD336MjtNHFHmU8mHf0GwendD4rMmMl5A6S1VM5N88,王越
758cm1q8t3NMzbEcGhXLm03oiEelzomItxYT4PuGD3xoJkcRnZ5VrFmeOt_Kucw5vyPUJDiOv0ipLxY1jL_RUjAraovb-jYVMObT1fru0DViCy0ZDmXzTLcEoQFVSdD336MjtNHFHmU8mHf0GwendD4rMmMl5A6S1VM5N88,王越

导出的文件 index.php

 1 <?php
 2 require 'class/csv.php';
 3 require 'class/Encrypt.php';
 4 $args = getopt('f:');
 5 $file = isset($args['f']) ? trim($args['f']) : '' ;
 6 if(empty($file)){
 7     exit("please input file " );
 8 }
 9 if(!file_exists(dirname(__FILE__) . "/csv/$file"  ) ){
10     exit($file . "---is not exists " );
11 }
12 
13 $csvreader = new CsvReader(dirname(__FILE__) . "/csv/" . $file);
14 
15 $data = $csvreader->get_data(1000 , 0 );
16 
17 if(empty($data) ){
18     exit("file content is empty ");
19 }
20 $folder = "/opt/data/site/export/file/" ;//创建文件的地址
21 $date = date("YmdHis" , time() );
22 $f = $folder . "/" . $date ;
23 mkdir( $f );
24 foreach($data as $key => $value ){
25     $url = isset($value[0]) ? trim($value[0]) : '' ;
26     $name = isset($value[1]) ? trim($value[1]) : '' ;
27     $url = Encrypt::AuthCode($url , "DECODE" , "*******@123reu!@^%" );//解密方式
28     if(empty($name) OR empty($url ) ){
29         continue ;
30     }
31 //    echo $url ;
32 //    echo "\n";
33     $commend = "/usr/local/wkhtmltox/bin/wkhtmltopdf '{{$url}}' {$f}/{$name}.pdf &";
34     system($commend );//调用系统命令
35     echo "export url is {$url} \n";
36 }
37 echo "OK-----success";
$args = getopt('f:');函数是读取shell脚本的 参数
例如: index.php -f 11.csv 直接读取csv文件夹中的11.csv文件,进行数据导出
csv类如下:
 1 <?php
 2 /** 7  */
 8 class CsvReader {
 9     private $csv_file;
10     private $spl_object = null;
11     private $error;
12     
13     public function __construct($csv_file = '') {
14         if($csv_file && file_exists($csv_file)) {
15             $this->csv_file = $csv_file;
16         }
17     }
18     
19     public function set_csv_file($csv_file) {
20         if(!$csv_file || !file_exists($csv_file)) {
21             $this->error = 'File invalid';
22             return false;
23         }
24         $this->csv_file = $csv_file;
25         $this->spl_object = null;
26     }
27     
28     public function get_csv_file() {
29         return $this->csv_file;
30     }
31     
32     private function _file_valid($file = '') {
33         $file = $file ? $file : $this->csv_file;
34         if(!$file || !file_exists($file)) {
35             return false;
36         }
37         if(!is_readable($file)) {
38             return false;
39         }
40         return true;
41     }
42     
43     private function _open_file() {
44         if(!$this->_file_valid()) {
45             $this->error = 'File invalid';
46             return false;
47         }
48         if($this->spl_object == null) {
49             $this->spl_object = new SplFileObject($this->csv_file, 'rb');
50         }
51         return true;
52     }
53 
54     public function get_data($length = 0, $start = 0) {
55         if(!$this->_open_file()) {
56             return false;
57         }
58         $length = $length ? $length : $this->get_lines();
59         $start = $start - 1;
60         $start = ($start < 0) ? 0 : $start;
61         $data = array();
62         $this->spl_object->seek($start);
63         while ($length-- && !$this->spl_object->eof()) {
64             $data[] = $this->spl_object->fgetcsv();
65             $this->spl_object->next();
66         }
67         return $data;
68     }
69     
70     public function get_lines() {
71         if(!$this->_open_file()) {
72             return false;
73         }
74         $this->spl_object->seek(filesize($this->csv_file));
75         return $this->spl_object->key();
76     }
77     
78     public function get_error() {
79         return $this->error;
80     }
81 }

原理:获取csv文件中的内容,创建保存数据的文件夹,通过导出类来导出文件。

wkhtmltopdf  的介绍:http://blog.csdn.net/qq_14873105/article/details/51394026

 
posted @ 2017-10-17 18:11  小军的代码库  阅读(1439)  评论(0编辑  收藏  举报