大者在途

  • 我的gitee仓库
大者在途
高山仰止 景行行止
  1. 首页
  2. 后端开发
  3. 正文

一个投机取巧的方式实现office文件在线预览

2021年9月6日 441点热度 0人点赞 0条评论

如果按照原始需求应该是在线直接渲染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'
]);
标签: 暂无
最后更新:2021年9月6日

猫铃儿

一个成长中的程序员

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

COPYRIGHT © 2021 大者在途. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS

冀ICP备18021118号-1