Skip to content

Queue Job

Class GeneratePdfJob cung cấp queueable job sẵn có để chuyển việc tạo PDF sang background worker. Nó nhận builder callback, ghi kết quả vào đường dẫn cho trước, và hỗ trợ hook thành công/thất bại, logic retry và dispatch batch.

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

Dispatch cơ bản

Truyền callback nhận Document mới và đường dẫn output:

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');

Job resolve Document từ container, truyền cho callback của bạn, rồi ghi output.

Tham số Constructor

Tham sốKiểuMô tả
callbackClosure(Document): voidBuilder để tạo nội dung PDF
outputPathstringĐường dẫn đích (tuyệt đối hoặc tương đối với root disk)
disk?stringLaravel filesystem disk (mặc định: null cho file cục bộ)
onSuccess?Closure(string): voidGọi với đường dẫn output khi thành công
onFailure?Closure(Throwable): voidGọi với exception khi thất bại

Callback thành công và thất bại

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');

Logic Retry

Cấu hình retry chuẩn của Laravel được hỗ trợ:

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]);

Giá trị retry mặc định có thể thiết lập trong config/tcpdf-next.php dưới key queue.

Cấu hình Queue Connection

Chuyển PDF job tới connection riêng:

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

Tạo PDF hàng loạt

Dùng Bus::batch() để tạo nhiều PDF song song:

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();

Bước tiếp theo

  • Pdf Facade — Tạo document dùng bởi job callback
  • HTTP Response — Phục vụ PDF đã tạo cho người dùng
  • Cấu hình — Queue connection và giá trị retry mặc định

Phân phối theo giấy phép LGPL-3.0-or-later.