Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
851 views
in Technique[技术] by (71.8m points)

php - dompdf page number

Hi I am using DOMPDF to generate PDF file, I would like to know how to get page number.

I have try the following as mention in FAQ page of DOMPDF. and not successful. NOTE: I also have turn on inline PHP as well as it mention in FAQ

following is my code

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


 ?>

Can someone point me out this issue please? where did i make it wrong

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are running your inline script as part of the PHP page instead of passing it to dompdf. Your code could be written as follows (truncated to the relevant section):

...
$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>
";
...

Note that inline script must currently appear inside the BODY element. Otherwise it will be ignored during document processing.

There are other ways to achieve what you want as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...