Skip to content

Queue Jobs

The GeneratePdfJob class provides a ready-made queueable job for offloading PDF generation to background workers. It accepts a builder callback, writes the result to a given path, and supports success/failure hooks, retry logic, and batch dispatching.

php
use Yeeefang\TcpdfNext\Laravel\Jobs\GeneratePdfJob;

Basic Dispatch

Pass a callback that receives a fresh Document and an output path:

php
use Yeeefang\TcpdfNext\Core\Document;

GeneratePdfJob::dispatch(
    callback: function (Document $pdf) {
        $pdf->setTitle('Monthly Report')
            ->addPage()
            ->setFont('Helvetica', '', 12)
            ->cell(0, 10, 'Generated asynchronously');
    },
    outputPath: storage_path('reports/monthly.pdf'),
)->onQueue('pdf-generation');

The job resolves a Document from the container, passes it to your callback, then writes the output.

Constructor Parameters

ParameterTypeDescription
callbackClosure(Document): voidBuilder that populates the PDF
outputPathstringDestination path (absolute, or relative to disk root)
disk?stringLaravel filesystem disk (default: null for local file)
onSuccess?Closure(string): voidCalled with the output path on success
onFailure?Closure(Throwable): voidCalled with the exception on failure

Success and Failure Callbacks

php
GeneratePdfJob::dispatch(
    callback: function (Document $pdf) {
        $pdf->setTitle('Contract')
            ->addPage()
            ->setFont('Helvetica', 'B', 14)
            ->cell(0, 10, 'Service Agreement');
    },
    outputPath: 'contracts/SA-0042.pdf',
    disk: 's3',
    onSuccess: function (string $path) {
        Notification::send($user, new ContractReady($path));
    },
    onFailure: function (Throwable $e) {
        Log::error('Contract PDF failed', ['error' => $e->getMessage()]);
    },
)->onQueue('pdf-generation');

Retry Logic

Standard Laravel retry configuration is supported:

php
GeneratePdfJob::dispatch(
    callback: fn (Document $pdf) => $pdf->addPage()->cell(0, 10, 'Retry demo'),
    outputPath: storage_path('reports/demo.pdf'),
)
->onQueue('pdf-generation')
->tries(3)
->backoff([10, 30, 60]);

Default retry values can be set in config/tcpdf-next.php under the queue key.

Queue Connection Configuration

Route PDF jobs to a dedicated connection:

php
GeneratePdfJob::dispatch(
    callback: fn (Document $pdf) => $pdf->addPage()->cell(0, 10, 'Hello'),
    outputPath: storage_path('output.pdf'),
)
->onConnection('redis')
->onQueue('pdf-generation')
->afterCommit();

Batch PDF Generation

Use Bus::batch() to generate multiple PDFs in parallel:

php
use Illuminate\Support\Facades\Bus;

$jobs = $invoices->map(fn (Invoice $inv) =>
    new GeneratePdfJob(
        callback: function (Document $pdf) use ($inv) {
            $pdf->setTitle("Invoice #{$inv->number}")
                ->addPage()
                ->setFont('Helvetica', 'B', 14)
                ->cell(0, 10, "Invoice #{$inv->number}");
        },
        outputPath: "invoices/{$inv->number}.pdf",
        disk: 'local',
    )
);

Bus::batch($jobs)
    ->name('Monthly Invoice Batch')
    ->onQueue('pdf-generation')
    ->allowFailures()
    ->then(fn () => Notification::send($admin, new BatchComplete()))
    ->catch(fn () => Log::warning('Some invoice PDFs failed'))
    ->dispatch();

Next Steps

Released under the LGPL-3.0-or-later License.