dtypj3308 2013-06-05 10:02
浏览 88

yii html2pdf保持css格式化

I am trying to create a PDF file using html2pdf. I have followed this guide. I can create it in html fine and everything is in printable area however the created PDF does not display same results. the table width are not kept. Data is cut off at end of page on right side.

In controller

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

page to be created

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

Using pdfable I get following error:

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

after updating to $this->renderPdf('print',array('results'=>$results)); i get new error

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)

via mpdf

PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 258180 bytes) in /opt/paradox/protected/vendors/MPDF56/mpdf.php on line 10125, referer: http‌://portal-test/paradox/index.php?r=ViewWebprojectreport/admin

I pasted the styling in the variable $stylesheet

  • 写回答

1条回答 默认 最新

  • duanchao4445 2013-06-05 12:44
    关注

    html2pdf is perfect extension and I working on it in perfect way .

    To solve your problem you have to add your css in external file .

    foe example :

    $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; 
    

    this example from my project it's work in perfect way with all css fetchers :

       $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; 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿