132 lines
4.6 KiB
PHP
132 lines
4.6 KiB
PHP
<?php
|
|
/**
|
|
* Created by Vim
|
|
* User: lcc
|
|
* Date: 2021/07/12
|
|
* Time: 16:49
|
|
*/
|
|
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
use mikehaertl\pdftk\Pdf;
|
|
|
|
class Pdftk {
|
|
|
|
private $is_dev = false;
|
|
private $api_url = 'https://api.liche.cn/';
|
|
public function __construct(){
|
|
if (false !== strpos($_SERVER['HTTP_HOST'], 'dev')) {//dev
|
|
$this->is_dev = true;
|
|
$this->dev_api_url = 'https://liche-api-dev.xiaoyu.com/';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* pdf文件表单域填充
|
|
* @param $pdf_path string pdf文件地址
|
|
* @param $save_path string 填充后文件保存路径
|
|
* @param $fill_data array 填充域对应内容['field'=>'text']
|
|
* @return array
|
|
*/
|
|
public function fill_pdf($pdf_path,$save_path,$fill_data){
|
|
$pdf_path = FCPATH.$pdf_path;
|
|
$save_path = FCPATH.$save_path;
|
|
if($this->is_dev){
|
|
$ci = & get_instance();
|
|
$ci->load->library('mycurl');
|
|
$file_name = str_replace(FCPATH,"",$pdf_path);
|
|
$p_data = [
|
|
'file_url' => $this->dev_api_url.$file_name,
|
|
'fill_data' => json_encode($fill_data)
|
|
];
|
|
$result = $ci->mycurl->httpPost($this->api_url.'pdfapi/fill_pdf',$p_data);
|
|
$resdata = json_decode($result,true);
|
|
$opt = false;
|
|
$data = [];
|
|
if($resdata['code'] && $resdata['data']['file_path']){
|
|
$save_path_arr = explode('/',$save_path);
|
|
array_pop($save_path_arr);
|
|
$tmp_path = implode('/',$save_path_arr);
|
|
if (!file_exists($tmp_path)) {
|
|
$oldumask = umask(0);
|
|
mkdir($tmp_path, 0777, true);
|
|
umask($oldumask);
|
|
}
|
|
$arrContextOptions = [
|
|
"ssl" => [
|
|
"verify_peer"=>false,
|
|
"verify_peer_name"=>false,
|
|
],
|
|
|
|
];
|
|
$file = file_get_contents($resdata['data']['file_path'],false,stream_context_create($arrContextOptions));
|
|
file_put_contents($save_path, $file);
|
|
if(file_exists($save_path)){
|
|
$opt = true;
|
|
$data['file_path'] = str_replace(FCPATH,'',$save_path);
|
|
}
|
|
}
|
|
if($opt){
|
|
return ['code'=>1,'msg'=>'保存成功','data' => $data];
|
|
}else{
|
|
return ['code'=>0,'msg'=>'保存失败'];
|
|
}
|
|
}else{
|
|
$pdf = new Pdf($pdf_path);
|
|
$result = $pdf->fillForm($fill_data)->needAppearances()->saveAs($save_path);
|
|
if ($result === false) {
|
|
return ['code'=>0,'msg'=>$pdf->getError()];
|
|
}else{
|
|
$data['file_path'] = $save_path;
|
|
return ['code'=>1,'msg'=>'保存成功',$data];
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
/**
|
|
* e签宝填充pdf表单域
|
|
* @param $pdf_path
|
|
* @param $save_path
|
|
* @param $fill_data
|
|
* @return array
|
|
*/
|
|
public function esign_fill_pdf($pdf_path,$save_path,$fill_data){
|
|
$pdf_path = FCPATH.$pdf_path;
|
|
$save_path = FCPATH.$save_path;
|
|
require_once COMMPATH."/third_party/esign/eSignOpenAPI.php";
|
|
try {
|
|
$sign = new \tech\core\eSign();
|
|
$iRet = $sign->init();
|
|
if (0 !== $iRet) {
|
|
dir('初始化失败');
|
|
return ['code' => 0 ,'msg' => '初始化失败'];
|
|
}
|
|
$tmpFile=array(
|
|
'srcFileUrl' =>$pdf_path,
|
|
'dstFileUrl' => ''
|
|
);
|
|
$ret = $sign->createFromTemplate($tmpFile,true,$fill_data,true);
|
|
if($ret['errCode']!==0){
|
|
return ['code' => 0 ,'msg' => $ret['msg']];
|
|
}
|
|
$save_path_arr = explode('/',$save_path);
|
|
array_pop($save_path_arr);
|
|
$tmp_path = implode('/',$save_path_arr);
|
|
if (!file_exists($tmp_path)) {
|
|
$oldumask = umask(0);
|
|
mkdir($tmp_path, 0777, true);
|
|
umask($oldumask);
|
|
}
|
|
file_put_contents($save_path, base64_decode($ret['stream']), true);
|
|
if(file_exists($save_path)){
|
|
$data['file_path'] = str_replace(FCPATH,'',$save_path);
|
|
return ['code' => 1 ,'msg'=>'保存成功','data' => $data];
|
|
}else{
|
|
return ['code' => 0 ,'msg' => '保存失败'];
|
|
}
|
|
} catch (Exception $e) {
|
|
return ['code' => 0 ,'msg' => '初始化出错:'.$e->getMessage()];
|
|
}
|
|
}
|
|
}
|