php5导出pdf
使用php5最低使用0.8.0版本
composer install dompdf/dompdf:0.8.0
php代码
<?php
include './vendor/autoload.php';
use Dompdf\Dompdf;
use Dompdf\Options;
$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isPhpEnabled', true);
$options->set('isFontSubsettingEnabled', true);
$options->set('isRemoteEnabled', true);
$options->set('defaultFont', 'msyh');//或者在css中设置字体body {font-family:msyh;}
$content = "<h1 style='text-align:center'>若水hello word!</h1>";
$dompdf = new Dompdf($options);
//加载HTML文档
$dompdf->loadHtml($content);
//array(0,0,1000,2000)会创建1000mmX2000mm的纸张大小
// $dompdf->setPaper(array(0,0,1000,2000));
//设置 PDF 页面的大小和方向 portrait,landscape横向
////设置页面尺寸为A4,页面方向
$dompdf->setPaper('A4', 'portrait');
//在上述代码中,我们使用setPaper()方法来设置页面尺寸和方向。第一个参数是页面尺寸,可以是A4、Letter等。第二个参数是页面方向,可以是portrait(纵向)或landscape(横向)。
$options = $dompdf->getOptions();
// 渲染 PDF 页面
$dompdf->render();
//输出PDF
$dompdf->stream('ruoshui.pdf', ['Attachment' => false]);
// 将 PDF 文件保存到指定位置
$output = $dompdf->output();
file_put_contents('exported_pdf.pdf', $output);
其中msyh是微软雅黑字体,要不然中文乱码
中文乱码解决方案:
1.下载类库语言安装脚本
git clone https://github.com/dompdf/utils.git
2.将里面的load_font.php文件拷贝到项目根目录,和verdor同级,把字体文件放到同级,如果没有字体,点击这里字体下载
3.运行安装
php load_font.php msyh msyh.ttf
乱码完美解决