Wordpress&;MDPF包含外部php文件模板

Wordpress&;MDPF包含外部php文件模板,php,wordpress,templates,mpdf,Php,Wordpress,Templates,Mpdf,我第一次使用mpdf,并将其与Wordpress集成。我目前在functions.php文件中设置了它,编写html,但我想使用一个单独的php文件作为pdf的模板,这可能吗?我在mpdf文档中找不到任何东西 以下是我目前的代码: add_action('init', 'congres_redirect'); function congres_redirect() { if(isset($_GET['offer'])) { $restName = get_field('restau

我第一次使用mpdf,并将其与Wordpress集成。我目前在functions.php文件中设置了它,编写html,但我想使用一个单独的php文件作为pdf的模板,这可能吗?我在mpdf文档中找不到任何东西

以下是我目前的代码:

add_action('init', 'congres_redirect');

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

  }
}
function view_conferinta() {
$post_id = false; // current post
$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>
body {
font-family: open sans, san-serif;
}
.voucher {
padding: 20px;
}

</style>
<body>';

 $output .= ' 

 <div class="voucher-content">
    <div class="inner-voucher">
        <h1>Voucher Title</h1>
        <p>Voucher Description</p>
        <p>Voucher Code: EL-200DEG07022020-123AB</p>
    </div>
 </div>


 ';


//$output .='<div class="voucher">Restaurant Name: '.$restName.'</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;
}
add_action('init','congres_redirect');
函数congres_redirect(){
如果(isset($_GET['offer'])){
$restName=get_字段('restaurant_name',$post->ID);
视图_conferinta();
}
}
功能视图_conferinta(){
$post\u id=false;//当前帖子
$output=
凭证下载
身体{
字体系列:开放式sans、sanserif;
}
.凭单{
填充:20px;
}
';
$output.='
凭证标题
凭证说明

凭证代码:EL-200DEG072020-123AB

'; //$output.='restance Name:'.$restName.'; $output.=' '; 需要一次/mpdf/vendor/autoload.php'; $mpdf=new\mpdf\mpdf(['debug'=>true]); $mpdf->WriteHTML($output); $mpdf->Output('my-voucher.pdf','I'); //$mpdf->Output($pdfheader.'.pdf',D'); 出口 }
这有点奇怪。。。 但这是可能的。为了安全起见,您需要将模板文件放入主题中。 然后,您需要获取该文件的内容,并将其用作pdf的输出

// get contents of a file into a string
$filename = include get_stylesheet_directory() . '/template.php';
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
require_once __DIR__ . '/mpdf/vendor/autoload.php';

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

如果库没有明确提供这一选项,那么使用您已有的,
$mpdf->WriteHTML($output)-并找到一种方法,首先将模板文件的内容(或呈现的输出,如果需要是动态的)加载到变量中。如果模板本身内部有编码,仅读取文件内容是不够的。(看到显示的代码中有
$output.='Restaurant Name:'.$restName.'
,虽然现在已经注释掉了,但我认为可以安全地假设它最终会包含这样的内容。)@04FS我将我的HTML内容与您的标记放在一起,它看起来很好。是的,对于静态内容,它会起作用。但很可能他们不希望这里有一个完全静态的模板,而是动态地将数据插入其中。亲爱的,我看到了静态内容,这就是为什么我要写这段代码…如果内容是动态的,那么代码就不同了。如果我想包含动态内容,我该怎么做,我目前有它设置为静态,而我测试,但我想在以后添加动态内容。谢谢