yii html2pdf保持css格式化

时间:2013-06-05 10:02:46

标签: php yii html-table wkhtmltopdf html2pdf

我正在尝试使用html2pdf创建PDF文件。我遵循了这个guide。我可以在html中创建它,并且所有内容都在可打印区域,但创建的PDF不会显示相同的结果。表格宽度不保留。数据在右侧页面末尾被截断。

在控制器中

$html2pdf = Yii::app()->ePdf->HTML2PDF('L',"A4","en", array(10, 10, 10, 10));
        $html2pdf->WriteHTML($print);
        $html2pdf->Output();

要创建的页面

<style type="text/css">

    @media print{thead{display:table-header-group; margin-bottom:2px;}}
    @page{margin-top:1cm;margin-left:1cm;margin-right:1cm;margin-bottom:1.5cm;}}

.odd {
    background: none repeat scroll 0 0 #E5F1F4;
}
.even {
    background: none repeat scroll 0 0 #F8F8F8;
}
table tbody tr:hover{
        background: none repeat scroll 0 0 lightgreen;
}
table tbody tr{
    font-size:10pt;
}
body
{
  margin: 0mm 0mm 0mm 0mm;
}

.grey{
    background: lightgrey;
}

.center{
    margin: 0 auto;
    text-align: center;
}

.size{
    font-size:15px;
}

table{
    padding-botton:30px;
}

</style>

<body onload="window.print();window.close();">
<h1 class="grey center">Project Report</h1>
<?php 
$count = 0;
$class= null;
foreach($results as $key=>$data) {
?>
<table width=0>
<tr class="grey size">
    <th colspan=12><?=$key ?></th>
</tr>
<thead>
    <tr>
        <th>Start Date</th>
        <th>Name</th>
        <th>Code</th>
        <th>Actual End Date</th>
        <th>Office</th>
        <th>%</th>
        <th>% Planned</th>
        <th>KM</th>
        <th>KM Planned</th>
        <th>Country</th>
        <th width="150">AREA</th>
        <th>PROJ INFO</th>
    </tr>
</thead>
<tbody>
<?php foreach($data as $column=>$q) {   ?>

    <?php 
        $class = ($count % 2 === 0) ? 'odd' : 'even';
        $this->renderPartial('_report',array('data'=>$q,'class'=>$class));
        $count++;
    }
    ?>
</tbody>
</table>

<?php }
?>

</body>

_report.php

<tr class=<?=$class?>>
    <td><?php //$startdate = 
    echo CHtml::encode($data['StartDATE']);
            /*$createstartdate = new DateTime($startdate);
            $strip = $createstartdate->format(Yii::app()->params['companies'][Yii::app()->params['currentLocation']]['dateFormat']);
            echo($strip);
            $strip = null;*/
        ?></td>
    <td><?php echo CHtml::encode($data['PROJECT']);?></td>
    <td><?php echo CHtml::encode($data['PROJCODE']); echo CHtml::encode($data['PROJID']);?></td>
    <td><?php //$enddate = 
    echo CHtml::encode($data['ActualEndDate']);
        /*$createenddate = new DateTime($enddate);
        $strip = $createenddate->format(Yii::app()->params['companies'][Yii::app()->params['currentLocation']]['dateFormat']);
        echo($strip);
        $strip = null;*/        
        ?></td>
    <td><?php echo CHtml::encode($data['OFFICE']);?></td>
    <td><?php echo CHtml::encode($data['PERCENT']);?></td>
    <td><?php echo CHtml::encode($data['PERCENTPlanned']);?></td>
    <td><?php echo CHtml::encode($data['KM']);?></td>
    <td><?php echo CHtml::encode($data['KMPlanned']);?></td>
    <td><?php echo CHtml::encode($data['COUNTRY']);?></td>
    <td><?php echo CHtml::encode($data['AREA']);?></td>
    <td><?php echo CHtml::encode($data['PROJINFO']);?></td> 
</tr>

使用pdfable我收到以下错误:

Stack trace:
#0 /opt/yii-1.1.12.b600af/framework/web/CController.php(783): CController->renderPartial('<style type="te...', Array, true)
#1 /opt/paradox/protected/extensions/pdfable/PdfFile.php(131): CController->render('<style type="te...', Array, true)
#2 /opt/paradox/protected/extensions/pdfable/PdfFile.php(86): PdfFile->render('<style type="te...', Array)
#3 /opt/paradox/protected/extensions/pdfable/Pdfable.php(205): PdfFile->renderPage('<style type="te...', Array, Array)
#4 [internal function]: Pdfable->renderPdf('<style type="te...')
#5 /opt/yii-1.1.12.b600af/framework/base/CComponent.php(261): call_user_func_array(Array, Array)
#6 [internal function]: CComponent->__call('renderPdf', Array)
#7 /opt/paradox/protected/controllers/ViewWebprojectreportController.php(297): ViewWebprojectreportController->renderPdf('<style type="te...')
#8 /opt/yii-1.1.12.b600af/framework/web/actions/CInlineAction.php(50): ViewWebprojectreportController->actionPrint()
#9 /opt/yii-1.1.12.b600af/framework/web/CController.php(309): CInlineAction->runWithParams(Array)
#10 /opt/yii-1.1.12.b600af/framework/web/filters/CFilterChain.php(134): CController->runAction(Object(CInlineAction))
#11 /opt/yii-1.1.12.b600af/framework/web/CController.php(292): CFilterChain->run()
#12 /opt/yii-1.1.12.b600af/framework/web/CController.php(266): CController->runActionWithFilters(Object(CInlineAction), Array)
#13 /opt/yii-1.1.12.b600af/framework/web/CWebApplication.php(283): CController->run('print')
#14 /opt/yii-1.1.12.b600af/framework/web/CWebApplication.php(142): CWebApplication->runController('ViewWebprojectr...')
#15 /opt/yii-1.1.12.b600af/framework/base/CApplication.php(162): CWebApplication->processRequest()
#16 /opt/paradox/index.php(13): CApplication->run()
#17 {main}
REQUEST_URI=/paradox/index.php?r=ViewWebprojectreport/print
HTTP_REFERER=http://portal-test/paradox/index.php?r=ViewWebprojectreport/admin

更新为$this->renderPdf('print',array('results'=>$results));后,我收到新错误

2013/06/05 12:11:33 [error] [ext.pdfable.Pdfable] Could not create PDF for view print
in /opt/paradox/protected/extensions/pdfable/Pdfable.php (209)
in /opt/paradox/protected/controllers/ViewWebprojectreportController.php (297)
in /opt/paradox/index.php (13)

来自mpdf

  

PHP致命错误:在10125行的/opt/paradox/protected/vendors/MPDF56/mpdf.php中,允许的内存大小为268435456字节(试图分配258180字节),参考资料:http:// portal-test /悖论/的index.php?R = ViewWebprojectreport /管理

我将样式粘贴到变量$stylesheet

1 个答案:

答案 0 :(得分:4)

html2pdf是完美的扩展,我以完美的方式开展工作。

要解决您的问题,您必须在外部文件中添加您的CSS。

敌人的例子:

$mpdf=Yii::app()->ePdf->mpdf('utf-8', 'Letter-L');
$mpdf->ignore_invalid_utf8 = true;
$stylesheet = file_get_contents('certificate.css'); /// here call you external css file 

$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($content);/// here your content



$mpdf->Output("myfile.pdf", 'D');


exit; 

这个例子来自我的项目,它与所有css取件人完美配合:

   $html= '   
<body>

<div class="wi">
        <p align="center" dir="LTR">
        </p>
        <p align="center" dir="LTR">
        <img src="illaf.jpg" width="10.23cm" height="2.36cm" /></p>
        <p align="center" dir="LTR">

            <div class="herby">
            Hereby certifies that
            </div>

            </p>
        <p align="center" dir="LTR">
            </p>
        <p align="center" >
            <div class='.$style.'>
           '.$name.'  '.$father.'  '.$last_name.'
            </div>
            </p>
        <p align="center" dir="LTR">
            Has completed training program which was conducted according to the standards and guidelines<br/> established by XXXXX<sup>&reg;</sup>. The holder of this certificate has been trained by a registered XXXXX<sup>&reg;</sup><br /> Trainer and has achieved the standards required to merit this certificate.</p>
        <p align="center" dir="LTR">
            </p>
        <p align="center" dir="LTR">
            ILLAFTrain<sup>&reg;</sup> has therefore conferred upon the holder this certificate in</p>
        <p align="center" dir="LTR">
            <div class="trainer_type">
        '.$course_name.'
            </div>
            </p>
            </br>
               </br>
                  </br>
            <div calss="marginbutton">
        <table  align="center" border="0" cellpadding="0" cellspacing="0" dir="ltr">
            <tbody>
                <tr>
                    <td style="width:643px;">
                        <p dir="LTR">
                        </p>
                        <p dir="LTR">
                            </p>
                        <p dir="LTR">

                    </td>
                    <td style="width:267px;">
                        <p dir="LTR">
                            </p>
                    </td>
                </tr>
                <tr>

                        <p dir="LTR">
                            <td>

                            The following have officially decreed their signatures<br/>
                            hereon on this '.$day.'<sup>th</sup>day of '.$month.' in the year '.$year.'</p>
            </td>


                </tr>
                <tr>
                    <td style="width:643px;">
                        <p dir="LTR">
                            </p>
                    </td>
                    <td style="width:267px;">
                        <p align="center" dir="LTR" style="margin-left:2.25pt;">
                            </p>
                    </td>
                </tr>
                <tr>
                    <td >
                        <p dir="LTR">
                            </p>
                    </td>
                    <td>


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



                    </td>
                    <td >


                    </td>
                </tr>
            </tbody>
        </table>
        </br>
        </br>
        </br>

         <table>
<tr>
    <td>                   <div class="pdraa" >
                                 Trainer:
    '.$trainer.' ____________  

          </div></td>
    <td>
               <div class="pdraa" >

    '.$trainers[0].' 

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

<tr>
    <td>
               <div class="pdraa" >

    '.$trainers[1].' 

          </div>

    </td>
    <td>
               <div class="pdraa" >

        '.$trainers[2].' 
          </div>

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

    </br>
    </br>
    </br>
         <div class="number">

                            <strong>'.$cirt_id.'</strong>

                             </div> 

        <div class="site">
                                    www.<strong>XXXX</strong>.co.uk
                                     </div> 
        <div style="clear:both;">
            </div>


            </div>
            </body>

    ';
$mpdf=Yii::app()->ePdf->mpdf('utf-8', 'Letter-L');
$mpdf->ignore_invalid_utf8 = true;
$stylesheet = file_get_contents('certificate.css'); /// here call you external css file 

$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html);



$mpdf->Output("$name$user_id$last_name.pdf", 'D');


exit;