使用“查看"时如何获取 dompdf PDF 上的页码 [英] How to get page number on dompdf PDF when using "view"

查看:24
本文介绍了使用“查看"时如何获取 dompdf PDF 上的页码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

function getView ($file, $data=NULL) {如果(!空($数据))提取($数据);ob_start();if (is_file($file)) 包含($file);返回 ob_get_clean();}

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

您的订单#


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

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

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

是的,我看过类似 标题在 PDF 页面中的页面在 PHP 中使用 DOMPDFdompdf 页码,但仍然没有任何进展!我想知道 inline php 脚本是否与我如何将 HTML 作为字符串获取有关?有什么指示、想法、建议吗?

解决方案

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

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

<打击>

在 dompdf/dompdf_config.custom.inc.php 中

在运行时

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

然后,在我的 html 文件中

<script type="text/php">如果 ( isset($pdf) ) {//老的//$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 及更高版本$x = 72;$y = 18;$text = "{PAGE_NUM} 个,共 {PAGE_COUNT} 个";$font = $fontMetrics->get_font("helvetica", "bold");$size = 6;$color = 数组(255,0,0);$word_space = 0.0;// 默认$char_space = 0.0;// 默认$角度 = 0.0;// 默认$pdf->page_text($x, $y, $text, $font, $size, $color, $word_space, $char_space, $angle);}

如果你走这条路,别忘了重启Apache

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

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 $orderNumber is then used in the HTML to fill in it's proper places. For instance something like:

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


Ok, so point is, that's how I get my HTML. I then load it to my dompdf variable as:

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

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

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

解决方案

Update Regarding changes with 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 answer for the updated information.

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

In 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

<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

这篇关于使用“查看"时如何获取 dompdf PDF 上的页码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆