TCPDF 是一個(gè)用于生成 PDF 的 PHP 類
composer require tecnickcom/tcpdf
tcpdf_pagination.php
,并在其中引入必要的類:<?php
require_once('vendor/autoload.php');
use TCPDF;
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Your Name');
$pdf->SetTitle('Document Title');
$pdf->SetSubject('Document Subject');
$pdf->SetKeywords('TCPDF, PDF, document, pagination');
$pdf->SetFont('helvetica', '', 16, '', true);
$pdf->SetAutoPageBreak(true, PDF_PAGE_MARGIN);
$pdf->AddPage();
function printTextWithPagination($pdf, $text, $page_break = 0.3)
{
$page_count = $pdf->getPageCount();
$line = '';
$y = $pdf->GetY();
$words = explode(' ', $text);
$current_line = '';
foreach ($words as $word) {
if (strlen($current_line) + strlen($word) + 1 > PDF_PAGE_WIDTH) {
$pdf->MultiCell(0, PDF_PAGE_MARGIN, $line);
$y = $pdf->GetY();
$line = $word . ' ';
} else {
if ($line != '') {
$line .= ' ';
}
$line .= $word;
}
}
if ($line != '') {
$pdf->MultiCell(0, PDF_PAGE_MARGIN, $line);
}
if ($y + PDF_FONT_SIZE > PDF_PAGE_HEIGHT) {
$pdf->AddPage();
}
}
printTextWithPagination
函數(shù)在 PDF 中添加文本,并設(shè)置分頁:$text = 'Your long text goes here. It will be divided into pages automatically.';
printTextWithPagination($pdf, $text);
$pdf->Output('tcpdf_pagination.pdf', 'I');
現(xiàn)在,當(dāng)你運(yùn)行 tcpdf_pagination.php
文件時(shí),它將生成一個(gè)包含分頁文本的 PDF 文件。你可以根據(jù)需要調(diào)整 printTextWithPagination
函數(shù)中的 $page_break
參數(shù)來控制分頁的位置。