如何将php html页面保存为pdf

时间:2015-12-08 06:01:55

标签: php html pdf swiftmailer

我有一个控制器功能来打印一个门厅

  function print_hallticket() {

        if (!$this->library['authentication']->permission(3)) {
            $this->redirect('gateway/no-access');
        } 

         $this->model('registration');
         $data['id']=$this->url_slice(3,'');

         $data['student_details'] = $this->model['registration']->student_details($this->url_slice(3,''));


        $this->view("print/hallticket",$data);
    }

打印页面hall_ticket.php如下所示,当点击打印按钮时,它会打印出门票。

<html lang="en">
<html>
<head>
<base href="<?php echo $this->get_base_url(); ?>" />
<title>Print</title>
<head>
<meta charset="UTF-8">
<style type="text/css">
/*  tr.ths td{
    border-bottom:1pt solid black; 
    border-top:1pt solid black; 
     border-left:1pt solid black; 
      border-right:1pt solid black; 
}*/
                body{font-family:Arial,sans-serif; margin:0px; padding:0px;  } 
                .page{ width:795px; height: auto;  margin: 0px auto;  font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; color:#000000;}  
                @media print {@page { margin: 30px 45px 0 45px;  visibility: hidden;   } .page-break {page-break-before: always;} }  

            </style>

<body onLoad="javascript:window.print();" >
<div class="page" id="pageid">
    <div style="width:780px;height:auto;margin:auto; background:url() repeat-x top; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; ">

        <div style="width:750px; height:auto; margin:auto; padding:10px 0 0 0;  float:left;  height:65px;">
            <table>
                <tr>   
                    <td width="70%"><div style="width:30%; height:65px;  margin-left: 15px; float:left; font-size:12px;"><img src="assets/images/logo1.png" height="85px"></div></td>
                    <td width="70%"><div style="width:600px; margin-left: 50px; color:#0000; float:left; font-size:20px;  font-weight: bold;"><span style="margin-left: 30px;"><?php  foreach($student_details as $rows){ echo $rows->exam_name; } ?></span><br/><span style="font-size:12px;margin-left: 150px; ">ELIGIBILITY TEST</span> 

                </tr>

            </table>

        </div>


        <div style="width:750px; float:left; height:25px;  background:#ffff; -webkit-border-radius: 3px;    -moz-border-radius: 3px;border-radius: 3px; margin:20px auto 0 15px; padding:5px 0 0 7px;">
            <table  style="text-align:  center;"  >
                <tr>
                    <td colspan="2"><div style="width:175px; margin-left: 210px; color:#0000; float:left; font-size:14px; padding:0 0 0 55px; font-weight: bold;"><u>ADMIT CARD </u></div>  </td>


                </tr>

            </table>


        </div>
                        <div>
                        <?php foreach($student_details as $row){  ?>
        <div style="width:75%; float:left; height:35px;  background:#ffff; -webkit-border-radius: 3px;  -moz-border-radius: 3px;border-radius: 3px; margin:10px auto 20px 15px; padding:5px 0 0 7px; ">
            <table>
                <tr></tr>
                <!--<tr  style="width:100% ;font-size:16px;font-weight: bold;" class="ths">  <td >Roll No</td ><td >:</td ><td ><?php // echo EXAMCODE.$row->student_rollno; ?>  </td></tr>-->
                 <tr>

                 </tr>
            </table>

            <table  style="text-align:  left;font-size: 18px;width:600;"  >
                <tr></tr>

                 <tr>

                 </tr><tr  style=" font-size:17px;font-weight: bold;" class="ths">  <td >Roll No</td ><td >:</td ><td ><?php echo $row->exam_code.$row->student_rollno; ?>  </td></tr>

                <tr>

                    <td style="width:30%">Venue Of Test</td>
                    <td>:</td><td  ><?php echo $row->exam_center_name; ?> </td>

                </tr>
                  <tr>

                    <td >Name </td>
                       <td>:</td><td><?php echo  $row->student_name; ?>  </td>

                    <td ></td>


                </tr>
                 <tr>
                      <td >Father's Name</td>
                       <td>:</td><td><?php echo $row->student_parent; ?> </td>
                 </tr>
                  <tr>
                      <td valign="top">Address</td>
                       <td valign="top">:</td><td><?php echo $row->student_address; ?><br /><?php echo $row->student_district; ?><br /><?php echo $row->student_pin; ?> </td>
                 </tr>
                  <tr>
                      <td >Email ID</td>
                       <td>:</td><td><?php echo $row->student_email; ?>  </td>
                 </tr>
                  <tr>
                      <td >Date Of Test</td>
                       <td>:</td><td><?php echo $row->exam_date; ?>    </td>
                 </tr>
                  <tr>
                      <td >Time</td>
                       <td>:</td><td><?php echo date('h:i:s a ', strtotime($row->exam_starttime)); ?>  To <?php echo  date('h:i:s a ', strtotime($row->exam_endtime)) ; ?>    </td>
                 </tr>

            </table>


        </div>
                        <div style="width:25%; float:right;   ">

                            <table style="border: 2 ;text-align:  left;font-size: 20px;width:200;" >

                                 <tr>  <td>   </td></tr>
                            <tr>  <td>  <?php if ($row->student_image != "") { ?>
                        <img src="<?php echo $this->get_base_url(); ?>public/uploads/students/<?php echo $row->student_image ?>" height="150px"/>
                        <?php } else { ?>
                        <img src="assets/images/imagesper.png" />
                            <?php } ?> 
                        </div>
                         <?php } ?>
  </td></tr>
</table>  </div></div>

        <div style="clear:both;"></div>






</div>
<div style="width:750px; text-align: center;  height:auto; margin:auto; padding:20px 0 0 0; color:#202020; font-weight: bold; font-size:11px;">

</div>

<div style="width:550px; height:33px; margin:auto; padding:20px 0 0 0; color:#202020; font-size:12px; text-align:right;">

</div>

</div> 
    <div style="width:780px;height:auto;margin:auto; background:url() repeat-x top; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; ">
         <table style="border: 2 ;text-align:  left;font-size: 20px;width:200;" >

                               <tr>  </tr>   <tr>  </tr>   <tr>  </tr>   <tr>  <td>  

                                     </td></tr>


</table>  

    </div>
<div class="page-break"></div>


        </body>
</html>

现在,当我点击另一个按钮&#39;批准&#39;时,我需要通过邮件发送这个门票。现在它使用swift mail将用户名和密码发送到邮件。我可以使用swift附加pdf文件邮件。我的问题是如何将其保存为pdf? 请建议我。

谢谢。

2 个答案:

答案 0 :(得分:0)

从观点来看Tcpdf是从HTMl创建PDF的最佳开源,它支持阿拉伯语和RTL语言,你可以添加你想要的字体,你可以制作页眉和页脚,然后重复每一页,可以将HTML字符串传递给writeHTML函数输出你的pdf,也可以在你下载的完整源代码中找到简单的例子来简化你的任务。

答案 1 :(得分:0)

尝试http://wkhtmltopdf.org/

  

wkhtmltopdf和wkhtmltoimage是开源(LGPLv3)命令行   使用Qt将HTML呈现为PDF和各种图像格式的工具   WebKit渲染引擎。这些完全是“无头”而不是   需要显示或显示服务。

wkhtmltopdf http://google.com google.pdf