登录
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
// app/service/pdf/MpdfDriver.php
|
||||
namespace app\common\utils\pdf\service;
|
||||
|
||||
use Mpdf\Mpdf;
|
||||
use Mpdf\Config\ConfigVariables;
|
||||
use Mpdf\Config\FontVariables;
|
||||
use Mpdf\HTMLParserMode;
|
||||
|
||||
class MpdfDriver
|
||||
{
|
||||
|
||||
public function htmlToPdf(string $html, array $options = []): string
|
||||
{
|
||||
$opts = array_merge([
|
||||
'format' => 'A4',
|
||||
'orientation' => 'L',
|
||||
'margin_top' => 12,
|
||||
'margin_bottom' => 12,
|
||||
'margin_left' => 10,
|
||||
'margin_right' => 10,
|
||||
'tempDir' => runtime_path('mpdf'),
|
||||
'header_html' => '',
|
||||
'footer_html' => '',
|
||||
'watermark_text'=> '',
|
||||
'css_files' => [],
|
||||
'default_font' => 'notosanssc',
|
||||
'font_dirs' => [public_path('fonts')],
|
||||
'font_data' => [
|
||||
'notosanssc' => ['R'=>'NotoSansSC-Regular.ttf','B'=>'NotoSansSC-Bold.ttf'],
|
||||
],
|
||||
'asset_base' => '',
|
||||
'enable_html5_parser' => true,
|
||||
], $options);
|
||||
|
||||
$defaultConfig = (new ConfigVariables())->getDefaults();
|
||||
$defaultFontConfig = (new FontVariables())->getDefaults();
|
||||
|
||||
$fontDirs = array_merge($defaultConfig['fontDir'], $opts['font_dirs']);
|
||||
$fontData = array_merge($defaultFontConfig['fontdata'], $opts['font_data']);
|
||||
|
||||
$mpdf = new Mpdf([
|
||||
'format' => $opts['format'],
|
||||
'orientation' => $opts['orientation'],
|
||||
'margin_left' => $opts['margin_left'],
|
||||
'margin_right' => $opts['margin_right'],
|
||||
'margin_top' => $opts['margin_top'],
|
||||
'margin_bottom' => $opts['margin_bottom'],
|
||||
'tempDir' => $opts['tempDir'],
|
||||
'fontDir' => $fontDirs,
|
||||
'fontdata' => $fontData,
|
||||
'default_font' => $opts['default_font'],
|
||||
'enable_html5_parser' => $opts['enable_html5_parser'],
|
||||
]);
|
||||
|
||||
if ($opts['header_html']) $mpdf->SetHTMLHeader($opts['header_html']);
|
||||
if ($opts['footer_html']) $mpdf->SetHTMLFooter($opts['footer_html']);
|
||||
|
||||
if (!empty($opts['watermark_text'])) {
|
||||
$mpdf->SetWatermarkText($opts['watermark_text']);
|
||||
$mpdf->showWatermarkText = true;
|
||||
$mpdf->watermarkTextAlpha = 0.08;
|
||||
}
|
||||
|
||||
// 资源绝对化(如果你有相对路径的图片等)
|
||||
$html = PdfHelper::absolutizeAssetUrls($html, $opts['asset_base']);
|
||||
|
||||
// === 关键改动:把 <style>…</style> 抽出来,作为 HEADER_CSS 写入 ===
|
||||
[$inlineCss, $cleanHtml] = $this->extractCssAndCleanHtml($html);
|
||||
|
||||
// 先写外部 css_files
|
||||
if (!empty($opts['css_files'])) {
|
||||
foreach ($opts['css_files'] as $cssFile) {
|
||||
if (is_file($cssFile)) {
|
||||
$mpdf->WriteHTML(file_get_contents($cssFile), HTMLParserMode::HEADER_CSS);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 再写内联 <style> 的 CSS
|
||||
if ($inlineCss !== '') {
|
||||
$mpdf->WriteHTML($inlineCss, HTMLParserMode::HEADER_CSS);
|
||||
}
|
||||
|
||||
// 最后写 HTML 正文(已剔除 <style>,避免任何情况下把 CSS 当文字打印)
|
||||
$mpdf->WriteHTML($cleanHtml, HTMLParserMode::HTML_BODY);
|
||||
|
||||
return $mpdf->Output('', \Mpdf\Output\Destination::STRING_RETURN);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把所有 <style>...</style> 的内容拼成一段 CSS,并把这些 <style> 标签从 HTML 中移除
|
||||
* @return array{0:string,1:string} [css, htmlWithoutStyle]
|
||||
*/
|
||||
private function extractCssAndCleanHtml(string $html): array
|
||||
{
|
||||
$cssParts = [];
|
||||
// 抓取所有 <style ...>...</style>,DOTALL 支持跨行
|
||||
if (preg_match_all('/<style\b[^>]*>(.*?)<\/style>/is', $html, $m)) {
|
||||
foreach ($m[1] as $block) {
|
||||
// 去掉 HTML 注释,以防注释把 CSS 包起来
|
||||
$block = preg_replace('/<!--|-->/', '', $block);
|
||||
$cssParts[] = trim($block);
|
||||
}
|
||||
// 从 HTML 删除所有 <style> 块
|
||||
$html = preg_replace('/<style\b[^>]*>.*?<\/style>/is', '', $html);
|
||||
}
|
||||
$css = implode("\n\n", $cssParts);
|
||||
|
||||
// 额外保险:把 <head> 也清理掉,防止某些解析器把 head 里残余文本当正文
|
||||
$html = preg_replace('/<head\b[^>]*>.*?<\/head>/is', '', $html);
|
||||
|
||||
// 确保剩余 HTML 有 <body>,没有就包一层
|
||||
if (!preg_match('/<body\b/i', $html)) {
|
||||
$html = '<body>' . $html . '</body>';
|
||||
}
|
||||
return [$css, $html];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user