Bảng ánh xạ API
Trang này cung cấp ánh xạ toàn diện mọi method TCPDF thường dùng sang tương đương TCPDF-Next. Dùng đây làm tham chiếu khi migration code hiện có.
Thiết lập Document
| TCPDF cũ | TCPDF-Next | Ghi chú |
|---|---|---|
new TCPDF($orientation, $unit, $format) | PdfDocument::create()->setOrientation(...)->setPageFormat(...)->build() | Named param, fluent builder |
SetCreator($creator) | ->setCreator($creator) | |
SetAuthor($author) | ->setAuthor($author) | |
SetTitle($title) | ->setTitle($title) | |
SetSubject($subject) | ->setSubject($subject) | |
SetKeywords($keywords) | ->setKeywords([...]) | Array thay vì string phân cách dấu phẩy |
SetMargins($left, $top, $right) | ->setMargins(new Margins($left, $right, $top, $bottom)) | Value object |
SetCellMargins($l, $t, $r, $b) | TextBlock::create()->setPadding(...) | Padding cho từng element |
SetAutoPageBreak($auto, $margin) | ->setAutoPageBreak($auto, bottomMargin: $margin) | Named parameter |
setLanguageArray($lang) | ->setLocale($locale) | Chuỗi locale chuẩn |
setFontSubsetting($enable) | ->getFontManager()->setSubsetting($enable) | |
SetDefaultMonospacedFont($font) | $renderer->setDefaultMonoFont($font) | |
SetDisplayMode($zoom, $layout) | ->setViewerPreferences(zoom: $zoom, layout: $layout) | |
setImageScale($scale) | Không cần | Kích thước dựa trên DPI |
SetHeaderMargin($margin) | ->setMargins(new Margins(..., header: $margin)) | |
SetFooterMargin($margin) | ->setMargins(new Margins(..., footer: $margin)) | |
setPrintHeader($enable) | Bỏ callback onPageHeader() | |
setPrintFooter($enable) | Bỏ callback onPageFooter() |
Quản lý trang
| TCPDF cũ | TCPDF-Next | Ghi chú |
|---|---|---|
AddPage($orientation, $format) | $pdf->addPage($format, $orientation) | |
endPage() | Tự động | Ngầm định khi addPage() tiếp theo |
getPage() | $pdf->getCurrentPageNumber() | |
setPage($page) | $pdf->getPage($pageNumber) | |
getNumPages() | $pdf->getPageCount() | |
deletePage($page) | $pdf->removePage($pageNumber) | |
movePage($from, $to) | $pdf->movePage($from, $to) | |
copyPage($page) | $pdf->duplicatePage($pageNumber) | |
lastPage() | $pdf->getPage($pdf->getPageCount()) | |
getPageWidth() | $page->getWidth() | |
getPageHeight() | $page->getHeight() | |
SetXY($x, $y) | ->setPosition($x, $y) | Định vị cho từng element |
GetX() / GetY() | $page->getCursorX() / $page->getCursorY() | |
SetX($x) / SetY($y) | $page->setCursor($x, $y) | |
getBreakMargin() | $page->getBottomMargin() |
Thao tác Font
| TCPDF cũ | TCPDF-Next | Ghi chú |
|---|---|---|
SetFont($family, $style, $size) | $page->setFont($family, size: $size, style: FontStyle::BOLD) | Enum cho style |
SetFontSize($size) | $page->setFont(..., size: $size) | |
addTTFfont($fontfile) | $pdf->getFontManager()->registerFont($path, $name) | TTF/OTF chuẩn |
getFontFamilyName() | $page->getCurrentFont()->getFamily() | |
getFontSize() | $page->getCurrentFont()->getSize() | |
getFontSizePt() | $page->getCurrentFont()->getSizeInPoints() | |
isCharDefined($char) | $font->hasGlyph($char) | |
GetStringWidth($string) | $font->measureText($string, $size)->getWidth() | |
GetCharWidth($char) | $font->getGlyphWidth($char, $size) | |
getNumLines($text, $width) | $font->calculateLines($text, $width, $size) |
Xuất văn bản
| TCPDF cũ | TCPDF-Next | Ghi chú |
|---|---|---|
Cell($w, $h, $txt, $border, $ln, $align) | $page->addText($txt)->setSize($w, $h)->setAlignment(...) | Fluent API |
MultiCell($w, $h, $txt, $border, $align) | $page->addParagraph($txt)->setWidth($w)->setAlignment(...) | |
Write($h, $txt, $link) | $page->addText($txt)->setLineHeight($h)->setLink($link) | |
writeHTML($html) | $renderer->writeHtml($html) | Parser dựa trên DOM |
writeHTMLCell($w, $h, $x, $y, $html) | $renderer->writeHtml($html, position: [$x, $y], width: $w) | |
Text($x, $y, $txt) | $page->addText($txt)->setPosition($x, $y) | |
Ln($h) | $page->advanceCursor($h) | |
SetTextColor($r, $g, $b) | ->setColor(Color::rgb($r, $g, $b)) | Value object |
SetTextColorArray($color) | ->setColor(Color::fromArray($color)) |
Hình ảnh
| TCPDF cũ | TCPDF-Next | Ghi chú |
|---|---|---|
Image($file, $x, $y, $w, $h, $type) | $page->addImage($file)->setPosition($x,$y)->setSize($w,$h) | Tự phát hiện định dạng |
ImageEps($file, $x, $y, $w, $h) | $page->addImage($file)->setPosition($x,$y)->setSize($w,$h) | API hợp nhất |
ImageSVG($file, $x, $y, $w, $h) | $page->addImage($file)->setPosition($x,$y)->setSize($w,$h) | API hợp nhất |
setImageBuffer($image, $data) | $page->addImage($binaryData, format: $fmt) | |
setJPEGQuality($quality) | ImagePolicy::create()->setJpegQuality($quality) |
Vẽ và đồ họa
| TCPDF cũ | TCPDF-Next | Ghi chú |
|---|---|---|
Line($x1, $y1, $x2, $y2) | $canvas->drawLine($x1, $y1, $x2, $y2)->stroke() | |
Rect($x, $y, $w, $h, $style) | $canvas->drawRect($x, $y, $w, $h)->fill() hoặc ->stroke() | |
RoundedRect($x, $y, $w, $h, $r) | $canvas->drawRoundedRect($x, $y, $w, $h, $r) | |
Circle($x, $y, $r) | $canvas->drawCircle($x, $y, $r) | |
Ellipse($x, $y, $rx, $ry) | $canvas->drawEllipse($x, $y, $rx, $ry) | |
Curve($x0,$y0,$x1,$y1,$x2,$y2,$x3,$y3) | $canvas->moveTo($x0,$y0)->curveTo($x1,$y1,$x2,$y2,$x3,$y3) | |
Polygon($points) | $canvas->drawPolygon($points) | |
RegularPolygon($x, $y, $r, $sides) | $canvas->drawRegularPolygon($x, $y, $r, $sides) | |
Arrow($x0, $y0, $x1, $y1) | $canvas->drawArrow($x0, $y0, $x1, $y1) | |
SetDrawColor($r, $g, $b) | $canvas->setStrokeColor(Color::rgb($r, $g, $b)) | |
SetFillColor($r, $g, $b) | $canvas->setFillColor(Color::rgb($r, $g, $b)) | |
SetDrawColorArray($color) | $canvas->setStrokeColor(Color::fromArray($color)) | |
SetFillColorArray($color) | $canvas->setFillColor(Color::fromArray($color)) | |
SetLineWidth($width) | $canvas->setLineWidth($width) | |
SetLineStyle($style) | $canvas->setLineStyle(LineStyle::from($style)) | |
SetAlpha($alpha, $blend) | $canvas->setOpacity($alpha)->setBlendMode($blend) |
Biến đổi
| TCPDF cũ | TCPDF-Next | Ghi chú |
|---|---|---|
StartTransform() | $canvas->saveState() | |
StopTransform() | $canvas->restoreState() | |
Translate($tx, $ty) | $canvas->translate($tx, $ty) | |
Rotate($angle, $x, $y) | $canvas->rotate($angle, $x, $y) | |
Scale($sx, $sy, $x, $y) | $canvas->scale($sx, $sy, $x, $y) | |
MirrorH($x) | $canvas->scale(-1, 1, $x, 0) | |
MirrorV($y) | $canvas->scale(1, -1, 0, $y) | |
Skew($xAngle, $yAngle) | $canvas->skew($xAngle, $yAngle) |
Link và Bookmark
| TCPDF cũ | TCPDF-Next | Ghi chú |
|---|---|---|
SetLink($link, $y, $page) | $pdf->createInternalLink($page, $y) | |
AddLink() | $pdf->createLink() | |
Link($x, $y, $w, $h, $link) | $page->addLink($url)->setRect($x, $y, $w, $h) | |
Annotation($x, $y, $w, $h, $text) | $page->addAnnotation($text)->setRect($x, $y, $w, $h) | |
Bookmark($txt, $level) | $pdf->addBookmark($txt, level: $level) |
Barcode
| TCPDF cũ | TCPDF-Next | Ghi chú |
|---|---|---|
write1DBarcode($code, $type, ...) | $page->addBarcode(BarcodeFactory::$type($code))->setPosition(...) | Factory type-safe |
write2DBarcode($code, 'QRCODE', ...) | $page->addBarcode(BarcodeFactory::qrCode($code))->setPosition(...) | |
write2DBarcode($code, 'DATAMATRIX', ...) | $page->addBarcode(BarcodeFactory::dataMatrix($code))->setPosition(...) | |
write2DBarcode($code, 'PDF417', ...) | $page->addBarcode(BarcodeFactory::pdf417($code))->setPosition(...) |
Mã hóa và bảo mật
| TCPDF cũ | TCPDF-Next | Ghi chú |
|---|---|---|
SetProtection($perms, $uPass, $oPass, $mode) | $pdf->setEncryption()->setPermissions(...)->apply() | Chỉ AES-256 |
setSignature($cert, $key, $pass, $extra) | $signer = new PdfSigner($pdf); $signer->setCertificate(...) | Hỗ trợ PAdES đầy đủ |
setSignatureAppearance($x, $y, $w, $h) | $signer->setSignatureField(new SignatureField(...)) |
TIP
setSignature() của TCPDF chỉ tạo chữ ký PKCS#7 cơ bản. TCPDF-Next hỗ trợ PAdES đầy đủ từ B-B đến B-LTA với tích hợp timestamp, nhúng OCSP, và Document Security Store. Xem PAdES B-LTA.
Output
| TCPDF cũ | TCPDF-Next | Ghi chú |
|---|---|---|
Output($name, 'F') | $pdf->save($path) | Lưu vào file |
Output($name, 'D') | Framework response với Content-Disposition: attachment | |
Output($name, 'I') | Framework response với Content-Disposition: inline | |
Output($name, 'S') | $pdf->toString() | Trả về binary string |
Output($name, 'FI') | $pdf->save($path) + inline response | |
Output($name, 'FD') | $pdf->save($path) + download response | |
Output($name, 'E') | base64_encode($pdf->toString()) |
PDF/A và Metadata
| TCPDF cũ | TCPDF-Next | Ghi chú |
|---|---|---|
setPDFVersion($version) | Luôn PDF 2.0 | Không cấu hình được |
| Không có | ->setPdfALevel(PdfALevel::PDF_A_4) | Tính năng mới |
| Không có | ->setPdfALevel(PdfALevel::PDF_A_4F) | Tính năng mới |
setExtraXMP($xmp) | $pdf->getMetadata()->setXmpProperty(...) |
Method không có tương đương trực tiếp
Các method TCPDF cũ này được xử lý khác trong TCPDF-Next:
| TCPDF cũ | Ghi chú |
|---|---|
setRTL($enable) | Tự động qua thuật toán Unicode BiDi (UAX #9) |
setTempPath($path) | PdfDocument::create()->setTempDir(...) |
setCellHeightRatio($ratio) | Dùng chiều cao dòng rõ ràng trong text block |
setListIndentWidth($width) | CSS padding-left trên list |
setOpenCell($isopen) | Không áp dụng (mô hình cell khác) |
getHTMLUnitToUnits() | Không áp dụng (chuyển đổi đơn vị tự động) |
pixelsToUnits($px) | Units::pixelsToMm($px, $dpi) |
unhtmlentities($text) | htmlspecialchars_decode() (PHP built-in) |
Đọc thêm
- Migration từ TCPDF — Hướng dẫn migration từng bước
- Tham chiếu API — Tài liệu API TCPDF-Next đầy đủ
- FAQ — Câu hỏi migration thường gặp