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
| Parameter | Type | Description |
|---|---|---|
callback | Closure(Document): void | Builder that populates the PDF |
outputPath | string | Destination path (absolute, or relative to disk root) |
disk | ?string | Laravel filesystem disk (default: null for local file) |
onSuccess | ?Closure(string): void | Called with the output path on success |
onFailure | ?Closure(Throwable): void | Called 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
- Pdf Facade — Create documents used by the job callback
- HTTP Responses — Serve generated PDFs to users
- Configuration — Queue connection and retry defaults