I am using MPDF with Wordpress and ACF to create vouchers. I want to click a button on my custom post type to generate the pdf (this works). I then want to pull variables into the PDF to fill it with dynamic content from my custom post type and advanced custom fields, it doesn't show errors the values are just blank. This is my code so far:
Button on post:
<a href="<?php get_bloginfo('url'); ?>?offer=<?php echo $post->ID ?>" download>Download The Voucher</a>
Functions.php code to generate the 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;
}
This generates a pdf but all the dynamic content is blank. What am I doing wrong?
You're not passing the dynamic variables into the function creating the output. You also need to declare the $post global
Change
function view_conferinta() {
$output = '<html>
to
function view_conferinta(restName, $image) {
global $post;
$output = '<html>
Then add global post to your init function:
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();
}
}