How to set css at time of PDF creation in yii2?

大城市里の小女人 提交于 2019-12-13 04:43:54

问题


Pdf fuction* This function is call from ajax after getting data from this function it's call anothe file here I am display data which is coming from data base.That file all css part in style tag using then as it is style tag prind in pdf as a text and if this all css file include in css folder then pdf is not create in proper format

public function actionInvoicespacking(){ 
    $pdf = Yii::$app->pdf;
    $modelShipment = Shipment::find()->orderBy('ship_id DESC')->one();
    if(count($modelShipment) == 0) {

      $shipid= 1;
    } else 
    {
     $shipid= $modelShipment->ship_id;

   }
   $htmlContent = file_get_contents('http://localhost/shepherdlogistics_v1.0/backend/views/shipment/invoicespackingdata.php');
   $pdf->content = $htmlContent;
   return $pdf->render();

 }

invoicespackingdata.php

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Invoice</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="css/bootstrap.fontawesome.min.css">
    <link rel="stylesheet" href="css/stylepdf.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <style type="text/css">
    body {
        font-size: 18px;
        font-family: cambria;
    }

    .nopadd {
        padding: 0px;
    }
    </style>
</head>

<body>
    <div class="container">
        <br>
        <br>
        <div class="">
            <div class="col-lg-2 nopadd">
                <img src="logopdf.png" alt="logo" width="100%">
            </div>
            <div class="col-lg-10">
                <h1 style="font-size: 55px;font-weight: bold; padding: 23px 0px;">
                SHEPHERD LOGISTICS CO. W.L.L.</h1>
            </div>
        </div>
</div>
</div>

回答1:


As the demo states:

Any <style> tag will be skipped (you must use cssInline property for this)

If you have just a few styles to add to the pdf, you can pass it like this:

$pdf->content = $htmlContent;
$pdf->cssInline = '.class1 {color: red} .class2 {color:blue}';

But if you have alot of styles, you can also consider change the default cssFile to any file you want with the correct css. (by default, this property is @vendor/kartik-v/yii2-mpdf/assets/bootstrap.min.css



来源:https://stackoverflow.com/questions/36832306/how-to-set-css-at-time-of-pdf-creation-in-yii2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!