2014-02-17 79 views
1

我正在使用TCPDF庫以PHP/HTML生成PDF。該文檔可以在這裏找到:http://www.tcpdf.org/doc/code/classTCPDF.htmlTCPDF目錄和頁碼問題

我想創建一個目錄(如example 59上演示TCPDF網站),但我遇到的幾個問題:

1)內容頁碼錶中顯示爲最後一頁的文件。 (實際上8中的8個是封面後的8)。

2)其他頁面的頁碼未調整。 TOC後面的頁面應該是8中的3,而不是8中的2。

3)目錄的書籤有正確的頁碼,但這些數字與這些頁面上的頁碼不匹配(與問題#2有關)。

我是如何生成書籤:

我通過調用每增加一頁後書籤()方法來生成每個頁面的書籤。

$pdf->AddPage(); 
$pdf->Bookmark('Chapter 1', 0, 0, '', 'B', array(0,64,128)); 

如何要生成的內容表:

這是直接從實施例59上面鏈接撕開。在這個例子中,由於沒有封面,情況稍有不同。

// add a new page for TOC 
$pdf->addTOCPage(); 

// write the TOC title and/or other elements on the TOC page 
$pdf->SetFont('times', 'B', 16); 
$pdf->MultiCell(0, 0, 'Table Of Content', 0, 'C', 0, 1, '', '', true, 0); 
$pdf->Ln(); 
$pdf->SetFont('helvetica', '', 10); 

// define styles for various bookmark levels 
$bookmark_templates = array(); 

/* 
* The key of the $bookmark_templates array represent the bookmark level (from 0 to n). 
* The following templates will be replaced with proper content: 
*  #TOC_DESCRIPTION# this will be replaced with the bookmark description; 
*  #TOC_PAGE_NUMBER# this will be replaced with page number. 
* 
* NOTES: 
*  If you want to align the page number on the right you have to use a monospaced font like courier, otherwise you  can left align using any font type. 
*  The following is just an example, you can get various styles by combining various HTML elements. 
*/ 

// A monospaced font for the page number is mandatory to get the right alignment 
$bookmark_templates[0] = '<table border="0" cellpadding="0" cellspacing="0" style="background-color:#EEFAFF"><tr><td  width="155mm"><span style="font-family:times;font-weight:bold;font-size:12pt;color:black;">#TOC_DESCRIPTION#</span></td> <td width="25mm"><span style="font-family:courier;font-weight:bold;font-size:12pt;color:black;" align="right"> #TOC_PAGE_NUMBER#</span></td></tr></table>'; 
$bookmark_templates[1] = '<table border="0" cellpadding="0" cellspacing="0"><tr><td width="5mm">&nbsp;</td><td width=" 150mm"><span style="font-family:times;font-size:11pt;color:green;">#TOC_DESCRIPTION#</span></td><td width="25mm"><span  style="font-family:courier;font-weight:bold;font-size:11pt;color:green;" align="right">#TOC_PAGE_NUMBER#</span></td></tr ></table>'; 
$bookmark_templates[2] = '<table border="0" cellpadding="0" cellspacing="0"><tr><td width="10mm">&nbsp;</td><td width=" 145mm"><span style="font-family:times;font-size:10pt;color:#666666;"><i>#TOC_DESCRIPTION#</i></span></td><td width="25mm "><span style="font-family:courier;font-weight:bold;font-size:10pt;color:#666666;" align="right">#TOC_PAGE_NUMBER#</span ></td></tr></table>'; 
// add other bookmark level templates here ... 

// add table of content at page 1 
// (check the example n. 45 for a text-only TOC 
$pdf->addHTMLTOC(2, 'INDEX', $bookmark_templates, true, 'B', array(128,0,0)); 

// end of TOC page 
$pdf->endTOCPage(); 

如何,我發現了頁碼的頁腳:

// Page footer 
public function Footer() { 

$pageN = $this->PageNo(); 

if($pageN === 1){ 

    // Do nothing 


    } else { 

    // Position at 15 mm from bottom 
    $this->SetY(-15); 

    // Set font 
    $this->SetFont($helvetica_light, '', 10); 
    $this->setTextColor(255,255,255); 

    // Page number 
    $this->Cell(0, 12, ' '.$this->PageNo().' of '.$this->getNumPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M'); 

    } 
} 

基本上,我想要的目錄中下跌2頁上,有一個頁腳那就是說第2頁,所有後續頁碼都要正確標註。我怎樣才能做到這一點?如果需要,我可以澄清問題/代碼。

任何幫助表示讚賞:)

+1

因此,目錄顯示正確的頁碼,但頁面底部的頁碼不正確,正確嗎?因此,由於在創建目錄時沒有任何問題,您能否提供代碼中要顯示的頁碼的部分? – olger

+0

@olger感謝您的回覆!看我的編輯。 – Andrew

回答

5

您做生意()是有點古怪,當它涉及到計算頁。它按照創建的順序輸出它所在的頁面,並且由於ToC頁面被創建爲最後一頁,它將顯示該頁面作爲最後一頁。

所以改用

// Page number 
    $this->Cell(0, 12, ' '.$this->getAliasNumPage().' of '.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M'); 

您所提供的代碼的其餘部分,像檢查,如果它是第一頁,應與您做生意工作()。

+1

工程很好。感謝您的快速答覆!我拉着我的頭髮:) – Andrew

+1

沒問題,不要忘記檢查接受的答案:) – olger

+0

接受!另外:關於如何從頁腳中的頁碼中刪除右邊距的任何想法?正確的對齊不會把數字放在邊緣上。任何想法? – Andrew