how to load blade view with tcpdf laravel 5? - pdf-generation

I'm trying to create a pdf using laravel 5 and https://github.com/elibyy/laravel-tcpdf
Is there a way to load view and passing data to the view?
something like
$pdf = new TCPDF();
$pdf -> SetPrintHeader(false);
$pdf -> SetPrintFooter(false);
$pdf -> loadView('pdf.invoice',$data); //
$pdf -> Output(storage_path().'/pdf/file.pdf', 'I');
thanks.

Try using
$pdf->writeHTML(view('your.view')->render());
However, the functionnalities of writeHTML() method are limited, see TCPDF documentation.
http://www.tcpdf.org/doc/code/classTCPDF.html#ac3fdf25fcd36f1dce04f92187c621407

Try using
$view = \View::make('myview_name',compact('dynamic_data'));
$html = $view->render();
$pdf = new TCPDF();
$pdf::SetTitle('Hello World');
$pdf::AddPage();
$pdf::writeHTML($html, true, false, true, false, '');
$pdf::Output('hello_world.pdf');

Related

Laravel: attach multiple PDFs to email

Following is a code to send a PDF with email:
$pdf = PDF::loadHTML($str_html)->setPaper('a4', 'portrait');
Mail::send('emails.mail', $data, function($message) use ($data,$pdf){
$message->from('noreply#...');
$message->to('...');
$message->subject('test test');
//Attach PDF doc
$message->attachData($pdf->output(),'invoice.pdf');
});
But my requirement is to send multiple PDFs with email, how can I do this?
$attachments = [
// first attachment
'/path/to/file1',
// second attachment
'/path/to/file2',
...
];
$pdf = PDF::loadHTML($str_html)->setPaper('a4', 'portrait');
Mail::send('emails.mail', $data, function($message) use ($data,$pdf){
$message->from('noreply#...');
$message->to('...');
$message->subject('test test');
//Attach PDF doc
foreach($attachments as $filePath){
$message->attach($filePath);
}
});
taibur rahman provides an answer in comment, new PDFs variables must be added in "use", then code will be correct.
Complete Code:
$pdf = PDF::loadHTML($str_html)->setPaper('a4', 'portrait');
$pdf2 = PDF::loadHTML($str_html2)->setPaper('a4', 'portrait');
Mail::send('emails.mail', $data, function($message) use ($data, $pdf, $pdf2){
$message->from('...#...');
$message->to(...#...);
$message->subject('test test');
$message->attachData($pdf->output(),'test.pdf');
$message->attachData($pdf2->output(),'test2.pdf');
});

Use Laravel to Download table as xls

I want to download the data of table in excel file
I have an application in Laravel and I want to add a functionality that download user data in .xls format
Route::get('users/download', function(){
$table = App\User::all();
$filename = "User.csv";
$handle = fopen($filename, 'w+');
fputcsv($handle, array('ID', 'User Name'));
foreach($table as $row) {
fputcsv($handle, array($row['id'], $row['username']));
}
fclose($handle);
$headers = array(
'Content-Type' => 'text/csv'
);
return Response::download($filename, 'User.csv', $headers);
});
This code works fine for .csv files what changes are required for .xls file?
Take a look at Laravel Excel: https://github.com/Maatwebsite/Laravel-Excel
Its very easy to use with Laravel.

Call to undefined method Intervention\Image\Facades\Image::make()

I upgraded from Laravel 4.2 to Laraveld5.3 with intervention/image : "^2.3",
if (Input::hasFile('logo')) {
$path = public_path()."/assets/admin/layout/img/";
File::makeDirectory($path, $mode = 0777, true, true);
$image = Input::file('logo');
$extension = $image->getClientOriginalExtension();
$filename = "logo.$extension";
$filename_big = "logo-big.$extension";
Image::make($image->getRealPath())->save($path.$filename);
Image::make($image->getRealPath())->save($path.$filename_big);
$data['logo'] = $filename;
}
The result Is, got the error below:
Call to undefined method Intervention\Image\Facades\Image::make()
I experienced the same issue in my Laravel 5.4 project. I stumble on this link
that help resolve the issue. This was the fix that was provided
In config/app change 'aliases' for Image from
'Image' => Intervention\Image\Facades\Image::class,
To
'Image' => Intervention\Image\ImageManagerStatic::class,
Then in your controller header add
use Image;
Make Sure that
In config/app update Providers with
Intervention\Image\ImageServiceProvider::class
and update aliases with
'Image' => Intervention\Image\Facades\Image::class,
In your config/app.php file, add
Intervention\Image\ImageServiceProvider::class,
in providers array and add
'Image' => Intervention\Image\Facades\Image::class,
in aliases array.
Run
php artisan config:cache
command.
In your controller add
use Image;
before class definition.
Now you can use the Image class according to your needs inside the controller's function. suppose,
$imageHeight = Image::make($request->file('file'))->height();
public function optimizeFile(Request $request)
{
$data = $request->file('image');
// dd($data);
if ($request->hasFile('image')) {
foreach($data as $key => $val){
$path=storage_Path('app/public/');
$filename='image-'.uniqid().$key.'.'.'webp';
$val->move($path,$filename);
$p['image']=$filename;
$insert[$key]['image'] = $filename;
//Resize image here
$thumbnailpath[$key]['abc'] = storage_path('app/public/'.$filename);
$img = Image::make($thumbnailpath)->resize(400, 150, function($constraint) {
$constraint->aspectRatio();
});
$img->save($thumbnailpath);
}
return redirect('ROUTE_URL')->with('success', "Image uploaded successfully.");
}
}

Codeigniter variable not found message in sub views

I have stacked with the situation , please anyone help me !
Controller ->
public function index() {
$data = array();
$data ['tittle'] = 'Membership Payment';
$data ['page'] = $this->load->view('membership_payment', $data, true);
$data ['breadcrumb'] = $this->load->view('breadcrumb', $data, true);
$data['result'] = $this->payment_model->membership_payment();
$data['db']='---------------';
$this->load->view('master', $data);
View->master page
<?php echo $db; ?>
Master pages working fine , but problem is i have subpages membership_payment , in this page the data array variable are not working ,
Display the variable not found , there is any trick to pass data array to master subpage
Thanks in Advance !
ROB
I'm assuming that you've used $result which is the returned data of membership_payment(); method in the membership_payment view file.
If so, just change the order of the following line and put that over the $data['page'], as follows:
// ...
$data['result'] = $this->payment_model->membership_payment();
$data ['page'] = $this->load->view('membership_payment', $data, true);
// ...

Magento model->save() does not work

I am using following code for saving data from form to database.
$data = $this->getRequest()->getPost();
$custom['name'] = $data['name'];
$model = Mage::getModel('my/custom_order');
$model->setOrderId($order_id);
$model->setKey('name');
$model->setValue(serialize($custom));
$model->save();
Data does not save in database.But When I pass static data like below, It is Working.
$custom['name'] = 'John';
When I use print_r($data),
Array(
[name] => xyz
[surname] =>
)
Maybe you need:
$data = $this->getRequest()->getPost()->getParams();
Please check this. Check data which sended to server in your browser Network tool.
You need
$params = json_decode(file_get_contents('php://input'));

Resources