1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| use Barryvdh\DomPDF\Facade\Pdf; use QL\QueryList;
public static function html2PDF($data) { $html = $data->content; $ql = QueryList::html($html); $ql->find('img')->each(function ($item) { $data_src = $item->attr('data-src'); $src = $item->attr('src'); $data_src = substr($data_src, 0, strrpos($data_src, '?')); $src = substr($src, 0, strrpos($src, '?'));
if ($src != $data_src) { $item->attr('src', $data_src); } }); $html = $ql->getHtml(); $html = str_replace(' ', ' ', $html); $html = preg_replace('/\x{00A0}/u', ' ', $html);
$style = ' <style> @font-face { font-family: "Noto_Serif_SC"; src: url("' . storage_path('fonts/NotoSerifSC-Regular.ttf') . '"); } body { font-family: "Noto_Serif_SC", DejaVu Sans, sans-serif; } p { line-height: 1.5; font-size: 14px; } </style> ';
$html = $style . $html;
$pdf = Pdf::loadHTML($html, 'UTF-8');
$cate_id = $data->cate_id; $cate = Db::table('cate')->where('id', $cate_id)->first(); $cate_name = $cate->name; $relativePath = 'pdf/' . $cate_name . '/' . str_replace('姓名:', '', $data->title) . '.pdf';
Storage::disk('public')->makeDirectory('pdf'); $re = Storage::disk('public')->put($relativePath, $pdf->output()); dump("$cate_name,$data->title,$re");
}
|