php 如何获取 DOMPDF 中的总页数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5486376/
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 21:36:05  来源:igfitidea点击:

How can I get the total number of pages in DOMPDF?

phpdompdf

提问by Pedro

For example Page 1 of 5.

例如Page 1 of 5

There's an example onlineof how to get teh Page 1part but not the of 5part. This is it:

网上有一个示例,说明如何获取Page 1零件而不是of 5零件。就是这个:

.pagenum:before { content: "Page " counter(page); }

I'm using version 0.6 and $PAGE_NUMand $PAGE_COUNTdoes not work.

我使用的是0.6版本,并$PAGE_NUM$PAGE_COUNT不工作。

回答by Fabien Ménager

By default, inline PHP is disabled for security reasons, you need to enable it yourself in dompdf_config.custom.inc.php. See here.

默认情况下,出于安全原因禁用内联 PHP,您需要在 dompdf_config.custom.inc.php 中自行启用它。见这里

For now, total page count is not supported with the CSS you are using, we are planning to make it work in 0.6 final though.

目前,您使用的 CSS 不支持总页数,但我们计划使其在 0.6 final 中工作。

回答by Erenor Paz

For people coming here using a new version of DomPdf

对于使用新版本 DomPdf 来到这里的人们

Since dompdf0.7.0, the dompdf_config.inc.phpfile has been removed (and is no longer referenced): all dompdf options should be set at run time.

自 以来,该文件已被删除(不再引用):所有 dompdf 选项都应在运行时设置。dompdf0.7.0dompdf_config.inc.php

This means you have to create a new instance of Optionsclass

这意味着您必须创建一个新的Options类实例

$domPdfOptions = new Options();

And then you may enable PHP inline using the following line

然后您可以使用以下行启用内联 PHP

$domPdfOptions->set("isPhpEnabled", true);

The rest of the codes are correct and will show a page number and page count

其余代码正确,将显示页码和页数

<script type="text/php">
    if (isset($pdf))
    {
        $x = 72;
        $y = 18;
        $text = "{PAGE_NUM} of {PAGE_COUNT}";
        $font = $fontMetrics->get_font("helvetica", "bold");
        $size = 6;
        $color = array(255,0,0);
        $word_space = 0.0;  //  default
        $char_space = 0.0;  //  default
        $angle = 0.0;   //  default
        $pdf->page_text($x, $y, $text, $font, $size, $color, $word_space, $char_space, $angle);
    }
</script>

UpdateAs pointed out by @london-smith, this also works for DomPDF 0.8.1

更新正如@london-smith 所指出的,这也适用于 DomPDF 0.8.1

回答by Salvador P.

Check you have enabled inline_pdf in dompdf.

检查您是否在 dompdf 中启用了 inline_pdf。

Use this code, you can put where you like, it gets the height and width of the document and puts the page/total_pages at the bottom right.

使用这段代码,你可以把它放在你喜欢的地方,它获取文档的高度和宽度并将page/total_pages放在右下角。

<script type = "text/php">
if ( isset($pdf) ) { 
    $pdf->page_script('
        if ($PAGE_COUNT > 1) {
            $font = Font_Metrics::get_font("Arial, Helvetica, sans-serif", "normal");
            $size = 12;
            $pageText = $PAGE_NUM . "/" . $PAGE_COUNT;
            $y = $pdf->get_height() - 24;
            $x = $pdf->get_width() - 15 - Font_Metrics::get_text_width($pageText, $font, $size);
            $pdf->text($x, $y, $pageText, $font, $size);
        } 
    ');
}

Seen at the end of this page

在本页末尾看到

回答by Henry Gabriel González Montejo

Hi this is mi full code... after all the HTML put this..

嗨,这是我的完整代码......毕竟HTML把这个......

<?
require_once("dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html(ob_get_clean());
$dompdf->render();
$canvas = $dompdf->get_canvas(); 
$font = Font_Metrics::get_font("helvetica", "bold"); 
$canvas->page_text(512, 10, "Página: {PAGE_NUM} de {PAGE_COUNT}",$font, 8, array(0,0,0)); 

$filename = "yourNameConvention".date("Y-m-d").'.pdf';
$dompdf->stream($filename);
?>

Test with a simple file, then put all the relevant code with querys, etc.. etc..

用一个简单的文件进行测试,然后将所有相关代码与查询等..等..

回答by Kevin Wizzle

I dont know which kind of content you want to render. I got a bunch of pictures and a maximal Pictures per Site constant.

我不知道您要呈现哪种内容。我得到了一堆图片和每个站点常量的最大图片。

So a function gives me the site number (total/max) and then I got everything I need to start creating the pdf.

因此,一个函数为我提供了站点编号(总数/最大值),然后我得到了开始创建 pdf 所需的一切。

@for ($i = 1;$i <= $pages; $i++)
@if ($i !== 1) 
   <div style="page-break-before: always;"></div>
@endif
   #Code for each Site
@end

Thats how I seperate my Sites and their I also got $pages as a blade variable.

这就是我如何分离我的站点和他们的我也将 $pages 作为刀片变量。

<div class="info_footer">
     Page <span class="pagenum"></span> / {{$pages}}
</div>

With Stylesheet:

使用样式表:

.pagenum:before { content: counter(page); }

回答by alex

$PAGE_COUNTis the total number of pages.

$PAGE_COUNT是总页数。

Example

例子

<script type="text/php">
if (isset($pdf) ) {
    echo $PAGE_COUNT;
}
</script>

Documentation

文档

Update

更新

If you are using an old version where this is not supported, upgrade. Otherwise, you may be out of luck.

如果您使用的是不受支持的旧版本,请升级。否则,您可能会倒霉。

回答by Exception e

In php, you can access dompdf variables:

在 php 中,您可以访问 dompdf 变量:

$PAGE_NUM       the current page number
$PAGE_COUNT     the total number of pages in the document 

more info http://code.google.com/p/dompdf/wiki/Usage

更多信息http://code.google.com/p/dompdf/wiki/Usage

回答by Dustin Butler

See the attachment named issue121.php on this bug report. Worked for me. As I understand it you can't echo the page num but you can draw it somewhere.

请参阅此错误报告中名为 issue121.php 的附件。为我工作。据我了解,您无法回显页面编号,但您可以将其绘制在某处。

http://code.google.com/p/dompdf/issues/detail?id=121

http://code.google.com/p/dompdf/issues/detail?id=121