doushai7225 2017-11-12 07:03
浏览 22
已采纳

如何在mPDF中的$ html中传递if语句?

How can I pass the if statement in the $html? I'm building pdf from the values of a form and I'm not sure how I can pass this if statement with the html.

I'm using mPDF version 6 to generate pdf.

I've researched for a while but didn't couldn't figure out the right way. So, I really appreciate your suggestions. Thanks in advance.

<?php
   include('mPDF/mpdf.php');

   $name = $_POST["name"];
   $age = $_POST["age"];

   $html = '<html>
               {if (!empty($_POST['name'])) echo 'Name: ' . $name;}<br>
               {if (!empty($_POST['age'])) echo 'Age: ' . $age;}
            </html>';
   $mpdf = new mPDF();
   $mpdf->WriteHTML($html);
   $mpdf->Output();
   ?>

This is the form in case you need a look:

<form method="post" action="generatePDF.php">   
   Name: <input type="text" name="name" id="name"  /><br>
   Age: <input type="text" name="age" id="age"  /><br>
</form>
  • 写回答

3条回答 默认 最新

  • duanseci1039 2017-11-12 07:10
    关注

    I will rather change this to below, so that I don't' have check for conditions in HTML. Perform all conditions beforehand and have the variables ready which can be used in HTML, to generate the PDF:

    <?php
       include('mPDF/mpdf.php');
    
       $name = !empty($_POST["name"]) ? 'Name: ' . $_POST['name'] : '';
       $age = !empty($_POST["age"]) ? 'Age: ' . $_POST["age"] : '';
    
       $html = '<html>' . 
                   $name . '<br>' .
                   $age . 
                '</html>';
       $mpdf = new mPDF();
       $mpdf->WriteHTML($html);
       $mpdf->Output();
       ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向