【发布时间】:2020-02-11 22:44:08
【问题描述】:
我正在将 MPDF 与 Wordpress 和 ACF 一起使用来创建凭证。我想单击自定义帖子类型上的按钮来生成 pdf(这有效)。然后,我想将变量拉入 PDF 以填充来自我的自定义帖子类型和高级自定义字段的动态内容,它不会显示错误值只是空白。到目前为止,这是我的代码:
帖子上的按钮:
<a href="<?php get_bloginfo('url'); ?>?offer=<?php echo $post->ID ?>" download>Download The Voucher</a>
生成pdf的Functions.php代码:
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,但所有动态内容都是空白的。我做错了什么?
【问题讨论】:
-
看来您需要将
$restName和$image变量传递给您的view_conferinta()函数 -
@benJ 也不返回值。如果我将它移到函数 view_conferinta() { 下仍然没有结果。
标签: php wordpress advanced-custom-fields mpdf