2012-10-31 68 views
4

您好我正在使用DOMPDF生成PDF文件,我想知道如何獲取頁碼。dompdf頁碼

我在DOMPDF的常見問題頁面中嘗試了以下內容。並沒有成功。 注:我也有轉內聯PHP,以及它在幫助提

下面是我的代碼

<?php 
require_once("dompdf/dompdf_config.inc.php"); 


ob_start(); 
//be sure this file exists, and works outside of web context etc.) 


$dompdf = new DOMPDF(); 


$html="  <script type='text/php'>"; 
     if (isset($pdf)) { 
      $font = Font_Metrics::get_font("yourfont", "normal"); 
      $size = 9; 
      $y = $pdf->get_height() - 24; 
      $x = $pdf->get_width() - 15 - Font_Metrics::get_text_width("1/1", $font, $size); 
      $pdf->page_text($x, $y, "{PAGE_NUM}/{PAGE_COUNT}", $font, $size); 
     } 
$html.="  </script>"; 

$html .=" 
     aaaaa<br/>aaaaaa<br/>aaaaaa<br/>aaaaa<br/>aaaaaa<br/>aaaaaa<br/>aaaa<br/>aaaaa<br/>aaaaaa<br/>aaaaa<br/> 
     aaaaa<br/>aaaaaaa<br/>aaaaaa<br/> 
aaaaa<br/>aaaaaa<br/> 
     aaaaaaa<br/>aaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/> 
     aaaaaaa<br/>aaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/> 
     aaaaaaaa<br/>aaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/> 
     aaaaaaaa<br/>aaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/> 
     aaaaaaaa<br/>aaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/> 
     aaaaaaaaa<br/>aaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/> 
     aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/> 
     </body></html>"; 


$dompdf->load_html($html); 
$dompdf->render(); 
$dompdf->stream("sample.pdf"); 


?> 

有人能指出我出這個問題嗎?我在哪裏弄錯了

回答

8

您正在將您的內聯腳本作爲PHP頁面的一部分運行,而不是將它傳遞給dompdf。如下代碼可以寫入(截斷相關章節):

... 
$html=" 
<html> 
    <body> 
    <script type='text/php'> 
     if (isset($pdf)) { 
     $font = Font_Metrics::get_font('helvetica', 'normal'); 
     $size = 9; 
     $y = $pdf->get_height() - 24; 
     $x = $pdf->get_width() - 15 - Font_Metrics::get_text_width('1/1', $font, $size); 
     $pdf->page_text($x, $y, '{PAGE_NUM}/{PAGE_COUNT}', $font, $size); 
     } 
    </script> 
"; 
... 

注意,內聯腳本目前必須出現在body元素中。否則在文檔處理過程中會被忽略。

There are other ways to achieve what you want as well

0

如果您使用'1/1'來獲取$ x,當您的頁數超過10頁時,這將不起作用。

一個可能的解決方案是否意味着編輯 「cpdf_adapter.cls.php」,所以_add_page_text()函數中使用下列只是str_replace()函數...約行之後790:

if ($x < 0) $x = (($this->_width + $x) - $this->get_text_width($text, $font, $size)); 
if ($y < 0) $y = (($this->_height + $y) - $this->get_font_height($font, $size)); 

的想法是你可以通過負X/Y座標,他們將從右邊/底邊開始工作。

然後發送到DOMPDF的HTML多數民衆贊成,我用:

<script type="text/php"> 
    $pdf->page_text(-30, -32, "{PAGE_NUM} of {PAGE_COUNT}", Font_Metrics::get_font("serif"), 10, array(0,0,0)); 
</script> 

或者,如果你想在指定的PX X/Y:

<script type="text/php"> 
    $pdf->page_text(((-30 * 72)/DOMPDF_DPI), ((-32 * 72)/DOMPDF_DPI), "{PAGE_NUM} of {PAGE_COUNT}", Font_Metrics::get_font("serif"), 10, array(0,0,0)); 
</script>