首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >dompdf page_script()变量

dompdf page_script()变量
EN

Stack Overflow用户
提问于 2015-03-13 23:28:54
回答 1查看 4.2K关注 0票数 1

我正在尝试生成一个PDF,它有多组页码,也有没有编号的页面。我将无法预先知道每组页面在动态生成时的长度。例如,PDF可能包含总共10页,其中第1-4页在页脚中有“pages of 4”,第5页没有编号,第6-8页有“pages of 3",第9-10页没有编号。

现在,我已经使用page_script()和text()函数实现了页码。本质上,我认为我需要的是一种在生成page_script()函数时能够将变量从文档传递到()函数的方法。这将允许我在文档中的不同位置添加类似<?php $page_count = false; ?><?php $page_count_reset = true; ?>的内容,并相应地在page_script()函数中进行操作。据我所知,这似乎不可能。

我能够在文档<?php $GLOBALS['page_count'] = false; ?>中设置全局值,并从page_script()中读取它们,但是这些都是一次性处理的。因此,无论我在文档中将最后的$GLOBALS‘’page_count‘设置为什么,都是整个page_script()函数中的$GLOBALS’‘page_count’。

我希望这有点道理。我的下一步是生成多个PDF并将它们连接在一起,但这是我不想做的事情,除非我必须这样做。有人有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-16 03:41:00

你走在正确的轨道上。实际上就快到了。您需要做的是跟踪全局变量中的区段。下面的片段可以放在HTML中,以便与dompdf 0.6.1一起使用。

1)首先在正文中设置一些全局变量:

代码语言:javascript
复制
$GLOBALS['start_pages'] = array( );
$GLOBALS['current_start_page'] = null;
$GLOBALS['show_page_numbers'] = false;

2)在每个部分的开头填写start_pages全局:

代码语言:javascript
复制
$GLOBALS['current_start_page'] = $pdf->get_page_number();
$GLOBALS['start_pages'][$pdf->get_page_number()] = array(
  'show_page_numbers' => true,
  'page_count' => 1
);

3)在每一节末尾,记录页数:

代码语言:javascript
复制
$GLOBALS['start_pages'][$GLOBALS['current_start_page']]['page_count'] = $pdf->get_page_number() - $GLOBALS['current_start_page'] + 1;

4)使用页面脚本为每一节写出页码:

代码语言:javascript
复制
$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);
    }
  }
');

最后的文件应该是这样的:

代码语言:javascript
复制
<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

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29043592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档