PdfServer.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace App\Servers\Wps;
  3. use App\Models\FactoringContract;
  4. use App\Models\FactoringContractFile;
  5. use App\Models\FileManager;
  6. use App\Servers\System\CommonServer;
  7. use Illuminate\Support\Facades\DB;
  8. use PhpOffice\PhpWord\IOFactory;
  9. class PdfServer
  10. {
  11. static private $server = null;
  12. function __construct()
  13. {
  14. }
  15. /**
  16. * 创建对象
  17. * @return PdfServer
  18. */
  19. static function creatServer()
  20. {
  21. if (empty(self::$server)) self::$server = new PdfServer();
  22. return self::$server;
  23. }
  24. /**
  25. * 文件转换为PDF
  26. * @return bool
  27. * @throws \Throwable
  28. */
  29. function createFileConvert(){
  30. $list = FileManager::where('status',0)
  31. ->where('is_convert',1)
  32. ->where('task_id','')
  33. ->orderBy('id','asc')
  34. ->limit(20)
  35. ->get();
  36. if (count($list) <= 0) {
  37. return false;
  38. }
  39. foreach ($list as $value){
  40. $extension = pathinfo($value->{'file_url'}, PATHINFO_EXTENSION);
  41. if($extension=='pdf'){
  42. $url=$value->{'file_url'};
  43. }else{
  44. if (in_array($extension,['png','jpg','jpeg','gif'])){//走图片转文档
  45. $url = $this->imgToPdf($value->{'file_url'});
  46. }else{
  47. $task_id= $this->wordToPdf($value->{'file_url'});
  48. if($task_id){
  49. $value->update(['task_id'=>$task_id]);
  50. }
  51. continue;
  52. }
  53. if (empty($url)) {
  54. $value->update(['is_convert'=>0]);
  55. continue;
  56. }
  57. }
  58. //更新url
  59. FileManager::where('id',$value->{'id'})->update(['file_url'=>$url,'old_file_url'=>$value->{'file_url'},'is_convert'=>0]);
  60. }
  61. DB::commit();
  62. return true;
  63. }
  64. /**
  65. * 文件转换pdf记录
  66. * @return bool
  67. */
  68. function queryFileConvert(){
  69. $list = FileManager::where('status',0)
  70. ->where('is_convert',1)
  71. ->where('task_id','<>','')
  72. ->orderBy('id','asc')
  73. ->limit(20)
  74. ->get();
  75. if (count($list) <= 0) {
  76. return false;
  77. }
  78. foreach ($list as $item){
  79. $url= WpsServer::creatServer()->queryConvertPdf($item['task_id']);
  80. if($url){
  81. $item->update(['file_url'=>$url,'old_file_url'=>$item->{'file_url'},'is_convert'=>0]);
  82. }
  83. }
  84. }
  85. /**
  86. * 合同文件更新
  87. * @return bool
  88. */
  89. function setFactoringContract(){
  90. $list=FactoringContractFile::where('file_pdf','')->where('task_id','')->where('is_end',1)->select(['id','file_path','file_url','file_pdf'])->limit(10)->get();
  91. if($list->count()<=0){
  92. return false;
  93. }
  94. foreach ($list as $value){
  95. $task_id= $this->wordToPdf($value->{'file_url'});
  96. if($task_id){
  97. $value->update(['task_id'=>$task_id]);
  98. }
  99. }
  100. }
  101. /**
  102. * 查询合同转换结果
  103. * @return bool
  104. */
  105. function queryFactoringContract(){
  106. $list=FactoringContractFile::where('file_pdf','')->where('task_id','<>','')->select(['id','file_path','file_url','task_id','file_pdf','type','file_no'])->limit(10)->get();
  107. if($list->count()<=0){
  108. return false;
  109. }
  110. foreach ($list as $item){
  111. $url= WpsServer::creatServer()->queryConvertPdf($item['task_id']);
  112. if($url){
  113. $item->update(['file_pdf'=>$url]);
  114. if($item->{'type'}==2){
  115. FactoringContract::where('file_no',$item['file_no'])->update(['contracts_pdf'=>$url]);
  116. }
  117. }
  118. }
  119. }
  120. /**
  121. * word转pdf
  122. * @param $wordUrl
  123. * @param $id
  124. * @param $type
  125. * @return string
  126. */
  127. function wordToPdf($wordUrl){
  128. $fileName=CommonServer::creatServer()->getFileName($wordUrl);
  129. return WpsServer::creatServer()->fileConvertPdf($wordUrl,$fileName);
  130. // $storageUrl='storage/pdf/'.date('Ymd').'/';
  131. // $path=public_path($storageUrl);
  132. // $fileUrl=$path.time().$fileName;
  133. // if(!is_dir($path)){
  134. // mkdir($path,0777,true);
  135. // }
  136. // $extension = pathinfo($fileName);
  137. //// file_put_contents($fileUrl ,file_get_contents($wordUrl));
  138. ////// $content = file_get_contents('https://wealfavor-1257406827.cos.ap-beijing.myqcloud.com/git.docx');
  139. ////// dd($content);
  140. //// dd($fileUrl);
  141. //// $phpWord = IOFactory::load($fileUrl);
  142. //// $wordHtml = IOFactory::createWriter($phpWord,'HTML');
  143. //// $fileHtml=$path.$fileName.'.html';
  144. //// $wordHtml->save($fileHtml);
  145. //// $html=file_get_contents($fileHtml);
  146. //// // 设置Dompdf选项
  147. //// $dompdf = new Dompdf();
  148. //// $dompdf->loadHtml($html,'UTF-8');
  149. //// $dompdf->setPaper('A4', 'portrait');
  150. //// $dompdf->render();
  151. //// $pdf = $dompdf->output();
  152. ////
  153. ////// 将PDF写入文件
  154. //// file_put_contents($path.$fileName.'.pdf', $pdf);
  155. // // 创建一个新的Word文档实例
  156. // file_put_contents($fileUrl ,file_get_contents($wordUrl));
  157. // $phpWord = IOFactory::load($fileUrl);
  158. //
  159. // // 创建一个新的FPDF实例
  160. //// $pdf = new \FPDF();
  161. // $pdf = new \PDF_Chinese();
  162. // $pdf->AddGBFont('simhei','黑体');
  163. // $pdf->AddGBFont('simkai','楷体_GB2312');
  164. //
  165. // // 遍历Word文档中的所有部分并转换为PDF
  166. // foreach ($phpWord->getSections() as $section) {
  167. // // 添加一页PDF
  168. // $pdf->AddPage();
  169. //// $pdf->setFont('freeserif');
  170. // $pdf->SetFont('simhei','',16);
  171. //// $pdf->AddFont('GB','', 'song.cng.php');
  172. //// $pdf->AddFont('GB','B', 'song.cnb.php');
  173. // // 遍历文档部分中的所有元素
  174. // foreach ($section->getElements() as $element) {
  175. // // 处理文本元素
  176. //
  177. // if (method_exists($element, 'getText')) {
  178. // $text = $element->getText();
  179. // $text = mb_convert_encoding($text, 'GBK', 'UTF-8');
  180. // $pdf->Cell(0, 5, $text, 0, 1, 'L');
  181. // }
  182. //
  183. // // 处理图像元素
  184. // if (method_exists($element, 'getImage')) {
  185. // $image = $element->getImage(0);
  186. // $pdf->Image($image->getSource(), $pdf->GetX(), $pdf->GetY(), $image->getWidth(), $image->getHeight());
  187. // }
  188. //
  189. // // 处理其他元素类型...
  190. // }
  191. // }
  192. // $fileName=str_replace($extension['extension'],'',$fileName);
  193. // // 保存PDF到文件
  194. // $pdf->Output($path.$fileName.'pdf', 'F');
  195. // return 'http://'.env('APP_ADMIN_HOST').'/'.$storageUrl.$fileName.'pdf';
  196. }
  197. /**
  198. * 图片转pdf
  199. * @param $imgUrl
  200. * @return string
  201. */
  202. function imgToPdf($imgUrl){
  203. $fileName=CommonServer::creatServer()->getFileName($imgUrl);
  204. $storageUrl='storage/pdf/'.date('Ymd').'/';
  205. $path=public_path($storageUrl);
  206. if(!is_dir($path)){
  207. mkdir($path,0777,true);
  208. }
  209. // $base64Img=base64_encode(file_get_contents($imgUrl));
  210. // $extension = pathinfo($fileName);
  211. // $htmlContent = '<html><body><img src="data:image/'.$extension['extension'].';base64,'.$base64Img.'" /></body></html>';
  212. // dd($htmlContent);
  213. // $dompdf = new Dompdf();
  214. // $dompdf->loadHtml($htmlContent,'UTF-8');
  215. // $dompdf->setPaper('A4', 'portrait');
  216. // $dompdf->render();
  217. // $pdf = $dompdf->output();
  218. // // 将PDF写入文件
  219. // $fileName=str_replace($extension['extension'],'',$fileName);
  220. // file_put_contents($path.$fileName.'pdf', $pdf);
  221. $pdf = new \FPDF();
  222. $pdf->AddPage();
  223. // 获取图片尺寸
  224. list($width, $height) = getimagesize($imgUrl);
  225. $extension = pathinfo($fileName);
  226. // 获取PDF页面宽度
  227. $pageWidth = $pdf->getPageWidth()-10;
  228. $pageHeight = $pdf->GetPageHeight()-10;
  229. // 计算图片缩放比例,使其宽度与页面宽度相匹配
  230. $scale = $pageWidth / $width;
  231. // 计算缩放后的高度
  232. $newHeight = $height * $scale;
  233. if($pageHeight<$newHeight){
  234. $newHeight=$pageHeight;
  235. $scale = $newHeight / $height;
  236. $newWidth=$width * $scale;
  237. }else{
  238. $newWidth=$pageWidth;
  239. }
  240. // 将图片添加到PDF,并设置缩放比例
  241. $pdf->Image($imgUrl, 5, 5, $newWidth, $newHeight, strtoupper($extension['extension']));
  242. $fileName=str_replace($extension['extension'],'',$fileName);
  243. $pdf->Output('F', $path.$fileName.'pdf');
  244. return 'https://'.env('APP_ADMIN_HOST').'/'.$storageUrl.$fileName.'pdf';
  245. }
  246. }