php - 将 html+css 内容添加到 mPDF
问题描述
我正在使用 mPDF 将 HTML+css 内容添加到已生成/创建的 PDF 中。HTML 内容已成功添加,但其中没有 css 内容。也就是说,它应该显示一个圆形进度条(如下所示),但不幸的是它不会生成它。我的假设是它不接受 css 输入,即使我试图按照他们的文档中提到的那样导入它。这里有什么问题?
编辑:我什至在此处将样式表添加到 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();
?>
在 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>
此进度条不会在 PDF 中生成。
解决方案
最好的解决方案是使用样式表。请参见下面的示例:
<?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 文件中
希望它有效!如果没有,请告诉我们我们在这里提供更多解决方案!
推荐阅读
- azure-sql-database - 仅当目标数据库不存在时,才使用 ARM 模板通过复制另一个数据库来创建数据库
- indexing - 带有 solr 的 Nutch 通过给定的 url 索引 PDF
- javascript - 按键对对象数组进行排序
- google-bigquery - 如何在 Google Data Studio 中按 2 个或更多用户属性过滤事件?
- message-queue - 暂时挂起:Azure 服务总线消息队列
- android - 奇怪的 AndroidViewModel LiveData 观察者行为
- kotlin - 如何捕获被拒绝的承诺并继续使用 kovenant 进行承诺更改
- laravel - 在 Hostinger 上部署 Laravel 项目
- c# - 如何用共享路径中的单个反斜杠替换双反斜杠
- python - 如何将购买 DataFrame 转换为 Spark DistributedMatrix 类型以计算项目之间的相似性?