
背景:突然有大量的文件需要导出成PDF文件,写一个批量导出pdf的脚本,同时文件的命名也需要有一定的规则
导出方式:向服务器中上传csv文件,csv文件中包含文件的地址和相对应的文件命名。
如下格式:(地址是已经加密的)
758cm1q8t3NMzbEcGhXLm03oiEelzomItxYT4PuGD3xoJkcRnZ5VrFmeOt_Kucw5vyPUJDiOv0ipLxY1jL_RUjAraovb-jYVMObT1fru0DViCy0ZDmXzTLcEoQFVSdD336MjtNHFHmU8mHf0GwendD4rMmMl5A6S1VM5N88,王越
758cm1q8t3NMzbEcGhXLm03oiEelzomItxYT4PuGD3xoJkcRnZ5VrFmeOt_Kucw5vyPUJDiOv0ipLxY1jL_RUjAraovb-jYVMObT1fru0DViCy0ZDmXzTLcEoQFVSdD336MjtNHFHmU8mHf0GwendD4rMmMl5A6S1VM5N88,王越
导出的文件 index.php
<?php
require 'class/csv.php';
require 'class/Encrypt.php';
$args = getopt('f:');
$file = isset($args['f']) ? trim($args['f']) : '' ;
if(empty($file)){
exit("please input file " );
}
if(!file_exists(dirname(__FILE__) . "/csv/$file" ) ){
exit($file . "---is not exists " );
} $csvreader = new CsvReader(dirname(__FILE__) . "/csv/" . $file); $data = $csvreader->get_data( , ); if(empty($data) ){
exit("file content is empty ");
}
$folder = "/opt/data/site/export/file/" ;//创建文件的地址
$date = date("YmdHis" , time() );
$f = $folder . "/" . $date ;
mkdir( $f );
foreach($data as $key => $value ){
$url = isset($value[]) ? trim($value[]) : '' ;
$name = isset($value[]) ? trim($value[]) : '' ;
$url = Encrypt::AuthCode($url , "DECODE" , "*******@123reu!@^%" );//解密方式
if(empty($name) OR empty($url ) ){
continue ;
}
// echo $url ;
// echo "\n";
$commend = "/usr/local/wkhtmltox/bin/wkhtmltopdf '{{$url}}' {$f}/{$name}.pdf &";
system($commend );//调用系统命令
echo "export url is {$url} \n";
}
echo "OK-----success";
$args = getopt('f:');函数是读取shell脚本的 参数
例如: index.php -f 11.csv 直接读取csv文件夹中的11.csv文件,进行数据导出
csv类如下:
<?php
/**
* @Author: Awe
* @Date: 2017-02-06 10:49:26
* @Last Modified by: Awe
* @Last Modified time: 2017-02-06 10:49:34
*/
class CsvReader {
private $csv_file;
private $spl_object = null;
private $error; public function __construct($csv_file = '') {
if($csv_file && file_exists($csv_file)) {
$this->csv_file = $csv_file;
}
} public function set_csv_file($csv_file) {
if(!$csv_file || !file_exists($csv_file)) {
$this->error = 'File invalid';
return false;
}
$this->csv_file = $csv_file;
$this->spl_object = null;
} public function get_csv_file() {
return $this->csv_file;
} private function _file_valid($file = '') {
$file = $file ? $file : $this->csv_file;
if(!$file || !file_exists($file)) {
return false;
}
if(!is_readable($file)) {
return false;
}
return true;
} private function _open_file() {
if(!$this->_file_valid()) {
$this->error = 'File invalid';
return false;
}
if($this->spl_object == null) {
$this->spl_object = new SplFileObject($this->csv_file, 'rb');
}
return true;
} public function get_data($length = , $start = ) {
if(!$this->_open_file()) {
return false;
}
$length = $length ? $length : $this->get_lines();
$start = $start - ;
$start = ($start < ) ? : $start;
$data = array();
$this->spl_object->seek($start);
while ($length-- && !$this->spl_object->eof()) {
$data[] = $this->spl_object->fgetcsv();
$this->spl_object->next();
}
return $data;
} public function get_lines() {
if(!$this->_open_file()) {
return false;
}
$this->spl_object->seek(filesize($this->csv_file));
return $this->spl_object->key();
} public function get_error() {
return $this->error;
}
}
原理:获取csv文件中的内容,创建保存数据的文件夹,通过导出类来导出文件。
wkhtmltopdf 的介绍:http://blog.****.net/qq_14873105/article/details/51394026