如何使用tcpdf laravel 5使用刀片视图生成pdf? [英] how to generate pdf using blade view with tcpdf laravel 5?
问题描述
我正在尝试使用laravel 5和 https://github.com/elibyy/创建一个pdf文件laravel-tcpdf
I'm trying to create a pdf using laravel 5 and https://github.com/elibyy/laravel-tcpdf
我的客户填写表格并单击提交"时,我有20页用laravel刀片书写的表格,我想为他生成pdf并保存
i have a form of 20 pages writing in laravel blade when my client fill the form and click submit i want to generate pdf for him and save it
我试图在我的控制器中这样做
i try to do it like this in my controller
public function createPDF()
{
$pdf = Input::get('pdf',null);
$company = Input::get('company',null);
$branch = Input::get('branch',null);
$sub_branch = Input::get('sub_branch',null);
$form_type = Input::get('form_type',null);
$form_name = Input::get('form_name',null);
$form_heb_name = Input::get('form_heb_name',null);
$sig_path=FormsController::getSignature_file();
$data=compact('company','branch','sub_branch','form_type','form_name','form_heb_name','sig_path');
Input::flash();
if ($pdf)
{
$pdf = new TCPDF();
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->AddPage();
$pdf->writeHTML(view('forms.'.$company.'.'.$branch.'.'.$sub_branch.'.'.$form_type.'.'.$form_name, $data)->render());
$filename = storage_path().'/forms_pdf/10006/26/4718326/'.$form_name.'.pdf';
$pdf->output($filename, 'I');
return redirect('forms');
}
return view('forms.'.$company.'.'.$branch.'.'.$sub_branch.'.'.$form_type.'.'.$form_name , $data);
}
无法正常工作,它会创建2个pdf页面,且所有字段相互重叠
bat its not working it's create 2 pdf page with All fields on top of each other
如何解决?
此外,我想以一种无法编辑的方式保存pdf文件,我该怎么办?
谢谢.
推荐答案
我将尝试为您提供逐步的答案(经过laravel 5.1测试):
I will try to give you a step by step answer (tested for laravel 5.1):
在使用它之前,请确保正确安装了TCPDF服务提供程序:
Before using it, be sure that you installed the TCPDF service provider correctly:
安装TCPDF服务提供商
在composer.json中,添加以下软件包:
In composer.json, add this package:
"require": {
"elibyy/laravel-tcpdf": "0.*"
}
运行作曲家更新
在config/app.php中添加此服务提供商
in config/app.php add this service provider
'providers' => [
//..
Elibyy\TCPDF\ServiceProvider::class,
]
运行 php artisan vendor:publish ,它将在您的文件中生成config/laravel-tcpdf.php
Run php artisan vendor:publish , that will generate config/laravel-tcpdf.php in your files
在您的控制器中生成pdf
public function createPDF()
{
[...]
//get default settings from config/laravel-tcpdf.php
$pdf_settings = \Config::get('laravel-tcpdf');
$pdf = new \Elibyy\TCPDF\TCPdf($pdf_settings['page_orientation'], $pdf_settings['page_units'], $pdf_settings['page_format'], true, 'UTF-8', false);
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->AddPage();
$pdf->writeHTML(view('forms.'.$company.'.'.$branch.'.'.$sub_branch.'.'.$form_type.'.'.$form_name, $data)->render());
$filename = storage_path().'/forms_pdf/10006/26/4718326/'.$form_name.'.pdf';
$pdf->output($filename, 'I');
return redirect('forms');
}
这篇关于如何使用tcpdf laravel 5使用刀片视图生成pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!