如何将Yii视图页面转换为PDF

时间:2014-02-19 17:04:00

标签: php pdf yii

我使用Yii框架创建了一个项目,现在我想将视图页面转换为PDF。请帮我找一个扩展来做到这一点。

我进行了搜索并找到了示例,但这些不是Yii扩展,我不知道如何实现它们。请给我一些关于如何将视图页面数据转换为PDF的教程。

查看页面代码:

 <div class="row">    

  <div class="row-column1">
          <?php echo CHtml::label('Reg. Number. :','student_enroll_no'); ?>
      <?php echo $info->student_enroll_no;?> 
  </div>

  <div class="row-column2">
          <?php echo CHtml::label('Adm. Date :','student_adm_date'); ?>
      <?php if($info->student_adm_date != NULL)
          echo date('d-m-Y',strtotime($info->student_adm_date));?>
  </div>

</div>

<div class="row">
  <div class="row-column1">
      <?php echo CHtml::label('Student Name :','student_first_name'); ?>
      <?php echo $info->student_first_name;?>
  </div>

  <div class="row-column2">
  <?php echo CHtml::label('Enroll No. :','student_mobile_no'); ?>   
      <?php echo $info->student_mobile_no;?>


  </div>


</div>


<div class="row">
  <div class="row-column1">
   <?php echo CHtml::label('Fathers Name :','student_last_name'); ?>
      <?php echo $info->student_last_name;?>
      <!--<?php echo CHtml::label('Gender :','student_gender'); ?>
      <?php echo $info->student_gender;?>-->
  </div>

  <div class="row-column3">
   <?php echo CHtml::label('Mothers Name :','student_mother_name'); ?>
      <?php echo $info->student_mother_name;?>
       <!-- <?php echo CHtml::label('Date of Birth :','student_dob'); ?>
  <?php   if($info->student_dob != NULL)
      echo date('d-m-Y',strtotime($info->student_dob));?>-->
  </div>
</div>

<div class="row">
  <div class="row-column1">
  <?php echo CHtml::label('Gender :','student_gender'); ?>
      <?php echo $info->student_gender;?>
  <!--<?php echo CHtml::label('Course :','student_transaction_course_id'); ?>
      <?php
          echo !empty($model->student_transaction_course_id) ? $model->relCourse->course_name : 'N/A';

      ?>-->


  </div>

  <div class="row-column2">
   <?php echo CHtml::label('Date of Birth :','student_dob'); ?>
  <?php   if($info->student_dob != NULL)
      echo date('d-m-Y',strtotime($info->student_dob));?>
      <!--<?php echo CHtml::label('Year :','student_academic_term_period_tran_id'); ?>
       <?php echo $model->Rel_student_academic_terms_period_name->academic_term_period;?>-->
  </div>  
</div>
<div class="row">
  <div class="row-column1">
<?php echo CHtml::label('Category :','title'); ?>
      <?php echo $info->title;?>


  </div>

  <div class="row-column2">
      <?php echo CHtml::label('Email ID :','student_email_id_1'); ?>                          <?php echo $info->student_email_id_1; ?>
  </div>  
</div>



<div class="row">
  <div class="row-left">
  <?php echo CHtml::label('Course :','student_transaction_course_id'); ?>
      <?php
          echo !empty($model->student_transaction_course_id) ? $model->relCourse->course_name : 'N/A';

      ?>

       <!-- <?php echo CHtml::label('Blood Group :','student_living_status'); ?>
        <?php echo $info->student_living_status; ?>-->
  </div>

  <div class="row-column3">

       <!-- <?php echo CHtml::label('Nationality :','student_transaction_nationality_id'); ?>
        <?php if($model->student_transaction_nationality_id!=null)
      echo $model->Rel_Nationality->nationality_name; 
  ?>-->
  <?php echo CHtml::label('Year :','student_academic_term_period_tran_id'); ?>
       <?php echo $model->Rel_student_academic_terms_period_name->academic_term_period;?>
  </div>

</div>

<div class="row">
  <div class="row-left">
        <?php echo CHtml::label('Subjects :','languages_known1'); ?>
        <?php 
      $knwLang = "";
      if($lang->languages_known1)
      $knwLang =  $lang->Rel_Langs1->languages_name; 
      if($lang->languages_known2)
      $knwLang .= ", ".$lang->Rel_Langs2->languages_name;
      if($lang->languages_known3)
      $knwLang .= ", ".$lang->Rel_Langs3->languages_name;
      echo $knwLang;
  ?>

  </div>
</div>

  <div class="row last">

      <?php echo ('_____________________Note -Click on edit and update DoB , Subjects and upload photo ,Enroll no. .'); ?>



</div>

2 个答案:

答案 0 :(得分:4)

在这里,您将获得扩展示例。

http://www.yiiframework.com/extension/pdf/

我正在使用: http://www.mpdf1.com/

您可以下载并创建扩展程序

下载此软件包:

http://www.yiiframework.com/extension/pdf/files/yii-pdf-0.3.2.zip

参考此论坛的用法: http://www.yiiframework.com/forum/index.php/topic/44211-mpdf-header/

答案 1 :(得分:3)

您可以按照以下给出的点使用yii扩展名的htmlpdf。 1.从pdf extension提取zip文件下载扩展名yii-pdf-0.3.2.zip并将你的yii项目放在protected / extension下

2.从这里下载html2pdf zip html2pdf解压zip文件并将其放入protected / extension

  1. 转到protected / config / main.php 转到组件数组并放入此       'ePdf' => array( 'class' => 'ext.yii-pdf.EYiiPdf', 'params' => array( 'HTML2PDF' => array( 'librarySourcePath' => 'ext.html2pdf.*', 'classFile' => 'html2pdf.class.php' ), ), ),

  2. 如果你得到TCPDF ERROR,请转到extension / html2pdf / html2pdf.class.php查找输出函数在返回语句ob_end_clean();

  3. 之前写一行