【问题标题】:Yii mPDF css errorYii mPDF css错误
【发布时间】:2014-02-16 01:12:57
【问题描述】:

我已经阅读了一些关于如何在 mPDF 中添加 css 样式的帖子,我正在使用 Yii 框架,我尝试了很多方法都没有结果,知道如何让它工作吗?

$this->layout="//layouts/pdf";
        $mPDF = Yii::app()->ePdf->mpdf();
        $html = $this->render('pdf', array('dataProvider'=>$dataProvider), true);

        $stylesheet = file_get_contents(Yii::app()->request->baseUrl.'/css/print.css');
        $stylesheet .= file_get_contents(Yii::app()->request->baseUrl.'/css/main.css');

        $mPDF->WriteHTML($stylesheet, 1);
        $mPDF->WriteHTML($html, 2);

        $mPDF->Output('Inmuebles',EYiiPdf::OUTPUT_TO_BROWSER);

这是控制器代码。

<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/print.css" media="print" />
    <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/main.css" />

这是 layout/pdf 中的代码

错误:

file_get_contents(/yii/yiitest/css/print.css): failed to open stream: No such file or directory

【问题讨论】:

    标签: css yii frameworks widget


    【解决方案1】:

    尝试这种方式(在控制器中),它对我有用:

    $mPDF = Yii::app()->ePdf->mpdf();
    $html = $this->render('pdf', array('dataProvider'=>$dataProvider), true);
    
    $stylesheet = file_get_contents(/* path to first css file */);
    $stylesheet .= file_get_contents(/* path to second css file */);
    
    $mPDF->WriteHTML($stylesheet, 1);
    $mPDF->WriteHTML($html, 2);
    
    echo $mPDF->Output('Inmuebles',EYiiPdf::OUTPUT_TO_BROWSER);
    

    【讨论】:

    • file_get_contents(/yii/yiitest/css/print.css):打开流失败:没有这样的文件或目录,但文件存在
    • 试试这个方法: file_get_contents(Yii::getPathOfAlias('webroot') . /*path to your css file from the project root*/) as the webroot is the actual path of your project root .
    • $stylesheet = file_get_contents(DIR.'/../../css/print.css'); $stylesheet .= file_get_contents(DIR.'/../../css/main.css');这就是我修复它的方法,但非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-06
    相关资源
    最近更新 更多