网站首页 laravel框架
laravel-orm---DOMPDF中文踩过的坑
发布时间:2016-08-27 02:47查看次数:42559
老板说要生成PDF
好,安装过程
1.
composer require barryvdh/laravel-dompdf
2.config\App.php
配置:providers
Barryvdh\DomPDF\ServiceProvider::class,
配置
'PDF' => Barryvdh\DomPDF\Facade::class,
3.调用
$pdf = PDF::loadView('pdf.invoice', $data);
return $pdf->download('invoice.pdf');
到这里坑来了 远程图片不能读取..导致图片images 不载入
2.中文乱码啊!!!!!!
各种百度 各种搜,全他没的是复制粘贴的一点都不责任~因为鸟语不好 github 看不懂啊
看不懂也要看啊
最后在
https://github.com/dompdf/dompdf/wiki/UnicodeHowTo
你能找到解决方案
图片远程问题 我用简单暴力的方法
代码如下:
public function show(){
$image_file = public_path ('images/201601.jpg'); //图片路径
$image_info = getimagesize($image_file); //获取图片类型
$data = file_get_contents($image_file); //读取图片信息
$str = "data:{$image_info['mime']};base64," . base64_encode($data); //返回base64字符串
return $str;
}
然后 img src= {!! $str !!}
共享一下中文字库
http://pan.baidu.com/s/1i4L83eH
OK 解决图片问题
乱码问题
1.先在public/fonts/ 加入你的字库
例如微软雅黑 msyh.ttf
2.在html style 写入
PDF 献给母亲的爱测试一下吧 中文来了!!!
set_option('defaultFont', 'msyh');
$dompdf->loadHtml($str,'UTF-8');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
?>关键字词:laravel-orm---DOMPDF