barryvdh / laravel-dompdf

A DOMPDF Wrapper for Laravel
MIT License
6.74k stars 971 forks source link

Issue with php 8.1.4 and Laravel 9 #879

Closed abhikm1991 closed 2 years ago

abhikm1991 commented 2 years ago

Describe the bug Just updated Laravel 8.81 to Laravel 9 ( "laravel/framework": "^8.81" TO "laravel/framework": "^9.0",). Previously I had 0.9 ("barryvdh/laravel-dompdf": "^0.9.0"), Now I updated it into v1.0 ("barryvdh/laravel-dompdf": "^1.0"). After update PDF generating is not working. The Facade is renamed from Barryvdh\DomPDF\Facade to Barryvdh\DomPDF\Facade\Pdf.

"message": "Barryvdh\DomPDF\PDF::loadView(): Argument #2 ($data) must be of type array, App\Models\Test given, called in D:\projects\test\vendor\barryvdh\laravel-dompdf\src\Facade\Pdf.php on line 51", "exception": "TypeError", "file": "C:\projects\test\vendor\barryvdh\laravel-dompdf\src\PDF.php", "line": 131,

To Reproduce "laravel/framework": "^8.81" TO "laravel/framework": "^9.0", "barryvdh/laravel-dompdf": "^0.9.0" TO "barryvdh/laravel-dompdf": "^1.0"

Expected behavior PDF should work without any error

Screenshots image

Additional context please update the plugin which suitable for the updated Laravel

egrobler commented 2 years ago

This doesn't sound like a bug to me, but rather how you're using loadView. Would you be willing to post some of the relevant code where you're using loadView so we can see what's what?

abhikm1991 commented 2 years ago

use \PDF;

public function getPDF($id){ $data= Service::find($id); if ($data) { $pdf = PDF::loadView('pdf/detail',$data); $pdf->setPaper('A4', 'landscape'); $fileName = 'export-'. date('m-d-Y-His').'.pdf'; Storage::put('public/uploads/datas/downloads/'.$fileName, $pdf->output()); $link = Storage::disk('datas')->url('downloads/'.$fileName); return $link; }

abhikm1991 commented 2 years ago

@egrobler please look into code snapshots

egrobler commented 2 years ago

@abhikm1991 is $data of type array there?

egrobler commented 2 years ago

Haven’t tested this but try this to see if $data will be turned into an array $data=Service::find($id)->toArray(); Or $data=Service::find($id)->all();

abhikm1991 commented 2 years ago

@egrobler Its also having error

egrobler commented 2 years ago

Even after returning $data as type array? Same error?

abhikm1991 commented 2 years ago

@egrobler yes bro

egrobler commented 2 years ago

@abhikm1991 I am struggling to understand. If $data is of type array, how can the error still say: Argument https://github.com/barryvdh/laravel-dompdf/issues/2 ($data) must be of type array, App\Models\Test given Are you sure it is still the exact same error, and that you have confirmed that if you do something like echo dd(gettype($data)); says it is an "array"?

abhikm1991 commented 2 years ago

@egrobler So Array only works in laravel 9?

egrobler commented 2 years ago

Don't know, but that's what the error is telling me...

abhikm1991 commented 2 years ago

@egrobler You are right. Its having Objects + Some Array data. So in the blade file, I am using that for the display. But this code works well in Laravel 8.*. So in Laravel 9, only the array will be working? We can't use objects directly?

egrobler commented 2 years ago

Probably, but if changing the return value to an array works, then stick with that. I am no expert on Laravel or dompdf, but if the error says it wants an array, give it an array :)

abhikm1991 commented 2 years ago

@egrobler it's a burden for me to change everywhere! @jdavidbakr @zaherg @luke83 Please check with the issue

abhikm1991 commented 2 years ago

@barryvdh Please check with the issue

egrobler commented 2 years ago

Why reopen this issue, as I feel it is clearly not an issue with the library? Even in Laravel 9 the 2nd parameter of the view() function has to be an array.

https://github.com/barryvdh/laravel-dompdf/blob/e49e1747d71f26454d3138d36247e9f8adc724e7/src/PDF.php

Lines 125-135

/**
     * Load a View and convert to HTML
     * @param array<string, mixed> $data
     * @param array<string, mixed> $mergeData
     * @param string|null $encoding Not used yet
     */
    public function loadView(string $view, array $data = [], array $mergeData = [], ?string $encoding = null): self
    {
        $html = $this->view->make($view, $data, $mergeData)->render();
        return $this->loadHTML($html, $encoding);
    }
jdavidbakr commented 2 years ago

Not sure why you pinged me into this conversation - but as far as I know, sending an object as the argument to a view has never been officially supported. It probably worked in the past because it wasn't strongly typed and so the object was converted to an array, but it looks like it's now strongly typed.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Any issues with PDF rendering itself that are not directly related to this package, should be reported on https://github.com/dompdf/dompdf instead. When having doubts, please try to reproduce the issue with just dompdf. If you believe this is an actual issue with the latest version of laravel-dompdf, please reply to this issue so we can investigate further. Thank you for your contribution! Apologies for any delayed response on our side.