首页 文章

如何在Yii2中使用kartik mpdf将HTML页面导出为PDF?

提问于
浏览
1

我需要将我的html文件导出为Pdf格式并将其保存到pdf文件夹中 . 到目前为止我做到了这一点 .

这是我保存为messagesend.php的html页面,需要导出 .

<?php
    $rfp = \app\models\RfpMeta::find()->where(['rfp_id' => $rfp_id])->all();

    ?>

<div style="display: inline-block;
     width: 85%;
     margin: 10px;
     border: 1px solid #ccc;
     ">
    <div style="width: 100%;*zoom: 1;">
        <div style="padding: 15px 5px;height: 65px;width: auto;background-color: #3398d4;">

        </div>
    </div>
    <div style="margin: 5px;"></div>
    <div style="width: 100%;*zoom: 1;">
        <div style="margin:10px 0;padding: 5px;">
            <span style="margin-top:20px;margin-bottom:10px;font-family:inherit;font-weight:bold;line-height:1.1;color:#444;font-size: 18px;"> Request For Proposal</span><br><br>
        </div>
    </div>
    <div style="width: 100%;*zoom: 1;">
        <div style="padding: 5px;">
             <p  class="text-center" style="text-align:justify">Subject : <?php echo $subject;
                                                ?></p>
                                                
<p style="text-align:justify;">Dear <?php foreach ($model as $key ) { echo $key ." ". "/" ; } ?>,</p>
<p style="text-align:justify;"> <?php echo $message ?><br> <table class="table table-hover table-striped table-bordered table-condensed"> <thead> <tr> <th class="text-center">Item</th> <th class="text-center">Quantity</th> <th class="text-center">Unit</th> </tr> </thead> <?php foreach ($rfp as $each) { $typ = $each->type; if ($typ == 1) { $model = \app\models\RawMaster::findOne(['id'=>$each['item']])->raw_name; } elseif ($typ ==2) { $model = \app\models\AssetMaster::findOne(['id'=>$each['item']])->asset_name; } elseif ($typ == 3) { $model = \app\models\MachineMaster::findOne(['id'=>$each['item']])->name; } ?> <tbody> <tr> <td> <?= $model;?></td> <td><?= $each['qty']?></td> <td><?= $each['unit'] ?></td> </tr> </tbody> <?php } ?> </table> <p style="text-align:justify;"> Thank you.<br> </p> </div> </div> <div style="width: 100%;*zoom: 1;"> <div style="padding: 5px;"> <br> <p style="text-align:justify;color:#777">If you want to unsubscribe <a href="">click here</a></p> <p style="font-size: x-small;text-align:justify;color:#777"> This e-mail and any files transmitted with it may contain privileged or confidential information. It is solely for use by the individual for whom it is intended, even if addressed incorrectly. If you received this e-mail in error, please notify the sender; do not disclose, copy, distribute, or take any action in reliance on the contents of this information; and delete it from your system. Any other use of this e-mail is prohibited. Thank you for your compliance. Copyright © 2016 by All rights reserved. </p> </div> </div> </div> </div>

现在下面是我用来生成pdf的控制器 .

use kartik\mpdf\Pdf;
public function actionReport() {
// get your HTML raw content without any layouts or scripts
$content = $this->renderPartial('messagesend');

// setup kartik\mpdf\Pdf component
$pdf = new Pdf([
    // set to use core fonts only
    'mode' => Pdf::MODE_CORE, 
    // A4 paper format
    'format' => Pdf::FORMAT_A4, 
    // portrait orientation
    'orientation' => Pdf::ORIENT_PORTRAIT, 
    // stream to browser inline
    'destination' => Pdf::DEST_BROWSER, 
    // your html content input
    'content' => $content,  
    // format content from your own css file if needed or use the
    // enhanced bootstrap css built by Krajee for mPDF formatting 
    'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
    // any css to be embedded if required
    'cssInline' => '.kv-heading-1{font-size:18px}', 
     // set mPDF properties on the fly
    'options' => ['title' => 'Krajee Report Title'],
     // call mPDF methods on the fly
    'methods' => [ 
        'SetHeader'=>['Krajee Report Header'], 
        'SetFooter'=>['{PAGENO}'],
    ]
]);

// return the pdf output as per the destination setting
return $pdf->render();

}

它现在显示以下错误 . 下图中出错 .
enter image description here

我现在如何生成Pdf?我也是新手,所以你们可以向我解释一下吗?

1 回答

  • 1

    您必须请求与您的操作相对应的网址:http://example.com/yourcontroller/report

    如果要生成PDF并将其存储在服务器中,则必须将 destination 更改为 Pdf::DEST_FILE ,指定路径/文件名 . 检查docs .

相关问题