php - DOMPDF如何添加页码

标签 php dompdf

我已经检查了与这个问题相关的每个答案,但它没有奏效。

我想在页脚中添加页码,我已经尝试过下面的代码,它没有奏效。谁能告诉我哪里出错了?

$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();

最佳答案

使用公共(public)方法page_text()

// 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

我的例子(https://prnt.sc/mifmxl):
$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));

版本:DomPDF ( v0.8.3 )

关于php - DOMPDF如何添加页码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52818205/

相关文章:

php - 在 jQuery 自动刷新 div 中使用 jQuery 工具提示

php - PHP 中的 MySQLdump,我需要它转储到特定目录中

php - DOMpdf:无法将两个 div 并排放置

php - dompdf/PHP 没有流式传输 PDF,只是在页面中显示代码

php - DomPDF:图像不可读或为空

php - 使用一个已知变量对 3 个表进行内连接

php - 如何从日期字段中从 MySQL 中减去 12 小时 30 分钟

php - 如何同时生成使用 DOMPDF 生成的 PDF 并通过电子邮件发送?

html - Laravel:DomPDF 孟加拉语字母显示为正方形

javascript - 使用 php codeigniter 将计算出的 javascript 变量值传递到文本字段