API 映射表
本页面提供 TCPDF 方法调用到 TCPDF-Next 流畅 API 映射的完整对照。迁移现有代码时,可将此页面作为参考。
文档配置
| TCPDF 方法 | TCPDF-Next 对应 |
|---|---|
new TCPDF($orientation, $unit, $format) | PdfDocument::create()->setOrientation(...)->setPageFormat(...) |
SetCreator($creator) | ->setCreator($creator) |
SetAuthor($author) | ->setAuthor($author) |
SetTitle($title) | ->setTitle($title) |
SetSubject($subject) | ->setSubject($subject) |
SetKeywords($keywords) | ->setKeywords([...]) |
SetMargins($left, $top, $right) | ->setMargins(new Margins($left, $right, $top, $bottom)) |
SetCellMargins($left, $top, $right, $bottom) | TextBlock::create()->setPadding(...) |
SetAutoPageBreak($auto, $margin) | ->setAutoPageBreak($auto, bottomMargin: $margin) |
setLanguageArray($lang) | ->setLocale($locale) |
setFontSubsetting($enable) | ->getFontManager()->setSubsetting($enable) |
SetDefaultMonospacedFont($font) | $renderer->setDefaultMonoFont($font) |
SetDisplayMode($zoom, $layout) | ->setViewerPreferences(zoom: $zoom, layout: $layout) |
setImageScale($scale) | 不需要(使用 DPI 为基准的尺寸) |
setHeaderFont($font) | 通过页眉回调的字体配置 |
setFooterFont($font) | 通过页脚回调的字体配置 |
SetHeaderMargin($margin) | ->setMargins(new Margins(..., header: $margin)) |
SetFooterMargin($margin) | ->setMargins(new Margins(..., footer: $margin)) |
setPrintHeader($enable) | 省略 onPageHeader() 回调 |
setPrintFooter($enable) | 省略 onPageFooter() 回调 |
页面管理
| TCPDF 方法 | TCPDF-Next 对应 |
|---|---|
AddPage($orientation, $format) | $pdf->addPage($format, $orientation) |
endPage() | 新增下一页时自动结束 |
getPage() | $pdf->getCurrentPageNumber() |
setPage($page) | $pdf->getPage($pageNumber) |
getNumPages() | $pdf->getPageCount() |
deletePage($page) | $pdf->removePage($pageNumber) |
movePage($fromPage, $toPage) | $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) |
GetX() / GetY() | $page->getCursorX() / $page->getCursorY() |
SetX($x) / SetY($y) | $page->setCursor($x, $y) |
getBreakMargin() | $page->getBottomMargin() |
字体
| TCPDF 方法 | TCPDF-Next 对应 |
|---|---|
SetFont($family, $style, $size) | $page->setFont($family, size: $size, style: FontStyle::BOLD) |
SetFontSize($size) | $page->setFont(..., size: $size) |
addTTFfont($fontfile) | $pdf->getFontManager()->registerFont($path, $name) |
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) |
文字输出
| TCPDF 方法 | TCPDF-Next 对应 |
|---|---|
Cell($w, $h, $txt, $border, $ln, $align) | $page->addText($txt)->setSize($w, $h)->setAlignment(...) |
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) |
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)) |
SetTextColorArray($color) | ->setColor(Color::fromArray($color)) |
图片
| TCPDF 方法 | TCPDF-Next 对应 |
|---|---|
Image($file, $x, $y, $w, $h, $type) | $page->addImage($file)->setPosition($x,$y)->setSize($w,$h) |
ImageEps($file, $x, $y, $w, $h) | $page->addImage($file)->setPosition($x,$y)->setSize($w,$h) |
ImageSVG($file, $x, $y, $w, $h) | $page->addImage($file)->setPosition($x,$y)->setSize($w,$h) |
setImageBuffer($image, $data) | $page->addImage($binaryData, format: $fmt) |
getImageBuffer($image) | 不适用(流式架构) |
setJPEGQuality($quality) | ImagePolicy::create()->setJpegQuality($quality) |
绘图与图形
| TCPDF 方法 | TCPDF-Next 对应 |
|---|---|
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() 或 ->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)) |
SetLineWidth($width) | $canvas->setLineWidth($width) |
SetLineStyle($style) | $canvas->setLineStyle(LineStyle::from($style)) |
SetAlpha($alpha, $blend) | $canvas->setOpacity($alpha)->setBlendMode($blend) |
变换
| TCPDF 方法 | TCPDF-Next 对应 |
|---|---|
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) |
链接与书签
| TCPDF 方法 | TCPDF-Next 对应 |
|---|---|
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) |
条码
| TCPDF 方法 | TCPDF-Next 对应 |
|---|---|
write1DBarcode($code, $type, ...) | $page->addBarcode(BarcodeFactory::$type($code))->setPosition(...) |
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(...) |
加密与安全
| TCPDF 方法 | TCPDF-Next 对应 |
|---|---|
SetProtection($perms, $user_pass, $owner_pass, $mode) | $pdf->setEncryption()->setPermissions(...)->setUserPassword(...)->apply() |
setSignature($cert, $key, $pass, $extracerts) | $signer = new PdfSigner($pdf); $signer->setCertificate(...) |
setSignatureAppearance($x, $y, $w, $h) | $signer->setSignatureField(new SignatureField(...)) |
TIP
TCPDF 的 setSignature() 仅创建基础 PKCS#7 签名。TCPDF-Next 支持完整的 PAdES B-B 至 B-LTA,含时间戳集成与 DSS。详见 PAdES B-LTA。
输出
| TCPDF 方法 | TCPDF-Next 对应 |
|---|---|
Output($name, 'F') | $pdf->save($path) |
Output($name, 'D') | 框架响应搭配 Content-Disposition: attachment |
Output($name, 'I') | 框架响应搭配 Content-Disposition: inline |
Output($name, 'S') | $pdf->toString() |
Output($name, 'FI') | $pdf->save($path) + 内嵌响应 |
Output($name, 'FD') | $pdf->save($path) + 下载响应 |
Output($name, 'E') | base64_encode($pdf->toString()) |
PDF/A 与元数据
| TCPDF 方法 | TCPDF-Next 对应 |
|---|---|
setPDFVersion($version) | 固定为 PDF 2.0(不可配置) |
| 不可用 | ->setPdfALevel(PdfALevel::PDF_A_4) |
| 不可用 | ->setPdfALevel(PdfALevel::PDF_A_4F) |
setExtraXMP($xmp) | $pdf->getMetadata()->setXmpProperty(...) |
表格生成
TCPDF 没有原生表格 API,大多数项目使用 HTML 表格或手动 Cell/MultiCell 调用。TCPDF-Next 提供原生 Table API:
php
// TCPDF(迁移前):手动 Cell 组合表格
$pdf->SetFont('helvetica', 'B', 10);
$pdf->Cell(60, 7, 'Item', 1, 0, 'L');
$pdf->Cell(20, 7, 'Qty', 1, 0, 'C');
$pdf->Cell(30, 7, 'Price', 1, 1, 'R');
// TCPDF-Next(迁移后):流畅 Table API
use YeeeFang\TcpdfNext\Text\Table;
use YeeeFang\TcpdfNext\Text\Alignment;
$table = Table::create()
->setPosition(15, 50)
->setWidth(110)
->addColumn('Item', width: 60)
->addColumn('Qty', width: 20, alignment: Alignment::CENTER)
->addColumn('Price', width: 30, alignment: Alignment::RIGHT)
->setHeaderStyle(font: 'Helvetica', size: 10, style: FontStyle::BOLD)
->setBodyStyle(font: 'Helvetica', size: 10)
->setBorder(true);
$table->addRow(['Widget A', '5', '$50.00']);
$page->addTable($table);无直接对应的方法
以下 TCPDF 方法在 TCPDF-Next 中没有直接对应,因为它们涉及 TCPDF 内部状态管理或以不同方式处理的功能:
| TCPDF 方法 | 备注 |
|---|---|
setRTL($enable) | 通过 Unicode BiDi 算法自动处理 |
setTempPath($path) | 通过 PdfDocument::create()->setTempDir(...) 配置 |
setCellHeightRatio($ratio) | 在文字区块中使用明确的行高 |
setListIndentWidth($width) | 在列表上使用 CSS padding-left |
setOpenCell($isopen) | 不适用(不同的单元格模型) |
pixelsToUnits($px) | Units::pixelsToMm($px, $dpi) |
unhtmlentities($text) | 使用 htmlspecialchars_decode()(PHP 内置) |
延伸阅读
- 从 TCPDF 迁移 — 逐步迁移指南
- API 参考 — TCPDF-Next 完整 API 文档
- 常见问题 — 常见迁移疑问