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条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)