php 使用“查看”时如何获取dompdf PDF上的页码

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

How to get page number on dompdf PDF when using "view"

phppdfdompdf

提问by SpYk3HH

Ok, so I use the following snippet to get "views" of HTML with PHP variables loaded in as $dataso that I can do things like fill in tr's of data from a database call or whatever.

好的,所以我使用以下代码段来获取加载了 PHP 变量的 HTML 的“视图”,$data以便我可以执行诸如填充tr来自数据库调用或其他任何数据的操作。

function getView ($file, $data=NULL) {
    if (!empty($data)) extract($data);
    ob_start();
    if (is_file($file)) include($file);
    return ob_get_clean();
}

Gets used for something like, $htmlPDF = getView('receipt.php', array( 'orderNumber' => $orderNumber ));Where $orderNumberis then used in the HTML to fill in it's proper places. For instance something like:

用于类似的东西,然后在 HTML 中使用$htmlPDF = getView('receipt.php', array( 'orderNumber' => $orderNumber ));Where$orderNumber来填充它的正确位置。例如类似的东西:

<h1>You Order #<?= $orderNumber; ?></h1>



好的,重点是,这就是我获得 HTML 的方式。然后我将它加载到我的 dompdf 变量中:

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();

Which all works great. The problem is getting the inline phpscript to work to get PAge Numbers / Page Headers|Footers. I've followed the directions herebest I can, but I can't seem to make the right blend. Thus far, not having any errors, except I get 0Page Numbers anywhere!

这一切都很好。问题是让inline php脚本工作以获取页码/页眉|页脚。我已经尽我所能遵循了这里的指示,但我似乎无法做出正确的混合。到目前为止,没有任何错误,除了我在任何地方得到0页码!

And yes I've looked at pages like Header in PDF page using DOMPDF in PHPand dompdf page number, but still no forward progress, at all! I'm wondering if the inline phpscripting has to do with how I'm getting the HTML as a string? Any pointers, ideas, advice?

是的,我已经使用 PHP 中的 DOMPDFdompdf page number查看了 PDF 页面中的 Header 之类的页面,但仍然没有任何进展!我想知道inline php脚本是否与我如何将 HTML 作为字符串获取有关?任何指示,想法,建议?

回答by SpYk3HH

UpdateRegarding changeswith version of dompdf>= 0.7.0
1. Because the dompdf_config.inc.php file has been removed from this release (and is no longer referenced) all dompdf options should be set at run time.
4. The FontMetrics class is now instantiated instead of static. To simplify migration of embedded scripts from earlier versions of dompdf we provide access to the instantiated FontMetrics class via the $fontMetrics variable. Please update your embedded scripts. For example, FontMetrics::get_font('helvetica') would now be $fontMetrics->getFont('helvetica').
~ Thanks to Dennis Ameling's answerfor the updated information.

更新关于changes与版本dompdf> =1。因为dompdf_config.inc.php文件已经从这个版本删除(不再被引用)所有DOMPDF选项应该在运行时进行设置。4. FontMetrics 类现在是实例化的,而不是静态的。为了简化从早期版本的 dompdf 迁移嵌入脚本,我们通过 $fontMetrics 变量提供对实例化 FontMetrics 类的访问。请更新您的嵌入式脚本。例如,FontMetrics::get_font('helvetica') 现在是 $fontMetrics->getFont('helvetica')。~ 感谢Dennis Ameling对更新信息的回答0.7.0


Found my answer by looking over the dompdf_config.inc.phpfile. As it turns out, DOMPDF_ENABLE_PHPis set to falsethus causing the inline php script to be ignored. I simply edited dompdf_config.custom.inc.phpto the following and all is fine and working with the later code in the view.

通过查看dompdf_config.inc.php文件找到了我的答案。事实证明,DOMPDF_ENABLE_PHP设置为false因此导致内联 php 脚本被忽略。我只是编辑dompdf_config.custom.inc.php了以下内容,一切都很好,并且可以使用view.

In dompdf/dompdf_config.custom.inc.php

<?php
    define("DOMPDF_ENABLE_PHP", true);

在 dompdf/dompdf_config.custom.inc.php

<?php
    define("DOMPDF_ENABLE_PHP", true);

At Run Time

在运行时

$dompdf->set_option("isPhpEnabled", true);

Then, in my html file

然后,在我的 html 文件中

<body>
    <script type="text/php">
        if ( isset($pdf) ) {
            // OLD 
            // $font = Font_Metrics::get_font("helvetica", "bold");
            // $pdf->page_text(72, 18, "{PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(255,0,0));
            // v.0.7.0 and greater
            $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>
    <div

If you go this route, don't forget to restart Apache

如果你走这条路,别忘了重新开始 Apache

回答by Dennis Ameling

If you're using DOMPDF >= 0.7.0, the dompdf_config.inc.php file has been removedand adding a page number now requires a slightly different approach:

如果您使用 DOMPDF >= 0.7.0,则 dompdf_config.inc.php 文件已被删除,现在添加页码需要稍微不同的方法:

Because the dompdf_config.inc.php file has been removed from this release (and is no longer referenced) all dompdf options should be set at run time.

由于 dompdf_config.inc.php 文件已从此版本中删除(不再引用),因此应在运行时设置所有 dompdf 选项。

To enable PHP code to be executed by DOMPDF, use:

要使 DOMPDF 执行 PHP 代码,请使用:

$dompdf->set_option("isPhpEnabled", true);

Also, FontMetrics should now be calledby using $fontMetricsinstead of Font_Metrics, so the code mentioned by @user1231342435346354 changes slightly:

此外,现在应该使用$fontMetrics代替调用FontMetrics Font_Metrics,因此@user1231342435346354 提到的代码略有变化:

<script type="text/php">
if ( isset($pdf) ) { 
    $pdf->page_script('
        if ($PAGE_COUNT > 1) {
            $font = $fontMetrics->get_font("Arial, Helvetica, sans-serif", "normal");
            $size = 12;
            $pageText = "Page " . $PAGE_NUM . " of " . $PAGE_COUNT;
            $y = 15;
            $x = 520;
            $pdf->text($x, $y, $pageText, $font, $size);
        } 
    ');
}
</script>

回答by Sumit Kumar Gupta

I have a simple code to showing page number in footer of every page of dom pdf

我有一个简单的代码来在 dom pdf 的每一页的页脚中显示页码

#footer { position: fixed; right: 0px; bottom: 10px; text-align: center;border-top: 1px solid black;}
        #footer .page:after { content: counter(page, decimal); }
 @page { margin: 20px 30px 40px 50px; }

above code is css code

上面的代码是css代码

<div id="footer">
    <p class="page">Page </p>
  </div> 

You can change text position according to your requirement

您可以根据需要更改文本位置

回答by abePdIta

Improved version of Dennis Ameling's answerto take into account…

丹尼斯·阿梅林( Dennis Ameling)回答的改进版本考虑到了……

Translation and text centering

翻译和文本居中

Do not forget to enable PHP supportlike this $dompdf->set_option("isPhpEnabled", true);(or, if you're using laravel-dompdfPDF::setOptions(['isPhpEnabled' => true]);).

不要忘记像这样启用PHP 支持$dompdf->set_option("isPhpEnabled", true);(或者,如果您使用的是laravel-dompdfPDF::setOptions(['isPhpEnabled' => true]);)。

<script type="text/php">
    if (isset($pdf)) {
        $pdf->page_script('
            $text = sprintf(_("Page %d/%d"),  $PAGE_NUM, $PAGE_COUNT);
            // Uncomment the following line if you use a Laravel-based i18n
            //$text = __("Page :pageNum/:pageCount", ["pageNum" => $PAGE_NUM, "pageCount" => $PAGE_COUNT]);
            $font = null;
            $size = 9;
            $color = array(0,0,0);
            $word_space = 0.0;  //  default
            $char_space = 0.0;  //  default
            $angle = 0.0;   //  default

            // Compute text width to center correctly
            $textWidth = $fontMetrics->getTextWidth($text, $font, $size);

            $x = ($pdf->get_width() - $textWidth) / 2;
            $y = $pdf->get_height() - 35;

            $pdf->text($x, $y, $text, $font, $size, $color, $word_space, $char_space, $angle);
        '); // End of page_script
    }
</script>

But

If you only need $pageNum, using a CSS counteris a way simpler approach!

如果您只需要 $pageNum,使用 CSS 计数器是一种更简单的方法!

回答by user1231342435346354

First you must enable executing inline php scripts in dompdf_config.inc.phpfile. (change define("DOMPDF_ENABLE_PHP", true);this line to true)

首先,您必须启用在dompdf_config.inc.php文件中执行内联 php 脚本。(define("DOMPDF_ENABLE_PHP", true);将此行更改为true)

This code sets Page indicator in right corner of the header...

此代码在标题的右上角设置页面指示器...

    <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 . " " . $PAGE_NUM . " of " . $PAGE_COUNT;
                $y = 15;
                $x = 520;
                $pdf->text($x, $y, $pageText, $font, $size);
            } 
        ');
    }
</script> 

Be sure that script tag is within body tag... otherwise it doesn't work!

确保 script 标签在 body 标签内......否则它不起作用!