add-api-fill_pdf

This commit is contained in:
lccsw
2022-01-06 15:27:18 +08:00
parent c2b3165473
commit 95cace10f2
+45
View File
@@ -0,0 +1,45 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use mikehaertl\pdftk\Pdf;
class Pdfapi extends CI_Controller {
//pdf填充表单域接口 dev使用
public function fill_pdf()
{
$file_url = $this->input->post('file_url');
$fill_data = $this->input->post('fill_data');
if(!$file_url || !$fill_data){
die(json_encode(['code'=>0,'msg'=>'参数错误']));
}
$fill_data = json_decode($fill_data,true);
$url = base64_decode(urldecode($file_url));
$file_path = FCPATH.'tmp.pdf';
$arrContextOptions = [
"ssl" => [
"verify_peer"=>false,
"verify_peer_name"=>false,
],
];
$file = file_get_contents($file_url,false,stream_context_create($arrContextOptions));
file_put_contents($file_path, $file);
if(file_exists($file_path)){
$pdf = new Pdf($file_path);
$result = $pdf->fillForm($fill_data)->needAppearances()->saveAs('fill.pdf');
if ($result === false) {
die(json_encode(['code'=>0,'msg'=>$pdf->getError()]));
}else{
$data = [
'file_path' => http_host_com('api').'/fill.pdf',
];
@unlink($file_path);
die(json_encode(['code'=>1,'msg'=>'保存成功','data'=>$data]));
}
}else{
die(json_encode(['code'=>0,'msg'=>'保存文件失败']));
}
}
}