php dompdf 页码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13159645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:52:00  来源:igfitidea点击:

dompdf page number

phpdompdf

提问by yas

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

嗨,我正在使用 DOMPDF 生成 PDF 文件,我想知道如何获取页码。

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

我已经尝试了以下在 DOMPDF 的常见问题页面中提到的内容。并没有成功。注意:我还打开了内联 PHP 以及常见问题解答中提到的

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

有人可以指出我这个问题吗?我哪里做错了

回答by BrianS

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

您将内联脚本作为 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>
";
...

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

请注意,内联脚本当前必须出现在 BODY 元素内。否则在文档处理过程中将被忽略。

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

还有其他方法可以实现您想要的

回答by Craig Francis

Where you have '1/1' being used to get $x, this won't work when you have more than 10 pages.

如果您使用“1/1”来获取 $x,当您有 10 个以上的页面时,这将不起作用。

A possible solution does mean editing "cpdf_adapter.cls.php", so the _add_page_text() function used the following just after the str_replace()... about line 790:

一个可能的解决方案确实意味着编辑“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));

The idea is that you can pass in negative x/y coords, and they will work from the right/bottom edges.

这个想法是你可以传递负 x/y 坐标,它们将从右/下边缘开始工作。

Then in the HTML thats sent to DOMPDF, I used:

然后在发送到 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>

Or if you want to specify the x/y in px:

或者,如果您想以 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>