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

如何在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条)

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,