php 使用phpSpreadsheet以及mpdf,实现将excel文件转换成pdf文件

首先确保安装了phpSpreadsheet以及mpdf插件


composer require phpoffice/phpspreadsheet
composer require mpdf/mpdf

代码示例如下:

    public function excelConvertPdf($file,$path)
    {
        $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($file);
        $phpWord = $reader->load($file);
        $phpWord ->getDefaultStyle()->applyFromArray(
            [
                // 字体
                'font'=>[
                  'name' => 'Arial',
                  'bold' => true,
                  'italic' => false,
                  'underline' => \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE,
                  'strikethrough' => false,
                  'color' => [
                      'rgb' => '808080'
                 ]
                ],
                // 边框
                'borders' => [
                    'allBorders' => [
                        'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                    ]
                ],
                'alignment' => [
                  'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
                  'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
                  'wrapText' => true,
              ],
              'quotePrefix'    => true
            ]
        );
        $xmlWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($phpWord,'Mpdf');
        $xmlWriter->writeAllSheets();
        $name = '123.pdf';
        $xmlWriter->save($path.'\\'.$name);
    }

此时遇到中文可能会出现乱码的情况,解决办法即是修改phpspreadsheet 中 Mpdf类文件,代码片段如下:

值得注意的是,转换后pdf的边框如果显示不全,此时需要检查excel文件的单元格问题

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值