简体   繁体   中英

Wordpress & MDPF include external php file template

I am using mpdf for the first time and have integrated it with Wordpress. I have it currently set up in the functions.php file writing out the html but i would like to use a seperate php file as the template for the pdf, is this possible? I can't find anything in the mpdf documentation.

Here is my code so far:

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;
}

That is something weird... But it is possible..you need to put your template file into theme for security purpose. Then you need to get that file contents and use it as out put of your 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM