使用“视图"时如何在dompdf PDF上获取页码? [英] How to get page number on dompdf PDF when using "view"
问题描述
好,所以我使用下面的代码片段获取HTML的视图",并将PHP变量作为$data
加载,这样我就可以执行诸如从数据库调用或其他方式填充tr
数据的操作.
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();
}
用于$htmlPDF = getView('receipt.php', array( 'orderNumber' => $orderNumber ));
之类的内容,然后在HTML中使用$orderNumber
来填充正确的位置.例如:
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>
好的,关键是,这就是我获取HTML的方式.然后,将其加载到我的dompdf变量中:
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();
哪个都很棒.问题是使inline php
脚本可以正常工作以获取页码/页眉|页脚.我已经按照此处的指示进行操作,但我可以做到最好,但是我可以似乎无法做出正确的融合.到目前为止,没有任何错误,除了我在任何地方都能获得 0 页码!
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!
是的,我看过的页面如 PDF页眉在PHP中使用DOMPDF 和 dompdf页号,但仍然没有前进的方向!我想知道inline php
脚本是否与我将HTML作为字符串获取有关?有任何指示,想法和建议吗?
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?
推荐答案
更新 关于 更改 ,版本为
dompdf
> =0.7.0
强>
1.由于dompdf_config.inc.php文件已从此发行版中删除(并且不再被引用),因此所有dompdf选项都应在运行时设置.
4.现在,实例化FontMetrics类而不是静态类.为了简化从早期版本dompdf的嵌入式脚本的迁移,我们通过$ fontMetrics变量提供对实例化FontMetrics类的访问.请更新您的嵌入式脚本.例如,FontMetrics :: get_font('helvetica')现在将为$ fontMetrics-> getFont('helvetica').
〜感谢 Dennis Ameling 的
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.
通过查看dompdf_config.inc.php
文件找到了我的答案.事实证明,DOMPDF_ENABLE_PHP
设置为false
,从而导致内联php脚本被忽略.我只是对dompdf_config.custom.inc.php
进行了以下编辑,一切都很好,并且可以处理view
中的更高版本的代码.
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
.
<?php
define("DOMPDF_ENABLE_PHP", true);
$dompdf->set_option("isPhpEnabled", true);
然后,在我的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
如果走这条路,别忘了重启Apache
If you go this route, don't forget to restart Apache
这篇关于使用“视图"时如何在dompdf PDF上获取页码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!