如何获取 DOMPDF 中的总页数?

发布于 2024-10-29 06:24:28 字数 390 浏览 5 评论 0原文

例如第 1 页,共 5 页

有一个在线示例说明如何获取 Page 1 部分,但不获取 of 5 部分。就是这样:

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

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

For example Page 1 of 5.

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

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

I'm using version 0.6 and $PAGE_NUM and $PAGE_COUNT does not work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

情归归情 2024-11-05 06:24:28

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

dompdf 0.7.0dompdf_config.inc.php 文件已被删除(并且不再引用):所有 dompdf 选项都应在运行时设置。

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

$domPdfOptions = new Options();

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

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

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

<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>

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

For people coming here using a new version of DomPdf

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

This means you have to create a new instance of Options class

$domPdfOptions = new Options();

And then you may enable PHP inline using the following line

$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>

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

信愁 2024-11-05 06:24:28

默认情况下,由于安全原因,您需要在Dompdf_config.custom.inc.inc.php中自己启用Inline PHP。参见在这里

目前,您正在使用的CSS不支持全部页面计数,但我们计划使其在0.6 FINS中起作用。

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

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.

离线来电— 2024-11-05 06:24:28

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

使用这段代码,你可以把它放在你喜欢的地方,它获取文档的高度和宽度,并将 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);
        } 
    ');
}

见本页末尾

Check you have enabled inline_pdf in dompdf.

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.

<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

王权女流氓 2024-11-05 06:24:28

请参阅此错误报告中名为 issues121.php 的附件。为我工作。据我了解,您无法回显页码,但您可以将其绘制在某个地方。

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

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.

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

李白 2024-11-05 06:24:28

嗨,这是我的完整代码...在所有 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);
?>

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

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

<?
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..

时光沙漏 2024-11-05 06:24:28

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

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

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

这就是我分离我的网站和他们的网站的方式,我还得到了 $pages 作为刀片变量。

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

使用样式表:

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

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.

@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.

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

With Stylesheet:

.pagenum:before { content: counter(page); }
寄人书 2024-11-05 06:24:28

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

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

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

In php, you can access dompdf variables:

$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

夜光 2024-11-05 06:24:28

$PAGE_COUNT 是总页数。

示例

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

文档

更新

如果您使用的是旧版本,但并非如此支持,升级。否则,你可能会不走运。

$PAGE_COUNT is the total number of pages.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文