2016-03-04 108 views
0

基本上,我想在第2页开始目录(toc)并计算每个toc的页码。在toc结束后,我想从主要内容开始。这些页面也应该编号。FPDF目录和问题页码

在nuthell:

  • 每一页必须具有页脚,除了封面页
  • 其自带后TOC必须具有页号4,而不是页号1的第一个页面(不象如在下面的示例代码)

源(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

您可以使用TCPDF而不是FPDF吗?它具有大部分相同的方法调用并且功能更全面。特别是,在保持页面编号理智的同时添加目录更为直接。如果这是一个选项,我可以提供一些示例代码。 –

回答

0

as @Matt Raines已经提到你必须切换到TCPDF才能达到所需的行为。

1

这可以通过几个简单的步骤完成。

  1. 在创建PDF文档内容,留下一个空白页面(或网页)在您需要的TOC坐,如:

    • $this->pdf->AddPage('P','letter');
    • $this->pdf->AddPage('P','letter');
  2. 在构建PDF内容时将TOC内容添加到数组中。

    • $toc[] = ['label' -> "item", 'page' -> $pdf->PageNo()];
  3. 建设的所有内容后,保存当前页码:

    • $last_page = $pdf->PageNo();
  4. 设置页码到您的TOC的位置:

    • $pdf->page = 2;
  5. 创建TOC

    • (无论)
  6. 重要:重置页码为保存的值:

    • $pdf->page = $last_page