Writer API¶
PdfWriter¶
通常無需直接使用 PdfWriter;透過 Document::save() 或 Document::toString() 即可完成輸出。
/**
* 寫入 Document 並回傳 PDF 二進位字串。
*
* @param Document $document
* @param WriterOptions $options
* @return non-empty-string
*/
public function write(Document $document, WriterOptions $options = new WriterOptions()): string
/**
* 串流寫入至可寫資源(減少記憶體使用,適合大型文件)。
*
* @param resource $stream 可寫的 PHP 資源(如 fopen('/output/file.pdf', 'wb'))
*/
public function writeToStream(Document $document, mixed $stream, WriterOptions $options = new WriterOptions()): void
WriterOptions¶
final readonly class WriterOptions
{
public function __construct(
public readonly bool $compressStreams = true,
public readonly int $compressionLevel = 6, // 0–9(zlib)
public readonly bool $compressObjects = true,
public readonly string $pdfVersion = '2.0',
public readonly bool $linearize = false,
) {}
public static function default(): self
public static function highCompression(): self // compressionLevel = 9
public static function noCompression(): self // 適用調試
}
Linearizer¶
public static function create(): self
/**
* 對文件進行線性化處理。
* 線性化後,PDF 瀏覽器可在下載完成前即開始渲染第一頁。
*
* @param Document $document 要線性化的文件
* @return Document 線性化後的新 Document 實例
*/
public function linearize(Document $document): Document
/**
* 驗證現有 PDF 是否已線性化。
*
* @param non-empty-string $path PDF 檔案路徑
* @return bool
*/
public static function isLinearized(string $path): bool
ViewerPreferences¶
public static function create(): self
/** 頁面佈局(頁面顯示模式)*/
public function withPageLayout(
'SinglePage'|'OneColumn'|'TwoColumnLeft'|'TwoColumnRight'|'TwoPageLeft'|'TwoPageRight' $layout,
): self
/** 開啟文件時的初始縮放模式 */
public function withPageMode(
'UseNone'|'UseOutlines'|'UseThumbs'|'FullScreen'|'UseOC'|'UseAttachments' $mode,
): self
/** 初始顯示比例(1.0 = 100%,null = 適應視窗)*/
public function withInitialZoom(?float $zoom): self
/** 隱藏選單列 */
public function hideMenubar(bool $hide = true): self
/** 隱藏工具列 */
public function hideToolbar(bool $hide = true): self
/** 隱藏視窗 UI 元件 */
public function hideWindowUI(bool $hide = true): self
/** 閱讀方向(影響雙頁模式的左右順序)*/
public function withDirection('L2R'|'R2L' $direction): self
/** 列印時的頁面縮放偏好 */
public function withPrintScaling(
'None'|'AppDefault' $scaling,
): self
/** 套用至指定 Document */
public function applyTo(Document $document): void