MPDF Wordpress 拉入变量

问题描述 投票:0回答:2

我正在使用 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,但所有动态内容都是空白的。我做错了什么?

php wordpress advanced-custom-fields mpdf
2个回答
1
投票

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

改变

function view_conferinta() {

$output = '<html>

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

$output = '<html>

然后将全局 post 添加到您的 init 函数中:

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();

  }
}


0
投票
   jQuery.ajax({
                    url: site_params.ajaxurl,
                    type: "POST",
                    data: total_box_data,
                    dataType: 'json',
                    success: function (response) {
                        if (response.status == 'failure') {
                            // show message as per design
                            alert(response.error_message);
                            //  jQuery('.digicore_form_registration-result').html('<div class="alert alert-danger">' + response.error_message + '</div>');
                            //  jQuery('#signup').val('Register');
                        } else if (response.status == 'success') {
                            var myBase64 = response.filenamepdf;
                            var blob;
                            blob = converBase64toBlob(myBase64, 'application/pdf');
                            var blobURL = URL.createObjectURL(blob);


                             if (!window.ActiveXObject) {
                                 var save = document.createElement('a');
                                 save.href = blobURL;
                                 save.download = 'test.pdf' || 'unknown';
                                 save.style = 'display:none;opacity:0;color:transparent;';
                                 (document.body || document.documentElement).appendChild(save);

                                 if (typeof save.click === 'function') {
                                     save.click();
                                 } else {
                                     save.target = '_blank';
                                     var event = document.createEvent('Event');
                                     event.initEvent('click', true, true);
                                     save.dispatchEvent(event);
                                 }
                                 (window.URL || window.webkitURL).revokeObjectURL(save.href);
                             } else if (!!window.ActiveXObject && document.execCommand) { // for IE
                                 var _window = window.open(blobURL, '_blank');
                                 _window.document.close();
                                 _window.document.execCommand('SaveAs', true, 'test.pdf' || blobURL)
                                 _window.close();
                             }
                            //  jQuery('.digicore_form_registration-result').html('<div class="alert alert-success alert-dismissible">' + response.success_msg + '</div>'); 
                            //  jQuery('#signup').val('Register');
                        } else {
                            // show error or success message as per design not in alert
                            alert('pdf is not generate');
                        }
                    }
                });

          var byteCharacters = atob(b64Data);
          var byteArrays = [];

          for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
            var slice = byteCharacters.slice(offset, offset + sliceSize);

            var byteNumbers = new Array(slice.length);
            for (var i = 0; i < slice.length; i++) {
              byteNumbers[i] = slice.charCodeAt(i);
            }

            var byteArray = new Uint8Array(byteNumbers);

            byteArrays.push(byteArray);
          }

          var blob = new Blob(byteArrays, {type: contentType});
          return blob;
        }


        var contentType = 'image/png';
        var b64Data = 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';

        var blob = b64toBlob(b64Data, contentType);
        var blobUrl = URL.createObjectURL(blob);

        var img = document.createElement('img');
        img.src = blobUrl;
        document.body.appendChild(img);

    <!-- end snippet -->


<?php
$mpdf = new mPDF();
    //echo $html;
    $mpdf->WriteHTML($html);

    $mpdf->useOnlyCoreFonts = true;

    $mpdf->SetDisplayMode('fullpage');

    //$mpdf->Output($file_name_url, 'I');


    $resultArr['status'] = 'success';
    $resultArr['success_msg'] = __('PDF Generated.');
    $resultArr['filepath'] = $newurl;
    $resultArr['filenamepdf'] = base64_encode($mpdf->Output('test.pdf', 'S'));
© www.soinside.com 2019 - 2024. All rights reserved.