生成pdf
This commit is contained in:
@@ -13,43 +13,43 @@ class MpdfDriver
|
||||
public function htmlToPdf(string $html, array $options = []): string
|
||||
{
|
||||
$opts = array_merge([
|
||||
'format' => 'A4',
|
||||
'orientation' => 'L',
|
||||
'margin_top' => 12,
|
||||
'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'],
|
||||
'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' => '',
|
||||
'asset_base' => '',
|
||||
'enable_html5_parser' => true,
|
||||
], $options);
|
||||
|
||||
$defaultConfig = (new ConfigVariables())->getDefaults();
|
||||
$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'],
|
||||
'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'],
|
||||
'tempDir' => $opts['tempDir'],
|
||||
'fontDir' => $fontDirs,
|
||||
'fontdata' => $fontData,
|
||||
'default_font' => $opts['default_font'],
|
||||
'enable_html5_parser' => $opts['enable_html5_parser'],
|
||||
]);
|
||||
|
||||
@@ -115,4 +115,55 @@ class MpdfDriver
|
||||
}
|
||||
return [$css, $html];
|
||||
}
|
||||
|
||||
public function exportUsedCarPdf($car)
|
||||
{
|
||||
/*$car = [
|
||||
'title' => '奥迪Q3 2013款 35 TFSI 舒适型',
|
||||
'vin' => '',
|
||||
'price' => 450000,
|
||||
'source_city' => '北京',
|
||||
'register_city' => '随州',
|
||||
'register_date' => '2026-01-05',
|
||||
'displacement' => '2.1T',
|
||||
'mileage' => 45000,
|
||||
'power' => 350,
|
||||
'color' => '',
|
||||
'created_at' => '2026-01-05 17:23:34',
|
||||
'images' => [
|
||||
'https://xxx.com/1.jpg',
|
||||
'https://xxx.com/2.jpg',
|
||||
'https://xxx.com/3.jpg',
|
||||
],
|
||||
'description' => '该车内饰干净整洁,车况精品...'
|
||||
];*/
|
||||
|
||||
ob_start();
|
||||
//include app_path() . '/view/pdf/car_detail_pdf.php';
|
||||
$tpl = base_path('app/common/utils/pdf/template/usedCar.stub');
|
||||
include $tpl;
|
||||
$html = ob_get_clean();
|
||||
|
||||
$mpdf = new Mpdf([
|
||||
'mode' => 'utf-8',
|
||||
'format' => 'A4',
|
||||
'margin_top' => 10,
|
||||
'margin_bottom' => 10,
|
||||
'margin_left' => 10,
|
||||
'margin_right' => 10,
|
||||
'default_font' => 'sun-exta'
|
||||
]);
|
||||
|
||||
$mpdf->WriteHTML($html);
|
||||
|
||||
$pdf = $mpdf->Output('车辆详情.pdf', 'S');
|
||||
$dir = runtime_path('pdf');
|
||||
if (!is_dir($dir)) mkdir($dir, 0777, true);
|
||||
$pdfName = '/invoice-' . date('YmdHis').'-'.$car['id'] . '.pdf';
|
||||
$path = $dir . $pdfName;
|
||||
file_put_contents($path, $pdf);
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user