| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?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;
- }
- /**
- * word转pdf
- * @param $wordUrl
- * @param $id
- * @param $type
- * @return string
- */
- function wordToPdf($wordUrl){
- $path=public_path('/storage/pdf/'.date('Ymd'));
- $file='/home/www/4.8.docx';
- $command = "libreoffice --headless --convert-to pdf --outdir {$path} {$file}";
- exec($command);
- // $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';
- }
- }
|