I'm upgrading from Laravel 7 to 8 and would like to switch to barryvdh/laravel-dompdf for PDF generation. I was using niklasravnsborg/laravel-pdf up until now, but since that package doesn't support Laravel 8, I need to switch. So I am in the processing of altering my existing code to use barryvdh/laravel-dompdf, but I'm running into an issue.
This is my (simplified) controller:
public function update(Request $request) {
$invoice = Invoice::find($request->invoice_id);
if(isset($request->export) AND $request->export == 1) {
$this->exportInvoice($invoice, $request);
}
}
This exportInvoice function is in the same controller file.
I'm using this to generate a test PDF:
$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML('<h1>Test</h1>');
return $pdf->stream();
Now I've managed to narrow down the issue to the place in my code where the PDF generation fails.
If I put the PDF generation code in the if statement in the update function above, then I get the expected result: a simple PDF file.
However, as soon as I move this piece of code to the exportInvoice function, I get a simple blank web page.
I've been googling around, but I was unable to find similar issues.
I've tried putting all my code together in the update function and guess what ... This works as expected. It's as if I'm doing something wrong with the subfunctions, but I can't figure out what.
Does anybody see what I'm doing wrong?
From your update() method, this will stream a PDF back to the browser:
return $pdf->stream();
But from a exportInvoice(), called by the update() method, that will just return the stream back to the update() method. If you don't do anything with it there, it won't reach the browser. You need to return the response returned from exportInvoice().
public function update(Request $request) {
$invoice = Invoice::find($request->invoice_id);
if(isset($request->export) AND $request->export == 1) {
// Note we need to *return* the response to the browser
return $this->exportInvoice($invoice, $request);
}
}
public function exportInvoice($invoice, $request) {
$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML('<h1>Test</h1>');
return $pdf->stream();
}
Related
OK, so Lets start with code
App\Models\User
public function registerMediaConversions(Media $media = null): void
{
$this
->addMediaConversion('thumb')
->width(368)
->height(232)
->extractVideoFrameAtSecond(1)
->performOnCollections('stories')
->nonQueued();
}
media-library.php
'ffmpeg_path' => env('FFMPEG_PATH', '/usr/local/bin/ffmpeg'),
'ffprobe_path' => env('FFPROBE_PATH', '/usr/local/bin/ffprobe'),
Test Route
Route::get('test', function() {
$video = new \Spatie\MediaLibrary\Conversions\ImageGenerators\Video();
echo $video->convert('demo.mov');
});
StoryController.php (snip)
$model = Auth::user();
//if request is a video
if($request->hasFile('video') && $request->file('video')->isValid()){
$media = $model->addMediaFromRequest('video')
->toMediaCollection('stories');
}
When the video is uploaded, it is added to the library successfully. The issue is no thumbnail is being generated based on the model information. When I use the test route, it does generate the thumbnail successfully. I am not sure what I am missing but after 8 hours I am starting to wonder if this is a bug or if there is more troubleshooting I can do.
Nothing is showing up in the laravel.log, nothing is showing up on the frontend, nothing is showing up out of the ordinary in Telescope so I am at a loss.
When trying to update a resource in Laravel Nova that has a Observer the update loads for a while and then ends with a 502 error. The observer is registered correctly (the created method works fine) and I'm not trying to do anything special in the updated method. Any ideas?
public function updated(Model $model)
{
//
$model->title = 'test';
$model->save();
}
If I try this without the $model->save(), there is no 502 error but the change I want to happen also doesn't happen. I get the green success message, and any change I make on the form prior to updating occurs, but not the change I'm trying to make during the updated method.
Any help troubleshooting this would be appreciated
I am not very good at Laravel, but I think, that you should to try this:
In your model file add method:
public function saveQuietly(array $options = [])
{
return static::withoutEvents(function () use ($options) {
return $this->save($options);
});
}
Then, in your updated method in observer do something like this:
public function updated(Model $model)
{
$model->title = 'test';
$model->saveQuietly();
}
I have a problem in laravel-dompdf. I think my code is right and I do not know why it directs to a white page instead of downloading a pdf. It is working in other controller but here is not.
My code in my controller:
public function export_pdf()
{
if(Auth::user()->campus_id == 0){
$processes = Process::join('bags','processes.bag_id', '=', 'bags.id')
->join('stations','processes.station_id', '=', 'stations.id')
->select('bags.*','stations.*','processes.*')
->where('stations.campus_id', Session::get('test'))
->orderBy('processes.id', 'desc')
->get();
// Send data to the view using loadView function of PDF facade
$pdf = PDF::loadView('collectionReportspdf', compact('processes'));
// If you want to store the generated pdf to the server then you can use the store function
$pdf->save(storage_path().'_filename.pdf');
// Finally, you can download the file using download function
return $pdf->download('reports.pdf');
}
else{
$processes = Process::join('bags','processes.bag_id', '=', 'bags.id')
->join('stations','processes.station_id', '=', 'stations.id')
->select('bags.*','stations.*','processes.*')
->where('stations.campus_id', Auth::user()->campus_id)
->orderBy('processes.id', 'desc')
->get();
// Send data to the view using loadView function of PDF facade
$pdf = PDF::loadView('collectionReportspdf', compact('processes'));
// If you want to store the generated pdf to the server then you can use the store function
$pdf->save(storage_path().'_filename.pdf');
// Finally, you can download the file using download function
return $pdf->download('reports.pdf');
}
}
In the past, I've had to render the PDF before saving or outputting to the browser.
Try adding the following above $pdf->save():
$pdf->render();
If it still doesn't work, try replacing the return statement with echo.
I am learning Laravel 5.4, Still new to Laravel and PHPUnit. Everything is working great after following online basic tutorial.
This test function is working correctly when run phpunit
public function testBasicExample()
{
$response = $this->call( 'GET' , '/welcome');
$this->assertTrue(strpos($response->getContent(), 'Laravel') !== false);
}
Problem comes when I try to test Api
Steps I took
Create Api route for books
Return all users from users talbe as json from localhost/api/books/
public function index()
{
$users = DB::table('users')->get()->toJson();
echo $users;
}
I open the link in browser and json is returned correctly
copy and pasted json into online json validator jsonlint and it is valid.
Create a new test function
public function test_index_method_returns_all_books()
{
$response = $this->call( 'GET' , '/api/books/');
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getContent(),true);
$this->assertJson($data);
}
run phpunit
200 status test passed but assertJson did not pass.
I tried to do var_dump for $response->getContent() and found out it return empty.
now I am not able to get getContent() for api/book/. Does anyone know if there is a solution for this?
Thanks.
Here is a screenshot
Try create some data with a factory before you call the api:
e.g.:
factory(\App\Books::class, 20)->create();
then
$response = $this->call( 'GET' , '/api/books/');
If you had set ":memory:" as your database on phpunit.xml, you will no longer see any data from your local database, that's why you should use factory instead.
I am intentionally making error i.e. using one of the coulmn's name wrong to learn how to handle the error with ajax call with codeigniter.
Last Comment in controller function at the end is my question/problem
My AJX Code is following. I am using ajaxform plugin. It shows me all response from controller perfectly but problem is only I am unable to get response from model in controller while using ajax call that is little weird
$('#myForm').ajaxForm
({
success: function(responseText)
{
if(responseText != "1")
$('#feedback').html(responseText);
}
});
Following is snapshot of exact error which i can see in console but unable to get in controller and hence in view. It describes complete error i.e unknown column in given query, but i captured only upper portion.
My model function is below
public function updateItem($id, $data, $tbl)
{
$this->db->where('id', $id);
$r = $this->db->update($tbl, $data);
if($r)
return $r;
else
{
$r = $this->db->_error_message();
return $r;
}
}
My controller function code
public function upadteme()
{
$r = $this->ajax_model->updateItem($uid, $data, 'users');
echo $r." -- "; // Unable to get this echo working
//when using ajaxcall (calling controller through ajax) otherwise fine
}
It looks like the class will not populate _error_message if db_debug is on which it appears to be by default.
In config/database.php, set
$db['default']['db_debug'] = FALSE;
and try again.