MPDF Wordpress 引入变量

MPDF Wordpress pull in variables

我将 MPDF 与 Wordpress 和 ACF 结合使用来创建凭证。我想单击自定义 post 类型上的一个按钮来生成 pdf(这有效)。然后我想将变量拉入 PDF 以用我的自定义 post 类型和高级自定义字段中的动态内容填充它,它不显示错误值只是空白。到目前为止,这是我的代码:

post 上的按钮:

<a href="<?php get_bloginfo('url'); ?>?offer=<?php echo $post->ID ?>" download>Download The Voucher</a>

Functions.php 生成 pdf 的代码:

add_action('init', 'congres_redirect');

function congres_redirect() {
  if(isset($_GET['offer'])) {
    $restName = get_field('restaurant_name', $post->ID);
    $image = get_field('restaurant_image', $post->ID);
      view_conferinta();

  }
}

function view_conferinta() {

$output = '<html>
<head><title>Voucher Download</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head>
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Open+Sans" />
<style>
.voucher-content {
padding: 20px;
}
.inner-voucher {
padding: 20px;
border: solid 1px #000;
}

</style>
<body>';

 $output .= ' 

 <div class="voucher-content" style="font-family:chelvetica">
    <div class="inner-voucher">
       <img src=".$image." alt="Logo" style=" margin: 0;" />
       <h1>Voucher Title</h1>
        <p>Voucher Description</p>
        <p>Voucher Code: EL-200DEG07022020-123AB</p>

        <p>Restaurant Name:'.$restName.'</p>
        <p>This is my static voucher template</p>
        <p>POST ID:'.$post->ID.'</p>
    </div>
 </div>

 ';

$output .= ' 
</body>
</html>';


require_once __DIR__ . '/mpdf/vendor/autoload.php';


$mpdf = new \Mpdf\Mpdf(['debug' => true]);
$mpdf->WriteHTML($output);
$mpdf->Output('my-voucher.pdf','I');
//$mpdf->Output($pdfheader.'.pdf', 'D');
exit;
}

这会生成一个 pdf,但所有动态内容都是空白的。我做错了什么?

您没有将动态变量传递给创建输出的函数。您还需要声明 $post global

改变

function view_conferinta() {

$output = '<html>

function view_conferinta(restName, $image) {
global $post;

$output = '<html>

然后将全局 post 添加到您的初始化函数中:

function congres_redirect() {

  if(isset($_GET['offer'])) {
    global $post; //ADD THIS
    $restName = get_field('restaurant_name', $post->ID);
    $image = get_field('restaurant_image', $post->ID);
      view_conferinta();

  }
}