基本上,我想在第2页上启动目录(toc)并计算toc的每个页码。在toc结束之后,我想从主要内容开始。这些页面也应该编号。
简而言之:
来源(http://www.fpdf.org/en/script/script73.php,文件名 toc.php )
<?php
require('fpdf.php');
class PDF_TOC extends FPDF {
var $_toc=array();
var $_numbering=false;
var $_numberingFooter=false;
var $_numPageNum=1;
function AddPage($orientation='', $format='') {
parent::AddPage($orientation,$format);
if($this->_numbering)
$this->_numPageNum++;
}
function startPageNums() {
$this->_numbering=true;
$this->_numberingFooter=true;
}
function stopPageNums() {
$this->_numbering=false;
}
function numPageNo() {
return $this->_numPageNum;
}
function TOC_Entry($txt, $level=0) {
$this->_toc[]=array('t'=>$txt,'l'=>$level,'p'=>$this->numPageNo());
}
function insertTOC( $location=1,
$labelSize=20,
$entrySize=10,
$tocfont='Times',
$label='Table of Contents'
) {
//make toc at end
$this->stopPageNums();
$this->AddPage();
$tocstart=$this->page;
$this->SetFont($tocfont,'B',$labelSize);
$this->Cell(0,5,$label,0,1,'C');
$this->Ln(10);
foreach($this->_toc as $t) {
//Offset
$level=$t['l'];
if($level>0)
$this->Cell($level*8);
$weight='';
if($level==0)
$weight='B';
$str=$t['t'];
$this->SetFont($tocfont,$weight,$entrySize);
$strsize=$this->GetStringWidth($str);
$this->Cell($strsize+2,$this->FontSize+2,$str);
//Filling dots
$this->SetFont($tocfont,'',$entrySize);
$PageCellSize=$this->GetStringWidth($t['p'])+2;
$w=$this->w-$this->lMargin-$this->rMargin-$PageCellSize-($level*8)-($strsize+2);
$nb=$w/$this->GetStringWidth('.');
$dots=str_repeat('.',$nb);
$this->Cell($w,$this->FontSize+2,$dots,0,0,'R');
//Page number
$this->Cell($PageCellSize,$this->FontSize+2,$t['p'],0,1,'R');
}
//Grab it and move to selected location
$n=$this->page;
$n_toc = $n - $tocstart + 1;
$last = array();
//store toc pages
for($i = $tocstart;$i <= $n;$i++)
$last[]=$this->pages[$i];
//move pages
for($i=$tocstart-1;$i>=$location-1;$i--)
$this->pages[$i+$n_toc]=$this->pages[$i];
//Put toc pages at insert point
for($i = 0;$i < $n_toc;$i++)
$this->pages[$location + $i]=$last[$i];
}
function Footer() {
if(!$this->_numberingFooter)
return;
//Go to 1.5 cm from bottom
$this->SetY(-15);
//Select Arial italic 8
$this->SetFont('Arial','I',8);
$this->Cell(0,7,$this->numPageNo(),0,0,'C');
if(!$this->_numbering)
$this->_numberingFooter=false;
}
}
?>
实施例
<?php
require('toc.php');
$pdf= new PDF_TOC();
$pdf->SetFont('Times','',12);
$pdf->AddPage();
$pdf->Cell(0,5,'Cover',0,1,'C');
$pdf->AddPage();
$pdf->startPageNums();
$pdf->Cell(0,5,'TOC1',0,1,'L');
$pdf->TOC_Entry('TOC1', 0);
$pdf->Cell(0,5,'TOC1.1',0,1,'L');
$pdf->TOC_Entry('TOC1.1', 1);
$pdf->AddPage();
$pdf->Cell(0,5,'TOC2',0,1,'L');
$pdf->TOC_Entry('TOC2', 0);
$pdf->AddPage();
for($i=3;$i<=80;$i++){
$pdf->Cell(0,5,'TOC'.$i,0,1,'L');
$pdf->TOC_Entry('TOC'.$i, 0);
}
$pdf->stopPageNums();
//Generate and insert TOC at page 2
$pdf->insertTOC(2);
$pdf->Output();
?>
我该怎么做?如果需要,我可以澄清问题/代码。我很感激任何帮助来解决我的问题。提前谢谢。
答案 0 :(得分:2)
这可以通过几个简单的步骤完成。
创建PDF文档内容时,请在您希望TOC所在的位置留一个空页(或多个页面),例如:
$this->pdf->AddPage('P','letter');
$this->pdf->AddPage('P','letter');
在构建pdf内容时将TOC内容添加到数组中。
$toc[] = ['label' -> "item", 'page' -> $pdf->PageNo()];
构建完所有内容后,请保存当前页码:
$last_page = $pdf->PageNo();
将页码设置为您的TOC位置:
$pdf->page = 2;
创建您的TOC
重要提示:将页码重置为已保存的值:
$pdf->page = $last_page
答案 1 :(得分:0)
正如@Matt Raines已经提到的那样,你必须切换到TCPDF以实现所需的行为。