如果按照原始需求应该是在线直接渲染office文件, 但是时间紧任务重, 没去细细想解决方案, 这里想了个投机取巧的方法, 来间接实现.
众所周知, html, txt, pdf 是可以直接在浏览器页面展示的, 那么把office文件转换成以上方式是不是可以, 但是office文件是一个富文本文件, 显然txt是不行的, 放在html中, 又担心style样式不能还原(没有测试, 只是担心), 所以采用先转换pdf 再输出PDF展现的方式.
服务器端安装libreoffice
yum list libreoffice
yum install libreoffice.x86_64
php 代码安装 mnvx/lowrapper
组件
composer require mnvx/lowrapper
代码示例
// 转换为pdf存储在服务器上
$converter = new Converter();
$a = Log::channel('libreoffice');
$converter->setLogger($a);
$parameters = new LowrapperParameters();
$parameters->setInputFile($inputFile)
->setOutputFormat(Format::GRAPHICS_PDF)
->setOutputFile($outputFilePath);
$converter->convert($parameters);
// laravel直接输出pdf文件给浏览器渲染
return Storage::disk('public')->response($path,'preview',[
'Content-Type'=> MimeType::from($path),
'X-Content-Type-Options' => 'nosniff'
]);
文章评论