php - Wordpress & MDPF 包含外部 php 文件模板
问题描述
我第一次使用 mpdf 并将其与 Wordpress 集成。我目前在写出html的functions.php文件中设置了它,但我想使用一个单独的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;
}
解决方案
这很奇怪......但有可能......出于安全目的,您需要将模板文件放入主题中。然后,您需要获取该文件内容并将其用作 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;
推荐阅读
- python - Pandas 过滤后给了我一个空的数据框
- python - 如何减少通过 Python 中的泡菜转储的图像对象大小
- javascript - 如何在nuxt中正确使用注入功能
- fail2ban - fail2ban 与日期模式不匹配
- c# - 发布请求获取“无法连接到远程服务器”错误
- java - 如何在同一个 Java Stream 上同时调用 takeWhile 和 dropWhile
- python - Python Strip 不删除字符
- tensorflow - 使用多个 GPU 的 TensorFlow 训练速度
- python - “Python 3.9”和“Python 3.9 (venv)”解释器有什么区别?
- php - preg_match_all 用于特殊字符 [?]