| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- namespace App\Servers\Wps;
- use App\Models\FactoringContract;
- use App\Models\FactoringContractFile;
- use App\Models\FileManager;
- use App\Servers\System\CommonServer;
- use Illuminate\Support\Facades\DB;
- use PhpOffice\PhpWord\IOFactory;
- class PdfServer
- {
- static private $server = null;
- function __construct()
- {
- }
- /**
- * 创建对象
- * @return PdfServer
- */
- static function creatServer()
- {
- if (empty(self::$server)) self::$server = new PdfServer();
- return self::$server;
- }
- /**
- * 文件转换为PDF
- * @return bool
- * @throws \Throwable
- */
- function createFileConvert(){
- $list = FileManager::where('status',0)
- ->where('is_convert',1)
- ->where('task_id','')
- ->orderBy('id','asc')
- ->limit(20)
- ->get();
- if (count($list) <= 0) {
- return false;
- }
- foreach ($list as $value){
- $extension = pathinfo($value->{'file_url'}, PATHINFO_EXTENSION);
- if($extension=='pdf'){
- $url=$value->{'file_url'};
- }else{
- if (in_array($extension,['png','jpg','jpeg','gif'])){//走图片转文档
- $url = $this->imgToPdf($value->{'file_url'});
- }else{
- $task_id= $this->wordToPdf($value->{'file_url'});
- if($task_id){
- $value->update(['task_id'=>$task_id]);
- }
- continue;
- }
- if (empty($url)) {
- $value->update(['is_convert'=>0]);
- continue;
- }
- }
- //更新url
- FileManager::where('id',$value->{'id'})->update(['file_url'=>$url,'old_file_url'=>$value->{'file_url'},'is_convert'=>0]);
- }
- DB::commit();
- return true;
- }
- /**
- * 文件转换pdf记录
- * @return bool
- */
- function queryFileConvert(){
- $list = FileManager::where('status',0)
- ->where('is_convert',1)
- ->where('task_id','<>','')
- ->orderBy('id','asc')
- ->limit(20)
- ->get();
- if (count($list) <= 0) {
- return false;
- }
- foreach ($list as $item){
- $url= WpsServer::creatServer()->queryConvertPdf($item['task_id']);
- if($url){
- $item->update(['file_url'=>$url,'old_file_url'=>$item->{'file_url'},'is_convert'=>0]);
- }
- }
- }
- /**
- * 合同文件更新
- * @return bool
- */
- function setFactoringContract(){
- $list=FactoringContractFile::where('file_pdf','')->where('task_id','')->where('is_end',1)->select(['id','file_path','file_url','file_pdf'])->limit(10)->get();
- if($list->count()<=0){
- return false;
- }
- foreach ($list as $value){
- $task_id= $this->wordToPdf($value->{'file_url'});
- if($task_id){
- $value->update(['task_id'=>$task_id]);
- }
- }
- }
- /**
- * 查询合同转换结果
- * @return bool
- */
- function queryFactoringContract(){
- $list=FactoringContractFile::where('file_pdf','')->where('task_id','<>','')->select(['id','file_path','file_url','task_id','file_pdf','type','file_no'])->limit(10)->get();
- if($list->count()<=0){
- return false;
- }
- foreach ($list as $item){
- $url= WpsServer::creatServer()->queryConvertPdf($item['task_id']);
- if($url){
- $item->update(['file_pdf'=>$url]);
- if($item->{'type'}==2){
- FactoringContract::where('file_no',$item['file_no'])->update(['contracts_pdf'=>$url]);
- }
- }
- }
- }
- /**
- * word转pdf
- * @param $wordUrl
- * @param $id
- * @param $type
- * @return string
- */
- function wordToPdf($wordUrl){
- $fileName=CommonServer::creatServer()->getFileName($wordUrl);
- return WpsServer::creatServer()->fileConvertPdf($wordUrl,$fileName);
- // $storageUrl='storage/pdf/'.date('Ymd').'/';
- // $path=public_path($storageUrl);
- // $fileUrl=$path.time().$fileName;
- // if(!is_dir($path)){
- // mkdir($path,0777,true);
- // }
- // $extension = pathinfo($fileName);
- //// file_put_contents($fileUrl ,file_get_contents($wordUrl));
- ////// $content = file_get_contents('https://wealfavor-1257406827.cos.ap-beijing.myqcloud.com/git.docx');
- ////// dd($content);
- //// dd($fileUrl);
- //// $phpWord = IOFactory::load($fileUrl);
- //// $wordHtml = IOFactory::createWriter($phpWord,'HTML');
- //// $fileHtml=$path.$fileName.'.html';
- //// $wordHtml->save($fileHtml);
- //// $html=file_get_contents($fileHtml);
- //// // 设置Dompdf选项
- //// $dompdf = new Dompdf();
- //// $dompdf->loadHtml($html,'UTF-8');
- //// $dompdf->setPaper('A4', 'portrait');
- //// $dompdf->render();
- //// $pdf = $dompdf->output();
- ////
- ////// 将PDF写入文件
- //// file_put_contents($path.$fileName.'.pdf', $pdf);
- // // 创建一个新的Word文档实例
- // file_put_contents($fileUrl ,file_get_contents($wordUrl));
- // $phpWord = IOFactory::load($fileUrl);
- //
- // // 创建一个新的FPDF实例
- //// $pdf = new \FPDF();
- // $pdf = new \PDF_Chinese();
- // $pdf->AddGBFont('simhei','黑体');
- // $pdf->AddGBFont('simkai','楷体_GB2312');
- //
- // // 遍历Word文档中的所有部分并转换为PDF
- // foreach ($phpWord->getSections() as $section) {
- // // 添加一页PDF
- // $pdf->AddPage();
- //// $pdf->setFont('freeserif');
- // $pdf->SetFont('simhei','',16);
- //// $pdf->AddFont('GB','', 'song.cng.php');
- //// $pdf->AddFont('GB','B', 'song.cnb.php');
- // // 遍历文档部分中的所有元素
- // foreach ($section->getElements() as $element) {
- // // 处理文本元素
- //
- // if (method_exists($element, 'getText')) {
- // $text = $element->getText();
- // $text = mb_convert_encoding($text, 'GBK', 'UTF-8');
- // $pdf->Cell(0, 5, $text, 0, 1, 'L');
- // }
- //
- // // 处理图像元素
- // if (method_exists($element, 'getImage')) {
- // $image = $element->getImage(0);
- // $pdf->Image($image->getSource(), $pdf->GetX(), $pdf->GetY(), $image->getWidth(), $image->getHeight());
- // }
- //
- // // 处理其他元素类型...
- // }
- // }
- // $fileName=str_replace($extension['extension'],'',$fileName);
- // // 保存PDF到文件
- // $pdf->Output($path.$fileName.'pdf', 'F');
- // return 'http://'.env('APP_ADMIN_HOST').'/'.$storageUrl.$fileName.'pdf';
- }
- /**
- * 图片转pdf
- * @param $imgUrl
- * @return string
- */
- function imgToPdf($imgUrl){
- $fileName=CommonServer::creatServer()->getFileName($imgUrl);
- $storageUrl='storage/pdf/'.date('Ymd').'/';
- $path=public_path($storageUrl);
- if(!is_dir($path)){
- mkdir($path,0777,true);
- }
- // $base64Img=base64_encode(file_get_contents($imgUrl));
- // $extension = pathinfo($fileName);
- // $htmlContent = '<html><body><img src="data:image/'.$extension['extension'].';base64,'.$base64Img.'" /></body></html>';
- // dd($htmlContent);
- // $dompdf = new Dompdf();
- // $dompdf->loadHtml($htmlContent,'UTF-8');
- // $dompdf->setPaper('A4', 'portrait');
- // $dompdf->render();
- // $pdf = $dompdf->output();
- // // 将PDF写入文件
- // $fileName=str_replace($extension['extension'],'',$fileName);
- // file_put_contents($path.$fileName.'pdf', $pdf);
- $pdf = new \FPDF();
- $pdf->AddPage();
- // 获取图片尺寸
- list($width, $height) = getimagesize($imgUrl);
- $extension = pathinfo($fileName);
- // 获取PDF页面宽度
- $pageWidth = $pdf->getPageWidth()-10;
- $pageHeight = $pdf->GetPageHeight()-10;
- // 计算图片缩放比例,使其宽度与页面宽度相匹配
- $scale = $pageWidth / $width;
- // 计算缩放后的高度
- $newHeight = $height * $scale;
- if($pageHeight<$newHeight){
- $newHeight=$pageHeight;
- $scale = $newHeight / $height;
- $newWidth=$width * $scale;
- }else{
- $newWidth=$pageWidth;
- }
- // 将图片添加到PDF,并设置缩放比例
- $pdf->Image($imgUrl, 5, 5, $newWidth, $newHeight, strtoupper($extension['extension']));
- $fileName=str_replace($extension['extension'],'',$fileName);
- $pdf->Output('F', $path.$fileName.'pdf');
- return 'https://'.env('APP_ADMIN_HOST').'/'.$storageUrl.$fileName.'pdf';
- }
- }
|