跳轉到

方法對應表

本頁列出 TCPDF 公開方法與 NextPDF Core 對應 API 的完整對應關係,協助開發者在完成相容層驗證後,逐步將程式碼遷移至 NextPDF 原生 API。

文件與頁面管理

TCPDF 方法 NextPDF Core API 說明
new TCPDF($orientation, $unit, $format, ...) Document::createStandalone($config) 建立文件
AddPage($orientation, $format) $document->addPage() 新增頁面
SetMargins($left, $top, $right) Configuration::create(margin: Margin::create(...)) 設定邊距
SetAutoPageBreak($auto, $margin) Configuration::create(autoPageBreak: ...) 自動換頁
SetHeaderMargin($margin) Configuration::create(headerMargin: ...) 頁首邊距
SetFooterMargin($margin) Configuration::create(footerMargin: ...) 頁尾邊距
GetPageWidth() $document->currentPage()->width() 取得頁面寬度
GetPageHeight() $document->currentPage()->height() 取得頁面高度
GetX() $document->cursor()->x() 取得目前 X 座標
GetY() $document->cursor()->y() 取得目前 Y 座標
SetX($x) $document->moveTo(x: $x) 設定 X 座標
SetY($y, $resetX) $document->moveTo(y: $y) 設定 Y 座標
SetXY($x, $y) $document->moveTo(x: $x, y: $y) 設定座標
Ln($height) $document->newLine(height: $height) 換行

字型設定

TCPDF 方法 NextPDF Core API 說明
AddFont($family, $style, $fontfile) $document->fonts()->load($path) 載入自訂字型
SetFont($family, $style, $size) $document->setFont(FontReference::create(...)) 設定當前字型
GetFontFamily() $document->currentFont()->family() 取得字型家族名稱
GetFontSize() $document->currentFont()->size() 取得字型大小
SetFontSize($size) $document->currentFont()->withSize($size) 設定字型大小
SetDefaultMonospacedFont($font) Configuration::create(monospacedFont: ...) 設定預設等寬字型

文字輸出

TCPDF 方法 NextPDF Core API 說明
Cell($w, $h, $txt, $border, $ln, $align, ...) $document->cell(...) 單行儲存格
MultiCell($w, $h, $txt, $border, $align, ...) $document->multiCell(...) 多行儲存格
Text($x, $y, $txt) $document->text($txt, x: $x, y: $y) 絕對位置文字
Write($h, $txt, $link) $document->write($txt) 行內文字
WriteHTML($html, ...) $document->renderHtml($html) HTML 渲染
WriteHTMLCell($w, $h, $x, $y, $html, ...) $document->renderHtmlAt($html, x: $x, y: $y) 定位 HTML

影像

TCPDF 方法 NextPDF Core API 說明
Image($file, $x, $y, $w, $h, $type, ...) $document->image($path, x: $x, y: $y, width: $w, height: $h) 嵌入影像
ImageSVG($file, $x, $y, $w, $h) $document->svg($path, x: $x, y: $y, width: $w) 嵌入 SVG

繪圖

TCPDF 方法 NextPDF Core API 說明
SetDrawColor($r, $g, $b) $document->graphics()->setStrokeColor(Color::rgb($r, $g, $b)) 設定繪圖顏色
SetFillColor($r, $g, $b) $document->graphics()->setFillColor(Color::rgb($r, $g, $b)) 設定填充顏色
SetTextColor($r, $g, $b) $document->graphics()->setTextColor(Color::rgb($r, $g, $b)) 設定文字顏色
SetLineWidth($width) $document->graphics()->setLineWidth($width) 設定線條寬度
Line($x1, $y1, $x2, $y2) $document->graphics()->line(from: Point::create($x1, $y1), to: Point::create($x2, $y2)) 繪製線段
Rect($x, $y, $w, $h, $style) $document->graphics()->rect(x: $x, y: $y, width: $w, height: $h) 繪製矩形
Ellipse($x0, $y0, $rx, $ry, ...) $document->graphics()->ellipse(cx: $x0, cy: $y0, rx: $rx, ry: $ry) 繪製橢圓

文件中繼資料

TCPDF 方法 NextPDF Core API 說明
SetTitle($title) Configuration::create(title: $title) PDF 標題
SetAuthor($author) Configuration::create(author: $author) 作者
SetSubject($subject) Configuration::create(subject: $subject) 主題
SetKeywords($keywords) Configuration::create(keywords: explode(',', $keywords)) 關鍵字
SetCreator($creator) Configuration::create(creator: $creator) 建立工具

加密

TCPDF 方法 NextPDF Core API 說明
SetProtection($permissions, $user_pass, $owner_pass, $mode) EncryptionConfig::aes256(...) 加密設定

注意:TCPDF 的 $mode 0-3 對應 RC4-40、RC4-128、AES-128、AES-256。NextPDF 強制使用 AES-256;若原始程式碼使用弱加密演算法,遷移後將自動升級至 AES-256。

條碼

TCPDF 方法 NextPDF Core API 說明
write1DBarcode($code, $type, $x, $y, $w, $h) $document->barcode()->render1D(code: $code, symbology: BarcodeSymbology::from($type)) 1D 條碼
write2DBarcode($code, $type, $x, $y, $w, $h) $document->barcode()->render2D(code: $code, symbology: BarcodeSymbology::from($type)) 2D 條碼

書籤與連結

TCPDF 方法 NextPDF Core API 說明
Bookmark($txt, $level, $y) $document->navigation()->addBookmark($txt, level: $level) 新增書籤
AddLink() $document->navigation()->createLinkTarget() 建立連結目標
SetLink($link, $y, $page) $document->navigation()->setLinkDestination(...) 設定連結目標
Link($x, $y, $w, $h, $link) $document->navigation()->addLinkAnnotation(...) 新增連結標註

輸出

TCPDF 方法 NextPDF Core API 說明
Output($name, 'D') $document->output() + HTTP headers 強制下載
Output($name, 'I') $document->output() + inline headers 內嵌顯示
Output($path, 'F') $document->save($path) 存至檔案
Output('', 'S') $document->output() 回傳 bytes 字串

參見