PdfServer.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. * word转pdf
  26. * @param $wordUrl
  27. * @param $id
  28. * @param $type
  29. * @return string
  30. */
  31. function wordToPdf($wordUrl){
  32. $path=public_path('/storage/pdf/'.date('Ymd'));
  33. $file='/home/www/4.8.docx';
  34. $command = "libreoffice --headless --convert-to pdf --outdir {$path} {$file}";
  35. exec($command);
  36. // $fileName=CommonServer::creatServer()->getFileName($wordUrl);
  37. // return WpsServer::creatServer()->fileConvertPdf($wordUrl,$fileName);
  38. // $storageUrl='storage/pdf/'.date('Ymd').'/';
  39. // $path=public_path($storageUrl);
  40. // $fileUrl=$path.time().$fileName;
  41. // if(!is_dir($path)){
  42. // mkdir($path,0777,true);
  43. // }
  44. // $extension = pathinfo($fileName);
  45. //// file_put_contents($fileUrl ,file_get_contents($wordUrl));
  46. ////// $content = file_get_contents('https://wealfavor-1257406827.cos.ap-beijing.myqcloud.com/git.docx');
  47. ////// dd($content);
  48. //// dd($fileUrl);
  49. //// $phpWord = IOFactory::load($fileUrl);
  50. //// $wordHtml = IOFactory::createWriter($phpWord,'HTML');
  51. //// $fileHtml=$path.$fileName.'.html';
  52. //// $wordHtml->save($fileHtml);
  53. //// $html=file_get_contents($fileHtml);
  54. //// // 设置Dompdf选项
  55. //// $dompdf = new Dompdf();
  56. //// $dompdf->loadHtml($html,'UTF-8');
  57. //// $dompdf->setPaper('A4', 'portrait');
  58. //// $dompdf->render();
  59. //// $pdf = $dompdf->output();
  60. ////
  61. ////// 将PDF写入文件
  62. //// file_put_contents($path.$fileName.'.pdf', $pdf);
  63. // // 创建一个新的Word文档实例
  64. // file_put_contents($fileUrl ,file_get_contents($wordUrl));
  65. // $phpWord = IOFactory::load($fileUrl);
  66. //
  67. // // 创建一个新的FPDF实例
  68. //// $pdf = new \FPDF();
  69. // $pdf = new \PDF_Chinese();
  70. // $pdf->AddGBFont('simhei','黑体');
  71. // $pdf->AddGBFont('simkai','楷体_GB2312');
  72. //
  73. // // 遍历Word文档中的所有部分并转换为PDF
  74. // foreach ($phpWord->getSections() as $section) {
  75. // // 添加一页PDF
  76. // $pdf->AddPage();
  77. //// $pdf->setFont('freeserif');
  78. // $pdf->SetFont('simhei','',16);
  79. //// $pdf->AddFont('GB','', 'song.cng.php');
  80. //// $pdf->AddFont('GB','B', 'song.cnb.php');
  81. // // 遍历文档部分中的所有元素
  82. // foreach ($section->getElements() as $element) {
  83. // // 处理文本元素
  84. //
  85. // if (method_exists($element, 'getText')) {
  86. // $text = $element->getText();
  87. // $text = mb_convert_encoding($text, 'GBK', 'UTF-8');
  88. // $pdf->Cell(0, 5, $text, 0, 1, 'L');
  89. // }
  90. //
  91. // // 处理图像元素
  92. // if (method_exists($element, 'getImage')) {
  93. // $image = $element->getImage(0);
  94. // $pdf->Image($image->getSource(), $pdf->GetX(), $pdf->GetY(), $image->getWidth(), $image->getHeight());
  95. // }
  96. //
  97. // // 处理其他元素类型...
  98. // }
  99. // }
  100. // $fileName=str_replace($extension['extension'],'',$fileName);
  101. // // 保存PDF到文件
  102. // $pdf->Output($path.$fileName.'pdf', 'F');
  103. // return 'http://'.env('APP_ADMIN_HOST').'/'.$storageUrl.$fileName.'pdf';
  104. }
  105. /**
  106. * 图片转pdf
  107. * @param $imgUrl
  108. * @return string
  109. */
  110. function imgToPdf($imgUrl){
  111. $fileName=CommonServer::creatServer()->getFileName($imgUrl);
  112. $storageUrl='storage/pdf/'.date('Ymd').'/';
  113. $path=public_path($storageUrl);
  114. if(!is_dir($path)){
  115. mkdir($path,0777,true);
  116. }
  117. // $base64Img=base64_encode(file_get_contents($imgUrl));
  118. // $extension = pathinfo($fileName);
  119. // $htmlContent = '<html><body><img src="data:image/'.$extension['extension'].';base64,'.$base64Img.'" /></body></html>';
  120. // dd($htmlContent);
  121. // $dompdf = new Dompdf();
  122. // $dompdf->loadHtml($htmlContent,'UTF-8');
  123. // $dompdf->setPaper('A4', 'portrait');
  124. // $dompdf->render();
  125. // $pdf = $dompdf->output();
  126. // // 将PDF写入文件
  127. // $fileName=str_replace($extension['extension'],'',$fileName);
  128. // file_put_contents($path.$fileName.'pdf', $pdf);
  129. $pdf = new \FPDF();
  130. $pdf->AddPage();
  131. // 获取图片尺寸
  132. list($width, $height) = getimagesize($imgUrl);
  133. $extension = pathinfo($fileName);
  134. // 获取PDF页面宽度
  135. $pageWidth = $pdf->getPageWidth()-10;
  136. $pageHeight = $pdf->GetPageHeight()-10;
  137. // 计算图片缩放比例,使其宽度与页面宽度相匹配
  138. $scale = $pageWidth / $width;
  139. // 计算缩放后的高度
  140. $newHeight = $height * $scale;
  141. if($pageHeight<$newHeight){
  142. $newHeight=$pageHeight;
  143. $scale = $newHeight / $height;
  144. $newWidth=$width * $scale;
  145. }else{
  146. $newWidth=$pageWidth;
  147. }
  148. // 将图片添加到PDF,并设置缩放比例
  149. $pdf->Image($imgUrl, 5, 5, $newWidth, $newHeight, strtoupper($extension['extension']));
  150. $fileName=str_replace($extension['extension'],'',$fileName);
  151. $pdf->Output('F', $path.$fileName.'pdf');
  152. return 'https://'.env('APP_ADMIN_HOST').'/'.$storageUrl.$fileName.'pdf';
  153. }
  154. }