dompdf - text on a single page / page_script doesnt work

淺唱寂寞╮ 提交于 2019-12-08 05:19:27

$canvas wouldn't be available from the page script as it is out of scope. The canvas object can be referenced inside your page script as $pdf.

Try the following page_script call instead:

$canvas->page_script('
  if ($PAGE_NUM > 1) {
    $current_page = $PAGE_NUM-1;
    $total_pages = $PAGE_COUNT-1;
    $font = $fontMetrics->getFont("open sans condensed", "normal"); // or bold, italic, or bold_italic
    $pdf->text(0, 0, "$current_page / $total_pages", $font, 10, array(0,0,0));
  }
};

There are a few variables available from within page scripts or embedded script:

  • $PAGE_NUM: current page number
  • $PAGE_COUNT: total number of pages
  • $pdf: the canvas object
  • $fontMetrics: an instance of the FontMetrics class

If you're using page text you have access to the following template variables:

  • {PAGE_NUM}: current page number
  • {PAGE_COUNT}: total number of pages

Note: Support for parsing embedded script or page scripts is disabled by default. Enable it with the following command: $dompdf->set_option("isPhpEnabled", true);. This must be done prior to calling $dompdf->render();.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!