您好我正在使用DOMPDF生成PDF文件,我想知道如何获取页码。
我在DOMPDF的FAQ页面中提到了以下内容。并没有成功。 注意:我也打开了内联PHP以及它在FAQ
中提到的内容以下是我的代码
<?php
require_once("dompdf/dompdf_config.inc.php");
ob_start();
//be sure this file exists, and works outside of web context etc.)
$dompdf = new DOMPDF();
$html=" <script type='text/php'>";
if ( isset($pdf) ) {
$font = Font_Metrics::get_font("yourfont", "normal");
$size = 9;
$y = $pdf->get_height() - 24;
$x = $pdf->get_width() - 15 - Font_Metrics::get_text_width("1/1", $font, $size);
$pdf->page_text($x, $y, "{PAGE_NUM}/{PAGE_COUNT}", $font, $size);
}
$html.=" </script>";
$html .="
aaaaa<br/>aaaaaa<br/>aaaaaa<br/>aaaaa<br/>aaaaaa<br/>aaaaaa<br/>aaaa<br/>aaaaa<br/>aaaaaa<br/>aaaaa<br/>
aaaaa<br/>aaaaaaa<br/>aaaaaa<br/>
aaaaa<br/>aaaaaa<br/>
aaaaaaa<br/>aaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>
aaaaaaa<br/>aaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>
aaaaaaaa<br/>aaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>
aaaaaaaa<br/>aaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>
aaaaaaaa<br/>aaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>
aaaaaaaaa<br/>aaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>
aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>aaaaaaaaaaaaaaaaa<br/>
</body></html>";
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>
有人能指出我这个问题吗?我哪里弄错了
答案 0 :(得分:10)
您正在将内联脚本作为PHP页面的一部分运行,而不是将其传递给dompdf。您的代码可以编写如下(截断到相关部分):
...
$html="
<html>
<body>
<script type='text/php'>
if ( isset($pdf) ) {
$font = Font_Metrics::get_font('helvetica', 'normal');
$size = 9;
$y = $pdf->get_height() - 24;
$x = $pdf->get_width() - 15 - Font_Metrics::get_text_width('1/1', $font, $size);
$pdf->page_text($x, $y, '{PAGE_NUM}/{PAGE_COUNT}', $font, $size);
}
</script>
";
...
请注意,内联脚本当前必须出现在BODY元素中。否则,在文档处理过程中将忽略它。
答案 1 :(得分:0)
如果您使用'1/1'来获得$ x,那么当您的页数超过10页时,这将不起作用。
一个可能的解决方案意味着编辑“cpdf_adapter.cls.php”,所以_add_page_text()函数在str_replace()之后使用以下内容...关于第790行:
if ($x < 0) $x = (($this->_width + $x) - $this->get_text_width($text, $font, $size));
if ($y < 0) $y = (($this->_height + $y) - $this->get_font_height($font, $size));
这个想法是你可以传递负x / y坐标,它们将从右边/底边工作。
然后在发送给DOMPDF的HTML中,我使用了:
<script type="text/php">
$pdf->page_text(-30, -32, "{PAGE_NUM} of {PAGE_COUNT}", Font_Metrics::get_font("serif"), 10, array(0,0,0));
</script>
或者如果要在px中指定x / y:
<script type="text/php">
$pdf->page_text(((-30 * 72) / DOMPDF_DPI), ((-32 * 72) / DOMPDF_DPI), "{PAGE_NUM} of {PAGE_COUNT}", Font_Metrics::get_font("serif"), 10, array(0,0,0));
</script>
答案 2 :(得分:0)
对我有用的代码。您可以根据 PDF 的大小自定义应位于的位置:
$pdf->load_html($html);
$pdf->render();
$dompdf = $pdf->getDomPDF();
$color = array(0, 0, 0);
$font = null;
$size = 10;
$canvas = $dompdf->get_canvas();
$canvas->page_text(0, 0, "{PAGE_NUM} of {PAGE_COUNT}", $font, $size, $color);
请记住,颜色数组不包含 RGB 值。您可以从行 RYB here 中获取适当的值作为范围 (0,1) 中的值,即 array(0.46,0.46,0.46)
。