在yii2中创建PDF时如何设置CSS? [英] How to set css at time of PDF creation in yii2?

查看:112
本文介绍了在yii2中创建PDF时如何设置CSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PDF功能* 从此函数获取数据后,从ajax调用此函数,该文件称为anothe文件,这里我显示来自数据库的数据.该文件的所有css部分都在style标签中使用,因为它是pdf中的style标签prind文本和如果所有css文件都包含在css文件夹中,则说明PDF格式不正确

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>

推荐答案

演示指出:

任何<style>标签都将被跳过(您必须为此使用cssInline属性)

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

如果只有几种样式可以添加到pdf中,则可以像这样传递它:

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}';

但是,如果您有很多样式,也可以考虑将默认的cssFile更改为具有正确CSS的任何文件. (默认情况下,此属性为@vendor/kartik-v/yii2-mpdf/assets/bootstrap.min.css

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

这篇关于在yii2中创建PDF时如何设置CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆