简体   繁体   English

将 html+css 内容添加到 mPDF

[英]Adding html+css contents to mPDF

I am using mPDF to add HTML+css contents to already generated/created PDF.我正在使用 mPDF 将 HTML+css 内容添加到已经生成/创建的 PDF。 The HTML contents are added successfully but there are no css content present in it. HTML 内容已成功添加,但其中没有 css 内容。 That is, it should display a circular progress bar (as shown below) but unfortunately it does not generate it.也就是说,它应该显示一个圆形进度条(如下所示),但不幸的是它不会生成它。 My assumption is that it does not take in css input, even though I am trying to import it as mentioned in their documentation.我的假设是它不接受 css 输入,即使我试图按照他们的文档中所述导入它。 What is the issue here?这里有什么问题?

EDIT: I even added the stylesheet to the html code here but still it does not take the input.编辑:我什至将样式表添加到 html 代码中,但它仍然不接受输入。

<?php
use setasign\Fpdi\Fpdi;

session_start();
ob_start();

require_once __DIR__ . '/vendor/autoload.php';

require_once('FPDI/autoload.php');
require_once('FPDI/Fpdi.php'); 

$pdf = new \Mpdf\Mpdf();

$pdf->AddPage(); 

$pdf->setSourceFile('file.pdf'); 

$tplIdx = $pdf->importPage(1); 

$pdf->useTemplate($tplIdx, 0, 0, 297, 420, true); 

$html = "

<style>
.flex-wrapper {
  display: flex;
  flex-flow: row nowrap;
}

.single-chart {
  width: 33%;
  justify-content: space-around;
}

.circular-chart {
  display: block;
  margin: 10px auto;
  max-width: 80%;
  max-height: 250px;
}

.circle-bg {
  fill: none;
  stroke: #eee;
  stroke-width: 3.8;
}

.circle {
  fill: none;
  stroke-width: 2.8;
  stroke-linecap: round;
}

.circular-chart.orange .circle {
  stroke: #ff9f00;
}

.percentage {
  fill: #666;
  font-family: sans-serif;
  font-size: 0.5em;
  text-anchor: middle;
}
</style>

<div class='html-content'>
    <div class='flex-wrapper'>
        <div class='single-chart'>
            <svg viewBox='0 0 36 36' class='circular-chart orange'>
                <path class='circle-bg' d='M18 2.0845
          a 15.9155 15.9155 0 0 1 0 31.831
          a 15.9155 15.9155 0 0 1 0 -31.831' />
                <path class='circle' stroke-dasharray='12, 100' d='M18 2.0845
          a 15.9155 15.9155 0 0 1 0 31.831
          a 15.9155 15.9155 0 0 1 0 -31.831' />
                <text x='18' y='20.35' class='percentage'>12%</text>
            </svg>
        </div>
    </div>
</div>

";

$pdf->WriteHTML($html);

$pdf->Output("$file_new.pdf", "D");
ob_end_flush(); 
?>

Outside the php tag, here is the html and css code given separetly:在 php 标签之外,这里是 html 和 css 代码分别给出:

 .flex-wrapper { display: flex; flex-flow: row nowrap; }.single-chart { width: 33%; justify-content: space-around; }.circular-chart { display: block; margin: 10px auto; max-width: 80%; max-height: 250px; }.circle-bg { fill: none; stroke: #eee; stroke-width: 3.8; }.circle { fill: none; stroke-width: 2.8; stroke-linecap: round; }.circular-chart.orange.circle { stroke: #ff9f00; }.percentage { fill: #666; font-family: sans-serif; font-size: 0.5em; text-anchor: middle; }
 <div class="html-content"> <div class="flex-wrapper"> <div class="single-chart"> <svg viewBox="0 0 36 36" class="circular-chart orange"> <path class="circle-bg" d="M18 2.0845 a 15.9155 15.9155 0 0 1 0 31.831 a 15.9155 15.9155 0 0 1 0 -31.831" /> <path class="circle" stroke-dasharray="56, 100" d="M18 2.0845 a 15.9155 15.9155 0 0 1 0 31.831 a 15.9155 15.9155 0 0 1 0 -31.831" /> <text x="18" y="20.35" class="percentage">56%</text> </svg> </div> </div> </div>

This progress bar does not get generated inside the PDF.此进度条不会在 PDF 内部生成。

Best solution is to use stylesheets.最好的解决方案是使用样式表。 See example below:请参见下面的示例:

             <?php

             $html = $divPrint;

             include('mpdf.php'); // including mpdf.php
             $mpdf=new mPDF();
             $stylesheet = file_get_contents('pdf.css'); //this is ur external css
             $mpdf->WriteHTML($stylesheet,1);
             $mpdf->WriteHTML($html,2);
             $mpdf->Output();
             exit;

             ?>

Assign html in $mpdf and then include it in mpdf.php file在 $mpdf 中分配 html 然后将其包含在 mpdf.php 文件中

Hope it works,!希望它有效,! If not, tell us we are here to give more solutions!!如果没有,请告诉我们我们在这里提供更多解决方案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM