项目近期有个业务需求:下载PDF版发票。和公司前辈沟通,有两种方案php
wkhtmltopdf依赖安装(https://wkhtmltopdf.org/downloads.html) 能够直接在官网下载安装包,像服务器经过经过如下方式(找好对应的版本下载呦,我用的是ubantu 16)
wget https://downloads.wkhtmltopdf.org/0.12/0.12.5/wkhtmltox_0.12.5-1.xenial_amd64.deb apt-get install -f dpkg -i wkhtmltox_0.12.5-1.xenial_amd64.deb
若是出现如下错误,就重复下上面第二行命令和第三行命令html
dpkg: error processing package wkhtmltox (--install): dependency problems - leaving unconfigured Errors were encountered while processing: wkhtmltox
$outPut = view('invoice', [ 'orderData' => $orderData ])->render(); //invoice.blade.php是html模板,往里填充动态数据 //将填充完整的html文件存储起来,稍后用来转PDF $htmlName = $this->getHtmlName();//获取html文件名 $htmlDir = $this->getHtmlDir();//获取 html 工做目录 $htmlFullPath = storage_path($htmlDir . $htmlName); file_put_contents($htmlFullPath, $outPut); //设置pdf文件路径 $pdfName = $this->getPdfName($orderData['order_no']);//获取pdf文件名 $pdfDir = $this->getPDFDir();//获取 pdf 工做目录 $pdfFullPath = storage_path($pdfDir . $pdfName); //设置PDF文件的属性 $pdf = new Pdf($htmlFullPath); $pdf->setOptions([ 'no-outline', //'zoom' => 3, 'margin-top' => 0, 'margin-right' => 0, 'margin-bottom' => 0, 'margin-left' => 0, //'disable-smart-shrinking', ]); //保存PDF if (!$pdf->saveAs($pdfFullPath)) { \Log::error('Could not create PDF:', [$pdf->getError()]); return false; }
由于咱们项目使用了多台服务器,因此咱们就把PDF文件先上传到oss再进行下载的,若是不须要能够直接用Header方式下载服务器
header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . $pdfName); readfile($pdfFullPath);
/usr/share/fonts
目录。