我正在尝试使用多组页面编号和未编号的页面生成PDF。我无法事先告诉每组页面动态生成多长时间。例如,PDF可以包含总共10页,其中页面1-4在页脚中具有“页面X的4”,页面5是未编号的,页面6-8具有“页面X的3”,而页面9-10是未编号的
现在我使用page_script()和text()函数实现了页码。基本上,我认为我需要的是一种能够在生成PDF时将变量从文档传递到page_script()函数的方法。这样我就可以在文档的不同位置添加<?php $page_count = false; ?>
或<?php $page_count_reset = true; ?>
之类的内容,并在page_script()函数中进行相应的操作。据我所知,这似乎不太可能。
我可以在文档<?php $GLOBALS['page_count'] = false; ?>
中设置全局变量并从page_script()中读取它们但是这些都是一次性处理的。所以无论我在文档中设置最后的$ GLOBALS ['page_count']是什么,$ GLOBALS ['page_count']在整个page_script()函数中。
我希望这有一定道理。我的下一步是生成多个PDF并将它们连接在一起,但这是我不想做的事情,除非我必须这样做。有没有人有任何想法?
答案 0 :(得分:2)
你走在正确的轨道上。实际上几乎就在那里。您需要做的是跟踪全局变量中的部分。可以在HTML中放置以下代码段,以便与dompdf 0.6.1一起使用。
1)在体内首先设置一些全局变量:
$GLOBALS['start_pages'] = array( );
$GLOBALS['current_start_page'] = null;
$GLOBALS['show_page_numbers'] = false;
2)在每个部分的开头填写start_pages
全局:
$GLOBALS['current_start_page'] = $pdf->get_page_number();
$GLOBALS['start_pages'][$pdf->get_page_number()] = array(
'show_page_numbers' => true,
'page_count' => 1
);
3)在每个部分的末尾,记录页数:
$GLOBALS['start_pages'][$GLOBALS['current_start_page']]['page_count'] = $pdf->get_page_number() - $GLOBALS['current_start_page'] + 1;
4)使用页面脚本为每个部分写出您的页码:
$pdf->page_script('
if ($pdf) {
if (array_key_exists($PAGE_NUM, $GLOBALS["start_pages"])) {
$GLOBALS["current_start_page"] = $PAGE_NUM;
$GLOBALS["show_page_numbers"] = $GLOBALS["start_pages"][$GLOBALS["current_start_page"]]["show_page_numbers"];
}
if ($GLOBALS["show_page_numbers"]) {
$font = Font_Metrics::get_font("helvetica", "bold");
$pdf->text(10, 10, "Page " . ($PAGE_NUM - $GLOBALS["current_start_page"] + 1) . " of " . $GLOBALS["start_pages"][$GLOBALS["current_start_page"]]["page_count"], $font, 12);
}
}
');
最终文件看起来像这样:
<html>
<body>
<script type="text/php">
// setup
$GLOBALS['start_pages'] = array( );
$GLOBALS['current_start_page'] = null;
$GLOBALS['show_page_numbers'] = false;
</script>
<script type="text/php">
// section start
$GLOBALS['current_start_page'] = $pdf->get_page_number();
$GLOBALS['start_pages'][$pdf->get_page_number()] = array(
'show_page_numbers' => true,
'page_count' => 1
);
</script>
<p>lorem ipsum ... <!-- lots of text -->
<script type="text/php">
// record total number of pages for the section
$GLOBALS['start_pages'][$GLOBALS['current_start_page']]['page_count'] = $pdf->get_page_number() - $GLOBALS['current_start_page'] + 1;
</script>
<div style="page-break-before: always;"></div>
<script type="text/php">
// section start
$GLOBALS['current_start_page'] = $pdf->get_page_number();
$GLOBALS['start_pages'][$pdf->get_page_number()] = array(
'show_page_numbers' => false,
'page_count' => 1
);
</script>
<p>lorem ipsum ... <!-- lots of text -->
<script type="text/php">
// record total number of pages for the section
$GLOBALS['start_pages'][$GLOBALS['current_start_page']]['page_count'] = $pdf->get_page_number() - $GLOBALS['current_start_page'] + 1;
</script>
<script type="text/php">
$pdf->page_script('
if ($pdf) {
if (array_key_exists($PAGE_NUM, $GLOBALS["start_pages"])) {
$GLOBALS["current_start_page"] = $PAGE_NUM;
$GLOBALS["show_page_numbers"] = $GLOBALS["start_pages"][$GLOBALS["current_start_page"]]["show_page_numbers"];
}
if ($GLOBALS["show_page_numbers"]) {
$font = Font_Metrics::get_font("helvetica", "bold");
$pdf->text(10, 10, "Page " . ($PAGE_NUM - $GLOBALS["current_start_page"] + 1) . " of " . $GLOBALS["start_pages"][$GLOBALS["current_start_page"]]["page_count"], $font, 12);
}
}
');
</script>
</body>
</html>
您可以在此处查看此示例:http://eclecticgeek.com/dompdf/debug.php?identifier=e980df4efacf5202c2f1d31579f09c56