Skip to content

출력 (HasOutput)

파일로 저장

PDF를 생성하는 가장 간단한 방법은 디스크에 직접 쓰는 것입니다:

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->addPage()
    ->setFont('Helvetica', '', 12)
    ->cell(0, 10, 'Hello, World!')
    ->save('/path/to/output.pdf');

save(string $path)는 렌더링된 PDF를 주어진 파일 경로에 쓰고 static을 반환합니다.

대상 제어를 통한 출력

전달을 더 세밀하게 제어하려면 OutputDestination 열거형과 함께 output()을 사용합니다:

php
use Yeeefang\TcpdfNext\Core\Document;
use Yeeefang\TcpdfNext\Contracts\OutputDestination;

$pdf = Document::create()
    ->addPage()
    ->setFont('Helvetica', '', 12)
    ->cell(0, 10, 'Hello, World!');

// 브라우저에 표시 (인라인)
$pdf->output('document.pdf', OutputDestination::Inline);

// 강제 다운로드
$pdf->output('document.pdf', OutputDestination::Download);

// 파일로 저장
$pdf->output('document.pdf', OutputDestination::File);

// 문자열로 반환
$pdf->output('document.pdf', OutputDestination::String);

OutputDestination 열거형

동작HTTP 헤더
Inline브라우저에 표시Content-Type: application/pdf, Content-Disposition: inline
Download다운로드 대화 상자 강제Content-Type: application/pdf, Content-Disposition: attachment
File서버 파일 시스템에 저장없음
String원시 PDF 바이트 반환없음

Inline 또는 Download를 사용할 때, TCPDF-Next는 PDF 콘텐츠를 플러시하기 전에 자동으로 적절한 Content-TypeContent-Disposition 헤더를 전송합니다.

PDF를 문자열로 가져오기

toString()를 사용하여 디스크에 쓰거나 헤더를 보내지 않고 원시 PDF 바이트를 가져옵니다. 이는 이메일 첨부, 스토리지 API 또는 추가 처리에 유용합니다:

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->addPage()
    ->setFont('Helvetica', '', 12)
    ->cell(0, 10, 'Monthly Report');

$content = $pdf->toString();

// 수동으로 디스크에 쓰기
file_put_contents('/path/to/output.pdf', $content);

// 또는 이메일 첨부에 사용
Mail::send([], [], function ($message) use ($content) {
    $message->attachData($content, 'report.pdf', ['mime' => 'application/pdf']);
});

대용량 PDF 스트리밍 출력

수천 페이지의 문서의 경우, 전체 PDF를 메모리에 보유하는 것은 비용이 많이 들 수 있습니다. PdfWriterChunked는 PDF 콘텐츠를 스트림에 점진적으로 씁니다:

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create();
$pdf->addPage()->setFont('Helvetica', '', 10);

for ($i = 0; $i < 5000; $i++) {
    $pdf->cell(0, 5, "Row {$i}: data content here", newLine: true);
}

// 스트리밍은 내부적으로 수행됨 — save가 청크 쓰기를 트리거
$pdf->save('large-report.pdf');

청크 라이터는 완료된 페이지 데이터를 플러시하여, 쓰기 전에 전체 문서를 버퍼링할 필요가 없습니다. 이는 호출자에게 투명합니다 -- 같은 save()output() API가 적용됩니다.

선형화 (빠른 웹 보기)

선형화는 전체 파일이 다운로드되기 전에 첫 번째 페이지를 렌더링할 수 있도록 PDF 객체를 재정렬합니다. 이는 웹으로 전달되는 PDF에 필수적입니다:

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->setLinearization(true)
    ->addPage()
    ->setFont('Helvetica', '', 12)
    ->cell(0, 10, 'Optimized for web viewing')
    ->save('linearized.pdf');

Linearizer는 PDF 뷰어에게 문서를 점진적으로 렌더링하는 방법을 알려주는 힌트 스트림을 작성합니다. PDF가 HTTP를 통해 제공되고 브라우저에서 볼 때 이를 활성화하십시오.

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