Skip to content

Document API

Class Document (TcpdfNext\Core\Document) là điểm truy cập chính để tạo PDF document. Nó kết hợp 12 trait, mỗi trait đóng góp một nhóm method liên quan. Mọi method thay đổi trả về static để fluent chaining.

php
use TcpdfNext\Core\Document;

$pdf = Document::create()
    ->setTitle('Invoice')
    ->addPage()
    ->setFont('Helvetica', size: 12)
    ->cell(width: 0, height: 10, text: 'Hello World')
    ->save('/output/invoice.pdf');

HasMetadata

Method đặt metadata cấp document (Title, Author, Subject, Keywords, Creator).

setTitle(string $title): static
Đặt tiêu đề document (xuất hiện trong title bar trình xem PDF và XMP dc:title).
setAuthor(string $author): static
Đặt tác giả document (XMP dc:creator).
setSubject(string $subject): static
Đặt chủ đề document (XMP dc:description).
setKeywords(string|array $keywords): static
Đặt từ khóa document. Nhận chuỗi phân cách dấu phẩy hoặc mảng chuỗi.
setCreator(string $creator): static
Đặt tên ứng dụng tạo (XMP xmp:CreatorTool). Mặc định 'TCPDF-Next'.

HasPages

Method thêm, xóa, di chuyển và cấu hình trang.

addPage(?PageSize $pageSize = null, ?Orientation $orientation = null): static
Thêm trang mới. Tùy chọn ghi đè kích thước và hướng trang mặc định cho trang này.
deletePage(int $pageNumber): static
Xóa trang tại chỉ mục 1-based cho trước. Throw OutOfRangeException nếu trang không tồn tại.
movePage(int $from, int $to): static
Di chuyển trang từ vị trí này sang vị trí khác. Cả hai chỉ mục đều 1-based.
copyPage(int $pageNumber): static
Nhân đôi trang và thêm bản sao sau trang cuối.
setAutoPageBreak(bool $auto, float $margin = 0): static
Bật hoặc tắt ngắt trang tự động. Khi bật, trang mới được thêm khi nội dung đạt margin dưới trừ giá trị margin cho trước.
setMargins(float $left, float $top, float $right = -1, float $bottom = -1): static
Đặt margin trang tính bằng đơn vị người dùng. Nếu right là -1, lấy đối xứng left. Nếu bottom là -1, lấy đối xứng top.
setLeftMargin(float $margin): static
Chỉ đặt margin trái.
setTopMargin(float $margin): static
Chỉ đặt margin trên.
setRightMargin(float $margin): static
Chỉ đặt margin phải.
setCellPadding(float $padding): static
Đặt padding nội bộ cell mặc định tính bằng đơn vị người dùng.
setPageSize(PageSize $size): static
Đặt kích thước trang mặc định cho trang mới.
setOrientation(Orientation $orientation): static
Đặt hướng trang mặc định cho trang mới.
pageCount(): int
Trả về tổng số trang trong document.
currentPage(): int
Trả về số trang hiện tại (1-based).
setPage(int $pageNumber): static
Di chuyển con trỏ đến trang hiện có.

HasTypography

Method chọn font, kích thước, khoảng cách và đo chuỗi.

setFont(string $family, string $style = '', float $size = 0): static
Chọn font theo tên family. Style có thể là '' (regular), 'B' (bold), 'I' (italic), hoặc 'BI'. Size 0 giữ kích thước hiện tại.
setFontSize(float $size): static
Đặt kích thước font tính bằng point.
setFontSpacing(float $spacing): static
Đặt khoảng cách ký tự thêm tính bằng đơn vị người dùng.
setFontStretching(float $percent): static
Đặt tỷ lệ font ngang dạng phần trăm (100 = bình thường).
getStringWidth(string $text, string $family = '', string $style = '', float $size = 0): float
Tính chiều rộng chuỗi tính bằng đơn vị người dùng dùng cài đặt font cho trước (hoặc hiện tại).
getCharWidth(string $char): float
Trả về chiều rộng một ký tự tính bằng đơn vị người dùng.
getNumberOfLines(string $text, float $width): int
Ước lượng text sẽ chiếm bao nhiêu dòng tại chiều rộng cho trước.

HasColors

Method đặt màu vẽ, tô và text với alpha transparency tùy chọn.

setDrawColor(int $r, int $g = -1, int $b = -1): static
Đặt màu stroke (vẽ). Truyền một giá trị cho grayscale hoặc ba giá trị cho RGB.
setFillColor(int $r, int $g = -1, int $b = -1): static
Đặt màu tô cho hình dạng và nền cell.
setTextColor(int $r, int $g = -1, int $b = -1): static
Đặt màu text.
setAlpha(float $alpha, string $blendMode = 'Normal'): static
Đặt alpha transparency (0.0 = trong suốt hoàn toàn, 1.0 = không trong suốt) và blend mode tùy chọn.
setDrawColorObject(Color $color): static
Đặt màu stroke dùng value object Color (hỗ trợ RGB, CMYK, Gray, Spot).
setFillColorObject(Color $color): static
Đặt màu tô dùng value object Color.
setTextColorObject(Color $color): static
Đặt màu text dùng value object Color.

HasTextOutput

Method render nội dung text lên trang.

cell(float $width, float $height = 0, string $text = '', string|int $border = 0, int $ln = 0, Alignment $align = Alignment::Left, bool $fill = false, string $link = ''): static
In cell một dòng. Cell có thể có border, nền tô và hyperlink tùy chọn.
multiCell(float $width, float $height, string $text, string|int $border = 0, Alignment $align = Alignment::Justified, bool $fill = false, int $maxLines = 0): static
In khối text nhiều dòng với tự động xuống dòng.
text(float $x, float $y, string $text): static
Đặt chuỗi text tại tọa độ chính xác. Không xuống dòng hoặc ranh giới cell.
write(float $height, string $text, string $link = ''): static
Viết text liên tục từ vị trí hiện tại. Hỗ trợ hyperlink inline.
writeHtml(string $html, bool $lineBreak = true, bool $fill = false, bool $resetStyles = false, string $align = ''): static
Render chuỗi HTML với hỗ trợ CSS. Hỗ trợ tag HTML phổ biến, bảng, danh sách và inline style.
writeHtmlCell(float $width, float $height, float $x, float $y, string $html, string|int $border = 0, int $ln = 0, bool $fill = false): static
Render HTML bên trong cell có vị trí.
ln(float $height = 0): static
Chèn ngắt dòng. Nếu height là 0, dùng chiều cao cell cuối cùng đã in.

HasDrawing

Method vẽ đường, hình dạng và mũi tên.

line(float $x1, float $y1, float $x2, float $y2): static
Vẽ đường thẳng giữa hai điểm.
rect(float $x, float $y, float $w, float $h, string $style = ''): static
Vẽ hình chữ nhật. Style: '' (stroke), 'F' (fill), 'DF' (stroke + fill).
roundedRect(float $x, float $y, float $w, float $h, float $r, string $roundCorner = '1111', string $style = ''): static
Vẽ hình chữ nhật bo góc. Bitmask roundCorner kiểm soát góc nào được bo.
circle(float $x, float $y, float $r, string $style = '', array $angles = [0, 360]): static
Vẽ hình tròn (hoặc cung) với tâm (x, y) và bán kính r.
ellipse(float $x, float $y, float $rx, float $ry, float $angle = 0, string $style = ''): static
Vẽ hình elip với tâm (x, y), bán kính ngang rx và bán kính dọc ry.
polygon(array $points, string $style = ''): static
Vẽ đa giác từ mảng cặp tọa độ [x, y].
arrow(float $x1, float $y1, float $x2, float $y2, int $headStyle = 0, float $armSize = 5, int $armAngle = 15): static
Vẽ đường có đầu mũi tên.
curve(float $x0, float $y0, float $x1, float $y1, float $x2, float $y2, float $x3, float $y3, string $style = ''): static
Vẽ đường cong Bezier bậc ba.
setLineWidth(float $width): static
Đặt độ rộng đường cho thao tác stroke.
setLineCap(int $style): static
Đặt style đầu đường: 0 (butt), 1 (round), 2 (square).
setLineJoin(int $style): static
Đặt style nối đường: 0 (miter), 1 (round), 2 (bevel).

HasTransforms

Method biến đổi hệ tọa độ (lưu dạng stack).

startTransform(): static
Lưu trạng thái đồ họa hiện tại. Phải ghép cặp với stopTransform().
stopTransform(): static
Khôi phục trạng thái đồ họa đã lưu trước đó.
rotate(float $angle, float $x = null, float $y = null): static
Xoay các thao tác vẽ tiếp theo theo góc cho trước (độ) quanh điểm (x, y).
scale(float $sx, float $sy, float $x = null, float $y = null): static
Co giãn các thao tác vẽ tiếp theo. 100 = không thay đổi, 200 = gấp đôi.
translate(float $tx, float $ty): static
Tịnh tiến (dịch chuyển) các thao tác vẽ tiếp theo theo (tx, ty) đơn vị người dùng.
skew(float $angleX, float $angleY, float $x = null, float $y = null): static
Nghiêng các thao tác vẽ tiếp theo theo góc cho trước (độ).
mirror(string $axis, float $x = null, float $y = null): static
Lật gương các thao tác vẽ tiếp theo theo trục cho trước ('H' ngang, 'V' dọc).

HasLayout

Method cho header, footer, cột và chế độ booklet.

setHeaderCallback(callable $callback): static
Đăng ký callback gọi ở đầu mỗi trang. Nhận instance Document.
setFooterCallback(callable $callback): static
Đăng ký callback gọi ở cuối mỗi trang. Nhận instance Document.
setColumnsArray(array $columns): static
Định nghĩa layout nhiều cột. Mỗi entry là mảng kết hợp với key 'width' và 'margin' tùy chọn.
selectColumn(int $column = 0): static
Di chuyển con trỏ đến cột chỉ định (0-based).
setBooklet(bool $enable, float $innerMargin = -1, float $outerMargin = -1): static
Bật chế độ booklet (saddle-stitch) với margin trong và ngoài tùy chỉnh.
setPrintHeader(bool $print): static
Bật hoặc tắt header trên các trang tiếp theo.
setPrintFooter(bool $print): static
Bật hoặc tắt footer trên các trang tiếp theo.

HasNavigation

Method cho bookmark, mục lục, link, annotation và file đính kèm.

bookmark(string $title, int $level = 0, float $y = -1, int $page = 0, string $style = '', Color $color = null): static
Thêm bookmark (outline entry). Level 0 là cấp cao nhất; giá trị cao hơn tạo con lồng nhau.
addTOC(int $page = 0, string $numberFormat = '', string $filler = '.'): static
Tạo mục lục từ mọi bookmark và chèn tại trang cho trước.
addLink(): int
Tạo đích link nội bộ và trả về định danh.
setLink(int $linkId, float $y = 0, int $page = -1): static
Đặt đích cho link nội bộ đã tạo trước đó.
annotation(float $x, float $y, float $w, float $h, string $text, array $options = []): static
Thêm annotation text tại hình chữ nhật cho trước.
addFileAttachment(string $filePath, string $description = '', string $name = ''): static
Đính kèm file vào document. Hỗ trợ associated file PDF/A-3.

HasInteractive

Method cho trường AcroForm và JavaScript.

textField(string $name, float $x, float $y, float $w, float $h, array $options = []): static
Thêm trường text input tại vị trí cho trước.
checkboxField(string $name, float $x, float $y, float $size = 10, bool $checked = false): static
Thêm trường checkbox.
radioField(string $name, float $x, float $y, float $size = 10, array $options = []): static
Thêm trường radio button thuộc nhóm có tên.
listboxField(string $name, float $x, float $y, float $w, float $h, array $items, array $options = []): static
Thêm trường listbox chọn.
comboboxField(string $name, float $x, float $y, float $w, float $h, array $items, array $options = []): static
Thêm trường dropdown combobox.
buttonField(string $name, float $x, float $y, float $w, float $h, string $caption, string $action = ''): static
Thêm trường push button với JavaScript action tùy chọn.
includeJS(string $script): static
Nhúng JavaScript action cấp document. Được thực thi khi document mở.

HasSecurity

Method cho mã hóa và chữ ký số.

setProtection(array $permissions = [], string $userPassword = '', string $ownerPassword = '', int $mode = 3): static
Mã hóa document bằng AES-256 (mode 3). Permission: 'print', 'copy', 'modify', 'annot-forms', v.v.
setSignature(string $certFile, string $privateKeyFile, string $password = '', string $extracertsFile = '', int $certType = 2, array $info = []): static
Áp dụng chữ ký số PAdES. Dùng certType 2 cho file PKCS#12 (.p12/.pfx).

HasOutput

Method render PDF cuối cùng.

save(string $path): bool
Ghi PDF ra file. Trả về true khi thành công.
output(OutputDestination $destination = OutputDestination::String, string $filename = 'doc.pdf', string $path = ''): string|bool
Render PDF đến đích chọn: String (trả về nhị phân), File, Inline (trình duyệt), hoặc Download.
toString(): string
Alias tiện lợi cho output(OutputDestination::String). Trả về nhị phân PDF thô.

Xem thêm

Phân phối theo giấy phép LGPL-3.0-or-later.