php laravel word 转 pdf

首先安装第三方类

composer require dompdf/dompdf
composer require phpoffice/phpword

加载字体
参考文章 laravel 使用 dompdf 将html 转为pdf文件

word 转 pdf 核心代码

		$filePath = public_path();
        $wordName = $filePath . '/test001.docx';//需要转的word文档
        $htmlName = $filePath . '/test.html';//html文件地址
        $pdfName = $filePath . '/test.pdf';//pdf保存地址
        $phpWord = IOFactory::load($wordName);
        // 将PhpWord文档转换为HTML
        IOFactory::createWriter($phpWord , 'HTML')->save($htmlName);
        $html = file_get_contents($htmlName);
        @unlink($htmlName);
        $options = new Options();
        $options->setIsRemoteEnabled(true);
        $options->setDefaultFont('simsun');
        $dompdf = new Dompdf($options);
        $css = '<style>
        body {
            word-wrap: break-word;
            word-break: break-all;
            font-size: 10px !important;
        }
        img{
           max-width:100%;
        }
    </style>';
        $html = substr_replace($html, $css, strpos($html, '</head>'), 0);
        $dompdf->loadHtml($html);
        $dompdf->setPaper('A4');
        $dompdf->render();
        //        file_put_contents($pdfName, $dompdf->output());
        $dompdf->stream('document.pdf', ['Attachment' => false]);
        exit;
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值