dongzhukuai8177 2016-03-04 19:21
浏览 113
已采纳

FPDF目录和页码问题

basically, I'd like to start the Table of Contents (toc) on page 2 and count each page number of the toc. After the toc has ended I would like to start with the main content. These page should be numbered, too.

In a nuthell:

  • each page has to have a footer, except the cover page
  • the first page which comes after the toc has to have the page number 4 instead of page number 1 (not like as in the example code below)

Source (http://www.fpdf.org/en/script/script73.php, filename is 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;
    }
}
?>

Example

<?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();
?>

How can I do this? I can clarify the question/code if needed. I would appreciate any help to resolve my issue. Thanks in advance.

  • 写回答

2条回答 默认 最新

  • douchun6221 2016-03-30 19:02
    关注

    as @Matt Raines already mentioned you have to switch to TCPDF to achieve the desired behavior.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况