dompdf-单页/ page_script上的文本不起作用 [英] dompdf - text on a single page / page_script doesnt work
问题描述
我正在使用dompdf 0.7.0,并尝试在渲染后用PHP在PHP上写下文本。我需要在特定页面上输入文字,并且从Brian
的Answer中找到以下内容: DomPDF {PAGE_NUM}不在首页上
I'm using dompdf 0.7.0 and try to write down text in PHP on my pdf after rendering. I need text on specific pages and found the following from Answer from Brian DomPDF {PAGE_NUM} not on first page
page_script函数听起来像是正确的答案。
我可以检查循环当前是否在第3页上。
The function page_script sound like the correct answer. I could check if loop is currently on page 3 or whatever.
是否需要为此功能启用任何选项?
Does I have to enable any options for this function?
示例:
$dompdf = new Dompdf( $options );
$dompdf->set_option('default_font', 'open sans');
$dompdf->set_option('fontHeightRatio', 1);
$dompdf->setPaper('A4')
$dompdf->set_option('enable_html5_parser', true);
$dompdf->set_option('enable_php', true);
$dompdf->loadHtml( $html );
$dompdf->render();
$canvas = $dompdf->get_canvas();
$canvas->page_script('
if ($PAGE_NUM > 1) {
$current_page = $PAGE_NUM-1;
$total_pages = $PAGE_COUNT-1;
$canvas->text(0, 0, "$current_page / $total_pages", "open sans condensed", 10, array(0,0,0));
}
');
它仍显示在我的第一页上。
It still be shown on my first page.
推荐答案
$ canvas
不在页面脚本中,因为它超出了范围。可以在页面脚本内将canvas对象引用为 $ pdf
。
$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
.
请尝试以下 page_script
调用:
$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
:当前页码 -
$ PAGE_COUNT
:总页数 -
$ pdf
:画布对象 -
$ fontMetrics
:FontMetrics类的实例
$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}
:当前页数 -
{PAGE_COUNT}
:总页数
{PAGE_NUM}
: current page number{PAGE_COUNT}
: total number of pages
注意:默认情况下,不支持解析嵌入式脚本或页面脚本。使用以下命令启用它: $ dompdf-> set_option( isPhpEnabled,true);
。必须在调用 $ dompdf-&r; render();
之前完成。
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();
.
这篇关于dompdf-单页/ page_script上的文本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!