如何在pdf-> page_script()中使用php变量? [英] How to use php variable inside pdf ->page_script()?

查看:34
本文介绍了如何在pdf-> page_script()中使用php变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在$ pdf-> page_script()中使用PHP变量;
这是我的代码

How to use PHP variable inside $pdf->page_script(); Here is my code

<?php
<script type="text/php">
$pdf->page_script(\'
    if ($PAGE_NUM >= 2) {$pdf->image($var,"jpg",25,500,0,0);
    }
  \');

</script>

?>

我在$ var中遇到语法错误。

I am getting an error Syntax error in $var.

推荐答案

通过运行内联脚本 eval() 函数和在全局范围内声明的任何变量只能通过 $ GLOBALS 超全局变量进行访问( $ GLOBALS ['var'] )或通过使用 global 关键字对其进行初始化(

Inline script is run via the eval() function and any variables declared in the global scope are only accessible via the $GLOBALS superglobal ($GLOBALS['var']) or by initializing them with the global keyword (global $var).

我应该注意,如果您使用的是dompdf 0.6.x,则无需指定图像类型。您似乎也将参数向后移动。该方法先查找图像路径,x,y,宽度,然后是高度。

I should note that if you're using dompdf 0.6.x you no longer have to specify the image type. You also appear to have the parameters backwards. The method looks for image path, x, y, width, then height.

以下方法应该起作用:

<?php
<script type="text/php">
  $pdf->page_script('
    if ($PAGE_NUM >= 2) {
      $pdf->image(' . $GLOBALS['var'] . ',0,0,25,500);
    }
  ');
</script>
?>

次要注意:由于您正在创建页面脚本,因此我将图像路径变量与其余部分连接在一起脚本字符串。您可以将该变量引用作为脚本的一部分而不是串联在一起,但是每次运行脚本时,您都要对该变量进行其他查找。尽管我必须管理这是运行它的宏伟方案中的次要性能考虑。

Minor note: Since you're creating a page script I concatenated the image path variable with the rest of the script string. You can include that variable reference as part of the script rather than concatenate, but then you're performing additional look-ups on the variable every time the script is run. Though I must admin this is a minor performance consideration in the grand scheme of where this is running.

这篇关于如何在pdf-&gt; page_script()中使用php变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆