Laravel using domPDF - php

I want to download a blade. I am using dompdf.
public function download($id)
{
$booking=Booking::where('id',$id)->first();
$pdf = PDF::loadView('pages.booking.bookingdetails',$booking);
return $pdf->download('bookingdetails.pdf');
}
bookingdetails.blade.php
<div class="container">
<div class="card">
<div class="card-header">
Invoice # {{$booking->id}}
<strong>01/01/01/2018</strong>
<span class="float-right"> <strong>Status:</strong> Pending</span>
</div>
</div>
</div>
So what I want to do is passing the $booking variable from the method and send to the view. Then download it as a pdf file.
But the error is:
"Undefined variable: booking

I'm shooting in the dark here, shouldn't it be:
$pdf = PDF::loadView('pages.booking.bookingdetails', ['booking' => $booking]);

You need to pass an array as the second argument to the view with the index name of booking so try this:
$pdf = PDF::loadView('pages.booking.bookingdetails',['booking' => $booking]);

Related

How to print an image with DomPDF in laravel?

I am making a pdf, for which I use the library: barryvdh/laravel-dompdf, in the pdf I try to print an image, with which when I open the recently downloaded file, all the data is printed except the image, I clarify this image if it is displayed correctly in my show.blade view, I cannot find a solution for this problem.
Below I detail how I am proceeding:
Controller: (fragment of store method and exportPdf method)
public function store(SaveDocumentRequest $request)
{
if ($request->hasFile('photo')) {
$file = $request->file('photo');
$name = time().$file->getClientOriginalName();
$file->move(public_path().'/images/', $name);
}else{
$name = null;
}
$document->photo = $name;
public function exportPdf()
{
$movie = Movies::with('document.creator', 'actors', 'generate_movie', 'document.adequacy', 'document.lenguage')->first();
$pdf = PDF::loadView('admin.movies.show', compact('movie'));
return $pdf->download('cine.pdf');
}
My show.blade file:
<div class="col-md-6">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Imagen de Portada </h3>
</div>
<div class="box-body box-profile">
<img class="img-responsive" src="{{ asset('/images/'.$movie->document->photo) }}" alt="{{ $movie->document->title }}" >
</div>
</div>
</div>
This way I am accessing the image that if it is displayed correctly in my show.blade file, but it is not displayed in the downloaded pdf.
I clarify I do not receive any type of error and the image is stored in the public folder of the project.
Could you guide me to find the solution?
Its simple just use this I have used same package and added image on pdf like this.
<img src="{{asset('foldername/path_to_image')}}">

How can I get the values of an array?

I am trying to access the properties of an array from a view. A controller is passing the array to that view. For some reason am not able to access the the array properties.
Error Message:
Trying to get property 'message' of non-object (View: /path/to/file/message.blade.php)
View code + where error is occurring:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{$message->title}}</div>
<div class="card-body">
{{$message->message}}
</div>
</div>
</div>
</div>
</div>
#endsection
Controller code that is returning the above view:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use App\User;
class becomeorganiser extends Controller
{
public function becomeorganiser(){
$user = Auth::user();
$user->organiser = 1;
$user->save();
$message = [];
$message['title'] = 'Success!';
$message['message'] = 'You are now an event organiser<br>You now have access the oragnisers control panel in your navigation bar!';
return view('message', $message);
}
}
If I do {{print_r($message)}} The contents is printed out. To be clear I cannot access either the title or message properties
What am I doing wrong?
$message is not an object, yet you are accessing it as one. It is an array as you have defined it as such in your controller ($message = [];), therefore you need to access it as such.
So, it should be like this,
<div class="card-header">{{ $message['title'] }}</div>
<div class="card-body">
{{ $message['message'] }}
</div>
Hence, the error:
Trying to get property 'message' of non-object (View: /path/to/file/message.blade.php)
is completely valid.
Reading Material
array
object
Edit #1
Regarding your new error,
Illegal string offset 'title' (View:.....
As per my comment and your update, you are using numeric keys yet the array has been defined as an associative array. Please read above again this time noticing how I am accessing the values from the array.

Download .PDF at the next page after successful form insertion in Laravel

I have a form, that user has to fill out. After pressing submit, the user will be redirected to page called /medicines/result with a success message and download button for the .PDF. I cannot figure out how to download the pdf on the download page. If I use the pdf method in the controller, it works fine.
I am using this for pdf: barryvdh/laravel-dompdf
What I have:
public function store(StoreConfirmationRequest $request) {
$formData = $request->all();
$prescription_confirmation = new Prescription_confirmations;
$prescription_confirmation->physician_id = Input::get('physician_id');
$prescription_confirmation->patient_code = Input::get('patient_code');
$prescription_confirmation->medicine = 1;
$prescription_confirmation->date = Input::get('physician_date');
//finally save the data to database
$prescription_confirmation->save();
$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML('<h1>Test</h1>');
$pdf->download();
return view('medicines.result', compact('formData'))->with('message', 'Successfully inserted);
}
Here is the blade
#extends ('layouts/master')
#section('title', 'Allalaadimine')
#section ('content')
<div class="container medicine_confirmation_container">
<div class="col-md-12">
#foreach ($formData as $data)
<li>{{$data}}</li>
#endforeach
<div class="flash-message">
#if(session()->has('message'))
<div class="alert-message alert-message-success">
<h4>Saved!</h4>
{{ session()->get('message') }}
</div>
#endif
</div> <!-- end .flash-message -->
<button class="btn btn-borders btn-success btn-lg btnDownloadPdf" type="submit">Lae alla pdf</button>
</div>
</div>
#endsection
#section ('footer')
#endsection
Returning both the PDF and a view won't work. You have to save the PDF on your server and make it accessible to download by another route.
From: https://github.com/barryvdh/laravel-dompdf
$pdf->save('/path-to/my_stored_file.pdf');
In the blade template you have to link your saved PDF.

Pass variable from controller to another in Symfony?

I have 2 actions functions in my controller and I want to pass variable to an other action function
This is my first function in my controller
public function newUserAction(Request $request)
{ .........
$url = $this->generateUrl('userBundle_new_user_reasonCodeAjaxView',
array('id' => $newUser->getCode(),
'countCode' => $countCode,));
return $this->redirect($url);
This my second funtion in my controller
public function userCodeAjaxViewAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$pc = $em->getRepository('UserBundle:Code')->find($id);
if($pc != null)
{
return $this->render('UserBundle:userCodeView.html.twig', array(
'pc' => $pc,
));
}
And my twif looks like this
<div class="">
<div class="panel panel-default step3-textarea top-arrow top-arrow">
<div class="panel-body">
<fieldset>
<div>
{{ pc.name|trans }}
{{countCode}}
</div>
</fieldset>
</div>
</div>
</div>
I am getting an error Variable "countCode" does not exist in...
Is there any idea how I can use a variable from a controller in a other controller?
You can not get a variable from another controller.
Every request creates only one controller instance.
What you can do is create a Service that you can call in the controller.
Something like that:
class CountUserService{
public function count(){
return 1; // count users here
}
}
And the in the controller do this:
$service = new CountUserService();
$data=['countCode' => $service->count()];
return $this->render('UserBundle:userCodeView.html.twig', $data);
You are using the same template scope for the two controllers, and this is not possible. If you are using two controllers to render two simple informations, why not simply returning a JSON and displaying it in a more global template using AJAX ?
Else a pure symfony solution would be to have a main template view.html.twig where you would put
<div class="">
<div class="panel panel-default step3-textarea top-arrow top-arrow">
<div class="panel-body">
<fieldset>
<div>
{{ render(controller("AppBundle:UserController:newUserAction")) }}
{{ render(controller("AppBundle:UserController:userCodeAjaxViewAction")) }}
</div>
</fieldset>
</div>
</div>
Given that then your two controller action templates would be simple {{ pc.name|trans }} and {{countCode}}.
Hope it helps you out !

Blank page on laravel 5

I just try to create a profile page i create a Route like this:
Route::get('profile/{id}/{name}','ProfileController#index');
and the index function like this :
public function index($id,$name)
{
$user = \App\User::find($id);
return view('pages.profile',compact('user'));
}
profile view:
#extends('app')
#section('content')
<div class="container" style="margin-top: 50px;padding: 30px;">
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12">
<div class="proPic"><img src="{{user->image}}" class="img-responsive" alt="Image"></div>
</div>
</div>
</div>
#endsection
but when I lunch the link i get a blank page
If you have a default installation, you probably need to have:
#extends('layouts.app')
instead of
#extends('app')
and change
{{user->image}}
to
{{ $user->image }}
Your code looks clean, a blank page can occur when laravel doesn't displays errors. I'd recommand you to :
1- Check the server response (HTTP code, content...)
2- Check your logs, maybe you'll discover a problem of configuration

Categories