Skip to content

Pdf 파사드

Pdf 파사드는 TcpdfServiceProvider가 등록한 PdfDocumentInterface 바인딩에 대한 편리한 정적 프록시를 제공합니다. config/tcpdf-next.php의 모든 구성 값이 자동으로 적용됩니다.

php
use Yeeefang\TcpdfNext\Laravel\Facades\Pdf;

문서 생성

Pdf::create()는 모든 설정 기본값이 미리 적용된 새 Document 인스턴스를 컨테이너에서 해석합니다:

php
$pdf = Pdf::create();

$pdf->addPage()
    ->setFont('Helvetica', '', 12)
    ->cell(0, 10, 'Hello from Laravel!')
    ->save(storage_path('app/hello.pdf'));

create()를 호출할 때마다 새로운 인스턴스가 반환됩니다. 문서는 호출 간에 공유되지 않습니다.

플루언트 API

모든 Document 메서드는 파사드를 통해 사용 가능하며 체이닝을 위해 static을 반환합니다:

php
$pdf = Pdf::create()
    ->setTitle('Quarterly Report')
    ->setAuthor('Finance Team')
    ->setMargins(left: 15, top: 20, right: 15)
    ->addPage()
    ->setFont('Helvetica', 'B', 18)
    ->cell(0, 12, 'Q1 2026 Report')
    ->ln()
    ->setFont('Helvetica', '', 11)
    ->multiCell(0, 6, $reportBody);

설정 바인딩

파사드는 config/tcpdf-next.php에서 기본값을 읽습니다. 문서별로 모든 설정을 재정의할 수 있습니다:

php
// 설정 기본값 사용 (A4 세로, Helvetica 11pt)
$default = Pdf::create();

// 이 문서에만 페이지 형식 재정의
$receipt = Pdf::create()
    ->setPageFormat('A5')
    ->setMargins(left: 10, top: 10, right: 10);

전체 참조는 설정을 참조하십시오.

파사드 클래스 참조

php
namespace Yeeefang\TcpdfNext\Laravel\Facades;

use Illuminate\Support\Facades\Facade;

/**
 * @method static \Yeeefang\TcpdfNext\Core\Document create()
 * @method static void fake()
 * @method static void assertCreated()
 * @method static void assertCreatedCount(int $count)
 *
 * @see \Yeeefang\TcpdfNext\Core\Document
 */
class Pdf extends Facade
{
    protected static function getFacadeAccessor(): string
    {
        return \Yeeefang\TcpdfNext\Contracts\PdfDocumentInterface::class;
    }
}

Pdf::fake()로 테스트하기

실제 PDF 바이트를 생성하지 않고 어설션을 위해 실제 바인딩을 페이크로 교체합니다:

php
use Yeeefang\TcpdfNext\Laravel\Facades\Pdf;

public function test_invoice_generates_pdf(): void
{
    Pdf::fake();

    $response = $this->get('/invoices/42/pdf');
    $response->assertOk();

    Pdf::assertCreated();
    Pdf::assertCreatedCount(1);
}

페이크는 모든 메서드 호출을 기록하므로 플루언트 체인을 검사할 수 있습니다:

php
Pdf::fake();

$this->get('/invoices/42/pdf');

Pdf::assertCreated(function ($pdf) {
    return $pdf->getTitle() === 'Invoice #42'
        && $pdf->getPageCount() >= 1;
});

다음 단계

  • HTTP 응답 -- 브라우저에 PDF를 안전하게 전달
  • 큐 작업 -- 백그라운드 워커에 생성 오프로드
  • 설정 -- 사용 가능한 모든 설정 키

LGPL-3.0-or-later 라이선스로 배포됩니다.