큐 작업
GeneratePdfJob 클래스는 PDF 생성을 백그라운드 워커에 오프로드하기 위한 즉시 사용 가능한 큐 작업을 제공합니다. 빌더 콜백을 받아 결과를 지정된 경로에 쓰며, 성공/실패 훅, 재시도 로직, 배치 디스패치를 지원합니다.
php
use Yeeefang\TcpdfNext\Laravel\Jobs\GeneratePdfJob;기본 디스패치
새 Document를 받는 콜백과 출력 경로를 전달합니다:
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');작업은 컨테이너에서 Document를 해석하고, 콜백에 전달한 다음, 출력을 씁니다.
생성자 매개변수
| 매개변수 | 유형 | 설명 |
|---|---|---|
callback | Closure(Document): void | PDF를 채우는 빌더 |
outputPath | string | 대상 경로 (절대 경로 또는 디스크 루트 기준 상대 경로) |
disk | ?string | Laravel 파일시스템 디스크 (기본값: 로컬 파일의 경우 null) |
onSuccess | ?Closure(string): void | 성공 시 출력 경로와 함께 호출됨 |
onFailure | ?Closure(Throwable): void | 실패 시 예외와 함께 호출됨 |
성공 및 실패 콜백
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');재시도 로직
표준 Laravel 재시도 구성이 지원됩니다:
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]);기본 재시도 값은 config/tcpdf-next.php의 queue 키에서 설정할 수 있습니다.
큐 연결 구성
PDF 작업을 전용 연결로 라우팅합니다:
php
GeneratePdfJob::dispatch(
callback: fn (Document $pdf) => $pdf->addPage()->cell(0, 10, 'Hello'),
outputPath: storage_path('output.pdf'),
)
->onConnection('redis')
->onQueue('pdf-generation')
->afterCommit();배치 PDF 생성
Bus::batch()를 사용하여 여러 PDF를 병렬로 생성합니다:
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();