答案 0 :(得分:2)
我已经使用这个库在laravel中生成PDF。请看doc。 https://github.com/barryvdh/laravel-dompdf
您也可以获取我的代码帮助。 首先安装库并按照doc中的说明进行配置。 之后,在控制器中
// Put this in top
use Dompdf\Dompdf;
use Dompdf\Options;
创建一个功能
public function DownloadPdf($id , $download = false){
// instantiate and use the dompdf class
$options = new Options();
$options->set('enable_remote', true);
$options->set('enable_css_float', true);
$dompdf = new Dompdf($options);
$dompdf->loadHtmlFile(action('HomeController@PdfHTML',['id'=>$id]));
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
if($download == true){
// Save PDF to server
file_put_contents('uploads/pdf/document_'.$id.'.pdf', $dompdf->output());
}else{
// Output the generated PDF to Browser
$dompdf->stream('document');
}
}
现在加载html文件操作
public function PdfHTML($id){
return view('home.home_pdf');
}
现在创建home.blade.pdf
<h2>Hello world</h2>
您的PDF将会生成。如果您收到任何错误,请告诉我
答案 1 :(得分:0)
Go trough this link指出Output()
的第一个参数是文件路径,第二个参数是保存模式-您需要将其设置为'F'
。
$upload_dir = public_path();
$filename = $upload_dir.'/testing7.pdf';
$mpdf = new \Mpdf\Mpdf();
//$test = $mpdf->Image($pro_image, 0, 0, 50, 50);
$html ='<h1> Project Heading </h1>';
$mail = ' <p> Project Heading </p> ';
$mpdf->autoScriptToLang = true;
$mpdf->autoLangToFont = true;
$mpdf->WriteHTML($mail);
$mpdf->Output($filename,'F');
$mpdf->debug = true;
示例:
$mpdf->Output($filename,'F');
示例2
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('Hello World');
// Saves file on the server as 'filename.pdf'
$mpdf->Output('filename.pdf', \Mpdf\Output\Destination::FILE);