dompdf page_script()变量 [英] dompdf page_script() variables

查看:61
本文介绍了dompdf page_script()变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成具有多组页面编号以及未编号页面的PDF。我将无法提前告知每组页面多长时间是动态生成的。例如,PDF可能包含总共10个页面,其中第1-4页的页脚中有第X页,共4页,第5页未编号,第6-8页有第X页,共3页,而第9-10页未编号

I'm trying to generate a PDF with multiple sets of page numberings and with unnumbered pages as well. I will not be able to tell in advance how long each set of pages is as they are dynamically generated. For example, the PDF may contain 10 total pages where pages 1-4 have "Page X of 4" in the footer, page 5 is unnumbered, pages 6-8 have "Page X of 3", and pages 9-10 are unnumbered.

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

Right now I have page numberings implemented using the page_script() and text() functions. Essentially, what I think I need, is a way to be able to pass variable from the document to the page_script() function as the PDF is generated. This would allow me to add something like <?php $page_count = false; ?> or <?php $page_count_reset = true; ?> at different places in the document and act accordingly in the page_script() function. As far as I can tell, this doesn't seem possible.

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

I am able to set globals within the document <?php $GLOBALS['page_count'] = false; ?> and read them from within page_script() BUT these are processed all at once. So whatever I set the last $GLOBALS['page_count'] to in the document is what $GLOBALS['page_count'] is in the entire page_script() function.

我希望这有某种意义。我的下一步是生成多个PDF并将它们合并在一起,但这是我不想做的,除非必须这样做。有人有什么想法吗?

I hope this makes some kind of sense. My next step is to generate multiple PDFs and join them together but that's something I don't want to do unless I have to. Does anyone have any thought?

推荐答案

您处在正确的轨道上。实际上,几乎在那儿。您需要做的是在全局变量中跟踪各个部分。可以在HTML中放置以下代码段,以便与dompdf 0.6.1。一起使用。

You're on the right track. Almost there, actually. What you need to do is track your sections in your global variable. The following snippets can be placed in your HTML for use with dompdf 0.6.1.

1)首先在主体中设置一些全局变量:

1) Set up some global variables first thing in the body:

$GLOBALS['start_pages'] = array( );
$GLOBALS['current_start_page'] = null;
$GLOBALS['show_page_numbers'] = false;

2)在每个部分的开头,填写 start_pages global:

2) At the start of each section fill out the start_pages global:

$GLOBALS['current_start_page'] = $pdf->get_page_number();
$GLOBALS['start_pages'][$pdf->get_page_number()] = array(
  'show_page_numbers' => true,
  'page_count' => 1
);

3)在每个部分的末尾,记录页数:

3) At the end of each section, record the number of pages:

$GLOBALS['start_pages'][$GLOBALS['current_start_page']]['page_count'] = $pdf->get_page_number() - $GLOBALS['current_start_page'] + 1;

4)使用页面脚本为每个部分写出页面编号:

4) Use page script to write out your page numbering for each section:

$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);
    }
  }
');

最终文档看起来像这样:

The final document would look something like this:

<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=e980df4efacf5202c2f1d31579f09 >


You can see a sample of this in practice here: http://eclecticgeek.com/dompdf/debug.php?identifier=e980df4efacf5202c2f1d31579f09c56

这篇关于dompdf page_script()变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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