Php DOMPDF如何添加页码

Php DOMPDF如何添加页码,php,dompdf,Php,Dompdf,我检查了与这个问题有关的每一个答案,但都不起作用 我想在页脚添加页码,我已经尝试了下面的代码,但没有成功。谁能告诉我哪里出了问题 $dompdf = new Dompdf(); $dompdf->set_option("isPhpEnabled", true); $html_content = " <html> <head> <style> @font-face { font-family: kindergarten; font-weight:

我检查了与这个问题有关的每一个答案,但都不起作用

我想在页脚添加页码,我已经尝试了下面的代码,但没有成功。谁能告诉我哪里出了问题

$dompdf = new Dompdf();
$dompdf->set_option("isPhpEnabled", true);
$html_content = "
<html>
<head>
<style>
@font-face {
  font-family: kindergarten;
  font-weight: normal;
  font-style: normal;
  src: url('fonts/kindergarten.ttf') format('truetype');
}
.test{  font-family: kindergarten;  }
</style>

</head>
<body>
<script type='text/php'>
if ( isset($pdf) ) { 
    $pdf->page_script('

            $font = $fontMetrics->get_font('Arial, Helvetica, sans-serif, 'normal');
            $size = 12;
            $pageText = 'Page 1';
            $y = 15;
            $x = 520;
            $pdf->text($x, $y, $pageText, $font, $size);

    ');
}
</script>
<div>My Content goes here</div>
</body>
</html>
"; 
//echo $html_content; die;
$dompdf->loadHtml($html_content);
$dompdf->render();
$dompdf=newdompdf();
$dompdf->set_选项(“isPhpEnabled”,true);
$html_内容=”
@字体{
字体家庭:幼儿园;
字体大小:正常;
字体风格:普通;
src:url('fonts/幼稚园.ttf')格式('truetype');
}
.test{字体系列:幼儿园;}
如果(isset($pdf)){
$pdf->page_脚本($pdf)
$font=$fontMetrics->get_font('Arial,Helvetica,sans serif,'normal');
$size=12;
$pageText='Page1';
$y=15;
$x=520;
$pdf->text($x,$y,$pageText,$font,$size);
');
}
我的内容在这里
"; 
//echo$html_内容;死亡
$dompdf->loadHtml($html\u内容);
$dompdf->render();

使用公共方法
page\u text()

我的例子():


版本:DomPDF(v0.8.3

谢谢您的回答。我有v0.8.3,我在如何访问FontMetrics类方面遇到了问题。如果这有助于回答您的问题,请选择此作为答案。谢谢,您的建议对我非常有用。干杯此版本0.8.3中不推荐使用“get_font”方法。可能应该使用“getFont”版本。我在这个版本的dompdf的任何地方都找不到“get_text”方法,VSCode给出了一个错误。我使用了一个简单的解决方案,包括CSS、页眉、页脚和main。提供页码(但不提供计数)。在
// Documentation

 * @param float  $x
 * @param float  $y
 * @param string $text       the text to write
 * @param string $font       the font file to use
 * @param float  $size       the font size, in points
 * @param array  $color
 * @param float  $word_space word spacing adjustment
 * @param float  $char_space char spacing adjustment
 * @param float  $angle      angle to write the text at, measured CW starting from the x-axis
$html   = 'Text to test...';
$dompdf = new Dompdf();

$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();

// Parameters
$x          = 505;
$y          = 790;
$text       = "{PAGE_NUM} of {PAGE_COUNT}";     
$font       = $dompdf->getFontMetrics()->get_font('Helvetica', 'normal');   
$size       = 10;    
$color      = array(0,0,0);
$word_space = 0.0;
$char_space = 0.0;
$angle      = 0.0;

$dompdf->getCanvas()->page_text(
  $x, $y, $text, $font, $size, $color, $word_space, $char_space, $angle
);

// stream
$dompdf->stream('pdf_out.pdf', array('Attachment' => false));