首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向mPDF添加html+css内容

向mPDF添加html+css内容
EN

Stack Overflow用户
提问于 2021-04-07 20:10:21
回答 1查看 83关注 0票数 0

我使用mPDF将HTML+css内容添加到已生成/创建的PDF中。HTML内容已成功添加,但其中不存在css内容。也就是说,它应该显示一个圆形进度条(如下所示),但不幸的是它没有生成它。我的假设是它不接受css输入,即使我试图像他们的文档中提到的那样导入它。这里的问题是什么?

编辑:我甚至在这里将样式表添加到html代码中,但它仍然不接受输入。

代码语言:javascript
复制
<?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(); 
?>

在php标签之外,下面是分别给出的html和css代码:

代码语言:javascript
复制
.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;
}
代码语言:javascript
复制
<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>

此进度条不会在PDF中生成。

EN

回答 1

Stack Overflow用户

发布于 2021-04-07 20:33:13

最好的解决方案是使用样式表。如下例所示:

代码语言:javascript
复制
             <?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;

             ?>

在$mpdf中分配html,然后将其包含在mpdf.php文件中

希望它能工作!!如果没有,告诉我们我们在这里提供更多的解决方案!!

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66985784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档