Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Defaults not used when running queue:work #107

Open
Propaganistas opened this issue Mar 22, 2024 · 10 comments
Open

[Bug]: Defaults not used when running queue:work #107

Propaganistas opened this issue Mar 22, 2024 · 10 comments
Labels
bug Something isn't working

Comments

@Propaganistas
Copy link

Propaganistas commented Mar 22, 2024

What happened?

I've set defaults for the PdfBuilder in a service provider's boot() method as described in the docs.

When a PDF gets generated in a queued job:

  • the defaults are applied when powering the queue using queue:listen
  • the defaults are NOT applied when powering the queue using queue:work

How to reproduce the bug

  1. Create a fresh Laravel application (I'm on 10.x).
  2. Change queue connection to database and publish the jobs table
  3. Set defaults in AppServiceProvider boot() (e.g. Pdf::default()->margin(0.4, 0.4, 0.4, 0.4, Unit::Inch)
  4. Create a job that generates a PDF ( e.g. Pdf::view('welcome')->save('somewhere'))
  5. Run queue:work
  6. Enqueue the job using Tinker (or otherwise) and check result

Or temporarily add a dump statement in PdfBuilder's view() method to check the margins property and return early.
You'll notice the margins are set when running queue:listen but remain null when running queue:work.

Package Version

1.4.0

PHP Version

8.3.3

Laravel Version

10.48.4

@Propaganistas Propaganistas added the bug Something isn't working label Mar 22, 2024
@freekmurze
Copy link
Member

Is there some way to add a test for this?

@Propaganistas
Copy link
Author

Is there some way to add a test for this?

Not sure. I think you could borrow ideas from framework tests?

https://github.com/laravel/framework/blob/10.x/tests/Queue/QueueWorkerTest.php

@spatie-bot
Copy link

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.

@Propaganistas
Copy link
Author

Time to reopen as still unsolved.

@freekmurze freekmurze reopened this Jul 24, 2024
@zarulizham
Copy link

Hi, I'm facing the same problem when generating the PDF under queue. If generate directly (sync), no issues. All defaults defined in app/Providers/AppServiceProvider.php boot().

@meyer59
Copy link

meyer59 commented Sep 30, 2024

Hi
Same issue here

@okaufmann
Copy link

I just ran into the same problem. The issue shows the same behavior.

Here is some minimal test code:

use Spatie\LaravelPdf\Facades\Pdf;
use Spatie\Browsershot\Browsershot;

Pdf::default()->withBrowsershot(function (Browsershot $browserShot) {
            logger('prepare browsershot for pdf generation'); // only logs in sync context, never in a async job
});

@okaufmann
Copy link

It appears there is a resolution issue with the Facade or the Pdf::default() call, which returns a new instance of PdfFactory in queued jobs on each call. I am unsure of the exact cause.

As a workaround, I am registering the PdfBuilder manually and resolving it from the container when needed:

class PdfServiceProvider extends ServiceProvider
{
    #[Override]
    public function register(): void
    {
        $this->app->bind(PdfBuilder::class, function () {
            return (new PdfBuilder())->withBrowsershot(function (Browsershot $browsershot) {
                // Apply customizations to Browsershot
                return $browsershot;
            });
        });
    }
}

Then, I resolve the PdfBuilder like this:

$pdfBuilder = resolve(PdfBuilder::class); // Or inject it via constructor or method

$builder = $pdfBuilder->view($contextData->viewName, ['contextData' => $contextData])
            ->format(Format::A4)
            ->portrait()
            ->save($fileName);

@KhairulAzmi21
Copy link

Any solution ?

happen to be laravel pdf working fine without queue , but when using queue .

i got error .

ProtocolError: Protocol error (Page.printToPDF): Printing failed
at

@KhairulAzmi21
Copy link

It appears there is a resolution issue with the Facade or the Pdf::default() call, which returns a new instance of PdfFactory in queued jobs on each call. I am unsure of the exact cause.

As a workaround, I am registering the PdfBuilder manually and resolving it from the container when needed:

class PdfServiceProvider extends ServiceProvider
{
#[Override]
public function register(): void
{
$this->app->bind(PdfBuilder::class, function () {
return (new PdfBuilder())->withBrowsershot(function (Browsershot $browsershot) {
// Apply customizations to Browsershot
return $browsershot;
});
});
}
}
Then, I resolve the PdfBuilder like this:

$pdfBuilder = resolve(PdfBuilder::class); // Or inject it via constructor or method

$builder = $pdfBuilder->view($contextData->viewName, ['contextData' => $contextData])
->format(Format::A4)
->portrait()
->save($fileName);

How you manage to fix it ? I try this but still getting error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants