脚本中的 $pdf 是什么以及如何使用 page_text 函数在页面 dompdf 中添加文本?

What is $pdf in script and how to use page_text function to add text inside page dompdf?

我是 dompdf 的新用户,我正在尝试进行分页并将自定义 html 添加到特定页码。我在一些帖子中看到他们有 $pdf、PAGE_NUM、PAGE_COUNT... 但我不知道如何使用它们。 这是我所做的:

PdfController.php

<?php
$html = view('pdf')->render();
$dompdf = new Dompdf();
$dompdf->load_html($html);

$canvas = $dompdf->get_canvas();
$cpdf = $canvas->get_cpdf();

$font = $dompdf->getFontMetrics()->get_font("helvetica", "bold");

$firstPageId = $cpdf->getFirstPageId();
$objects = $cpdf->objects;
$pages = array_filter($objects, function($v) {
    return $v['t'] == 'page';
});
$number = 1;
foreach($pages as $pageId => $page) {
    if(($pageId + 1) !== $firstPageId) {
        $canvas->reopen_object($pageId + 1);
        $canvas->text(72, 18, "<div>Header: $number</div>", $font, 6, array(0,0,0));
        $canvas->close_object();
        $number++;
    }
}

$dompdf->render();
$dompdf->stream("dompdf_out.pdf", ["Attachment" => false]);
exit(0);
?>

这里是 html:

<style>
    body {
      counter-reset: page;
    }

    .footer:not(:first-child) .page-number:before {
      counter-increment: page;
      color: red;
      content: 'Page ';
    }

    .footer:not(:first-child) .page-number:after {
      color: red;
      content: counter(page);
    }
</style>

<script type="text/php">
    if ( isset($pdf) ) {
        $font = Font_Metrics::get_font("helvetica", "bold");
        if (PAGE_NUM===2) $pdf->page_text(72, 18, "<div>Header: {PAGE_NUM} of {PAGE_COUNT}</div>",
                        $font, 6, array(0,0,0));
    }
</script>

<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 0</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 1</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 2</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 3</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 4</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 5</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 6</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 7</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 8</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 9</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 10</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 11</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 12</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 13</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>
<hr>
<div class="footer fixed-section">
  <div class="left">
    <span class="report-time">08/05/2016 09:43:06</span><br>
    <span class="promo">Generated by ...</span>
  </div>
  <div class="center">
    <span class="signature-warning">This is page 14</span>
  </div>
  <div class="right">
    <span class="page-number"></span>
  </div>
</div>

还有我的问题脚本中的$pdf是什么以及如何使用page_text函数在页面dompdf中添加文本?或者如何在 php 中做同样的事情会更好?

这里发生了很多事情,但我会尽力回答。我假设您使用的是当前版本。

首先,Canvas 对象。这提供了对底层 PDF writer 后端的直接访问。因此你不能用它来渲染 HTML。文本的任何样式都必须手动制作。因此,像您尝试使用 DIV 元素那样将 HTML 写入 PDF 是行不通的。写出你想要的文字即可。

其次,$pdf变量。该变量是一个 eval-local 变量,您可以在您的页面脚本中使用它并保存对 Canvas 对象的引用。您无需执行任何操作即可访问它,只需在任何 Canvas 脚本中引用它即可。

在您的包含文件中(例如 PdfController.php):

$canvas = $dompdf->get_canvas();
$canvas->page_script(<<<EOF
  if ($PAGE_NUM !== 1) {
    $font = $fontMetrics->get_font("helvetica", "bold");
    $pdf->text(72, 18, "Header: $PAGE_NUM of $PAGE_COUNT", $font, 6, array(0,0,0));
  }
EOF;
);

请注意,您无需解析 PDF 对象即可找到所需的页面。 HTML 完全呈现后,每个页面上的页面脚本 运行 因此您只需检查当前页面即可。

在您的实际 HTML 文档中:

<script>
if ( !isset($pdf) ) { return; }
$pdf->page_script(<<<EOF
  if ($PAGE_NUM !== 1) {
    $font = $fontMetrics->get_font("helvetica", "bold");
    $pdf->text(72, 18, "Header: $PAGE_NUM of $PAGE_COUNT", $font, 6, array(0,0,0));
  }
EOF;
);
</script>

请注意,为了使用 HTML 嵌入的脚本,您必须启用它。这是一个安全选项,因为如果您允许未过滤的用户内容,HTML-嵌入的脚本可能会被用作利用您的系统的载体。

要启用 HTML- 嵌入式脚本:

$dompdf = new Dompdf(['isPhpEnabled' => true]);

三、关于page_scriptpage_text。这些 Canvas 方法在 HTML 完全呈现后在 PDF 文档的每一页的上下文中执行。这与仅在活动 PDF 页面上执行的其他方法(例如 text)不同。

目前,由于 Dompdf 呈现文档的方式,总页数 ($PAGE_COUNT) 只能准确反映 HTML 文档完全呈现后的页数。因此,要将该值写入 PDF,您必须使用 page_text 或 page_script.


仅供参考,虽然您通常不会混合使用 Canvas 脚本和 HTML,但可以通过有限的方式混合使用。这是最近关于该主题的讨论:https://github.com/dompdf/dompdf/discussions/2475