跳轉到

Document API

PHP Compatibility

This example uses PHP 8.5 syntax. If your environment runs PHP 8.1 or 7.4, use NextPDF Backport for a backward-compatible build.

Document

namespace NextPDF\Core;

final class Document

靜態工廠方法

/**
 * 建立獨立模式 Document(適用 CLI / 一次性使用)。
 *
 * @param PageSize $pageSize  預設頁面大小
 * @param Margin   $margin    預設頁面邊距
 * @param Config   $config    文件設定(可選)
 */
public static function createStandalone(
    PageSize $pageSize = PageSize::A4,
    Margin   $margin   = Margin::DEFAULT,
    Config   $config   = new Config(),
): self

實例方法

/** 頁面管理器 */
public function pages(): PageManager

/** 文字渲染器 */
public function text(): TextRenderer

/** 繪圖引擎 */
public function drawing(): DrawingEngine

/** 圖像操作 */
public function images(): ImageRegistry

/** 表單管理器 */
public function forms(): FormFieldManager

/** 條碼渲染器 */
public function barcodes(): BarcodeRenderer

/** 導覽功能(書籤、連結、目錄)*/
public function navigation(): NavigationManager

/** 無障礙功能 */
public function accessibility(): AccessibilityManager

/** 取得渲染上下文(唯讀)*/
public function renderingContext(): RenderingContext

/** 水印工具 */
public function watermarker(): Watermarker

/** 儲存至檔案 */
public function save(string $path): void

/** 取得 PDF 位元組字串 */
public function toString(): string

/**
 * HTTP 串流輸出。
 *
 * @param non-empty-string $filename 檔案名稱(下載提示用)
 * @param bool             $inline   true = 在瀏覽器內顯示,false = 強制下載
 */
public function stream(string $filename, bool $inline = true): void

/** 設定無障礙屬性 */
public function withAccessibility(
    Language $language,
    string   $title  = '',
    bool     $tagged = true,
): self

DocumentFactory

namespace NextPDF\Core;

final class DocumentFactory

Worker 模式的文件工廠。FontRegistry 與 ImageRegistry 在程序級別共享,每個請求僅建立輕量的 Document 實例。

/**
 * 建立 DocumentFactory(程序啟動時呼叫一次)。
 *
 * @param FontRegistry      $fontRegistry  字型 Registry(共享)
 * @param ImageRegistry     $imageRegistry 圖像 Registry(共享)
 * @param SpectrumClient|null $accelerator Spectrum 加速引擎(可選)
 */
public static function create(
    FontRegistry     $fontRegistry,
    ImageRegistry    $imageRegistry,
    ?SpectrumClient  $accelerator = null,
): self

/**
 * 建立新的 Document 實例(每個請求呼叫)。
 *
 * @param PageSize $pageSize 預設頁面大小
 * @param Margin   $margin   預設頁面邊距
 * @param Config   $config   文件設定(可選)
 */
public function make(
    PageSize $pageSize = PageSize::A4,
    Margin   $margin   = Margin::DEFAULT,
    Config   $config   = new Config(),
): Document

RenderingContext

namespace NextPDF\Core;

final class RenderingContext
// PHP 8.5 public private(set) 屬性
public private(set) int    $currentPageNumber = 0;
public private(set) string $currentLanguage   = 'und';
public private(set) float  $currentFontSize   = 12.0;
public private(set) string $currentFontName   = 'Helvetica';

// 唯讀訪問器
public function typography(): TypographyContext
public function fontRegistry(): FontRegistry
public function imageRegistry(): ImageRegistry
public function pages(): PageCollection

Config

namespace NextPDF\Core;

final class Config
public function __construct(
    public readonly string  $producer       = 'NextPDF — NextGen PDF 2.0 Engine for Modern PHP',
    public readonly string  $creator        = '',
    public readonly ?string $author         = null,
    public readonly ?string $subject        = null,
    public readonly ?string $keywords       = null,
    public readonly bool    $embedFonts     = true,
    public readonly bool    $compressStream = true,
    public readonly int     $compressionLevel = 6,
)

/** 從環境變數建立 Config */
public static function fromEnvironment(): self

/** 流式建立 Config */
public function withProducer(string $producer): self
public function withCreator(string $creator): self
public function withAuthor(string $author): self
public function withEmbedFonts(bool $embed): self
public function withCompressionLevel(int $level): self

延伸閱讀