如何在yii2中为生成pdf文件的MPDF设置CSS



我正在使用yii2 basic,我需要生成一个带有CSS文件样式的pdf报告文件,我很好地获得了pdf,但CSS无法处理生成的文件。以下是我如何实现的。

在我的controller.php文件中

public function actionPrintReport($student_id,$exam_id,$total_subjects,$exam_date){
$model = new Results();
$examReportData = $model->getExamReport($student_id,$exam_id,$exam_date);

$htmlContent = $this->renderPartial('_report',
[
'model' => $model,
'total_subjects' => $total_subjects,
'examReportData' => $examReportData
]);
$pathfile = "Student_exam_report";
$mpdf = new MpdfMpdf([
'tempDir' => Yii::getAlias('@runtime/') . 'mpdf2/tmp',
'format' => 'A4-L',
'margin_right' => 5,'margin_left' => 5,
'defaultFont' => 'Calibri',
]);
$stylesheet = '<head><link rel="stylesheet" type="text/css" href="'.Yii::getAlias('@web').'cssexam_report.css'.'"/></head>';
$mpdf->WriteHTML($stylesheet, MpdfHTMLParserMode::HEADER_CSS);
$mpdf->WriteHTML($htmlContent, MpdfHTMLParserMode::HTML_BODY);
$mpdf->Output($pathfile, 'I');
}

_report.php文件位于view/中,具有以下最小代码

<?php
use yiihelpersHtml;
?>
<div class="container" style="width: 100%">
<div class="leftpan">Left Pan</div>
<div class="rightpan">Right Pan</div>
</div>

CSS文件位于project_folder/web/css/exam_report.css,代码最少,如下所示。

.rightleft{
height: 150px;
float: left;
border-style: dotted dashed solid double;
}
.rightpan{
height: 150px;
float: right;
border-style: dotted dashed solid double;
}

我已经在这里堆积了两天:(,如果你能支持我,我将不胜感激。

您可以使用;cssFile";外部css文件的选项。在我的情况下,插件中包含的引导css文件不起作用,所以我在web文件夹中创建了自己的文件。添加时还必须检查文件的路径。

$pdf = new Pdf([
'mode' => Pdf::MODE_CORE,
'format' => Pdf::FORMAT_A4,
'orientation' => Pdf::ORIENT_PORTRAIT,
'filename' => '/backend/web/invoices/invoice.pdf',
'destination' => Pdf::DEST_FILE,
'content' => $content,
'cssFile' => 'css/invoicetemplate.css',
'cssInline' => '.heading{font-size:18px}',
'options' => ['title' => 'Invoice'],
]);

最新更新