博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
shell下office、html、pdf文档互转方法
阅读量:4647 次
发布时间:2019-06-09

本文共 1625 字,大约阅读时间需要 5 分钟。

OFFICE 文档在线预览方案很多:

  • 服务器先转换为PDF,再转换为SWF,最后通过网页加载Flash预览,比如flexpaper
  • Office文档直接转换为SWF,通过网页加载Flash预览
  • 微软的Office365
  • 在浏览器中直接打开
  • 转换为html

今天,我们要用的方案是转换为html来预览。

技术方案: 

- office文档转换为pdf:使用libreoffice 
- pdf转html,使用pdf2htmlex

测试环境: 

操作系统:ubuntu 12.04

1. office文档转pdf

1.1 安装libreoffice

apt-get install libreoffice-common
  • 1

1.2 启动转换服务

soffice --accept="socket,host=127.0.0.1,port=2002;urp;" --nofirststartwizard
  • 1

1.3 文档转换

网上一般介绍的都是使用JODConverter ,通过java来调用转换服务,其实还有一种替代方案,就是使用pyodconverter,python版本的转换脚本

下载后,进行测试:

python DocumentConverter.py Website_Information_Form.doc new.pdf
  • 1

soffice 可以正常转换为pdf,但是中文显示为乱码

1.4 中文乱码问题修复

google一下,乱码问题大概是字体缺失的问题,于是尝试将windows下的字体拷贝过去,复制windows下的字体到/usr/share/fonts下,然后刷新字体缓存

sudo fc-cache -fv 2>&1 | grep failed | cut -f1 -d":" | xargs -i sudo touch {} && sudo fc-cache -fv
  • 1

再重启转换服务,再次测试,已经OK了!

2.pdf转html

pdf转html,使用国人开源的,一开始尝试通过源码编译安装,依赖组件太多,安装非常麻烦!感兴趣的可以执行编译安装,参见()

下面介绍简单安装方式:

2.1 通过apt安装

sudo add-apt-repository ppa:coolwanglu/pdf2htmlex sudo apt-get update sudo apt-get install pdf2htmlex
  • 1
  • 2
  • 3

2.2 测试pdf2htmlex

pdf2htmlEX --zoom 1.3 xiaoshujiang.pdf
  • 1

可以看到,当前目录生成了xiaoshujiang.html

2.3 转换脚本

写一个脚本,将两次转换放到一起,方便调用:

#convert2html.sh temp=$(date +%Y%m%d%H%m%s) python DocumentConverter.py $1 ${temp}.pdf pdf2htmlEX --zoom 1.3 $temp.pdf mv $temp.html $2 rm $temp.pdf
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3. 测试

3.1 word (doc.docx)测试

convert2html.sh imo云办公室-私有云用户使用手册V2.0.doc imo-doc.html
  • 1

效果:

enter description here

3.2 表格 (xls)测试

convert2html.sh xxx.xlsx xxx.html
  • 1

3.3 PPT (pptx)测试

./convert.sh xxx.pptx xxx.html
  • 1

4. 总结

本文介绍了一种在服务端将office文档转换为html以方便预览的方法。

实际使用时,可以将生成好的html放到网站路径下,通过拦截器,设置访问权限。

转载于:https://www.cnblogs.com/jinhengyu/p/10257946.html

你可能感兴趣的文章
视频教程--ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库
查看>>
第五次作业
查看>>
织梦教程
查看>>
杭电多校 Harvest of Apples 莫队
查看>>
java 第11次作业:你能看懂就说明你理解了——this关键字
查看>>
C/C++心得-结构体
查看>>
函数名作为参数传递
查看>>
apt-get for ubuntu 工具简介
查看>>
数值计算算法-多项式插值算法的实现与分析
查看>>
day8-异常处理与网络编程
查看>>
Python基础-time and datetime
查看>>
Linux epoll 笔记(高并发事件处理机制)
查看>>
shell脚本练习01
查看>>
WPF图标拾取器
查看>>
通过取父级for循环的i来理解闭包,iife,匿名函数
查看>>
HDU 3374 String Problem
查看>>
数据集
查看>>
打印python包含汉字报SyntaxError: Non-ASCII character '\xe4' in file
查看>>
[Leetcode] unique paths ii 独特路径
查看>>
HDU 1217 Arbitrage (Floyd + SPFA判环)
查看>>