如何在mpdf中添加多个css文件

yxyvkwin  于 2022-11-27  发布在  其他
关注(0)|答案(3)|浏览(123)

我正在尝试转换我的html页面到pdf格式使用mpdf.问题是,我无法应用一个以上的css到pdf文件..这里是我的代码的php

<?php
           $html =file_get_contents('mpdf/test.html');
            include("../mpdf.php");
            $mpdf=new mPDF(); 
           $mpdf->SetDisplayMode('fullpage','two');
            // LOAD a stylesheet 1
            $stylesheet = file_get_contents('assets/css/main.css');
            $mpdf->WriteHTML($stylesheet,1);    // The parameter 1 tells that this is css/style only and no body/html/text
          // LOAD a stylesheet 2
            $stylesheetextra = file_get_contents('assets/css/test.css');
            $mpdf->WriteHTML($stylesheetextra ,1);  // The parameter 1 tells that this is css/style only and no body/html/text
            $mpdf->WriteHTML($html,2);
          $mpdf->Output();

            exit;
            ?>

它给出的输出不附带test.css。main.css是正确应用到pdf文件,但test.css不适用。请帮助我?提前感谢您

thigvfpy

thigvfpy1#

只需将CSS内容存储在一个变量中即可,如下所示:

$stylesheet  = '';
$stylesheet .= file_get_contents('css/bootstrap.min.css');
$stylesheet .= file_get_contents('css/style.css');
$stylesheet .= file_get_contents('css/impressao_ctr.css');
x6h2sr28

x6h2sr282#

这可能是一个丑陋的解决方案,但我以前遇到过类似的问题。我所做的是,因为这不是一个大问题,我把所有的css从第二个我不能添加,并添加到“main.css”。

ux6nzvsh

ux6nzvsh3#

(mPDF ≥ 1.0)

使用\Mpdf\HTMLParserMode::HEADER_CSS作为第二个参数。

$stylesheet = file_get_contents('assets/css/main.css');
$mpdf->WriteHTML($stylesheet, \Mpdf\HTMLParserMode::HEADER_CSS);

$stylesheetextra = file_get_contents('assets/css/test.css');
$mpdf->WriteHTML($stylesheetextra, \Mpdf\HTMLParserMode::HEADER_CSS);

链接origin

相关问题