文件在线预览doc,docx转换pdf(一)
文件在线预览doc,docx转换pdf(一)
1. 前言
文档转换是一个是一块硬骨头,但是也是必不可少的,我们正好做的知识库产品中,也面临着同样的问题,文档转换,精准的全文搜索,知识的转换率,是知识库产品的基本要素,初识阅读时同时绞尽脑汁,自己开发?,集成第三方?都是中小企业面临的一大难题…….
自己在网上搜索着找到poi开源出来的很多例子,最开始是用poi把所有文档转换为html,
1) 在github上面找到一个https://github.com/litter-fish/transform完整的demo,你想要的转换基本都提供,初学者可以参照实现转换出来的基本样子,达到通用级别,需要自己花很多功夫。此开源代码是基于poi和itext(pdf)的转换方式。
2) https://gitee.com/kekingcn/file-online-preview这是开源中国提供的一个源码,基于jodconverter,原理是调用windows,另存为的组件,实现转换。
3) 收费产品例如【永中office】【office365】【idocv】、【https://downloads.aspose.com/words/java】
2. 转换思路
自己在尝试过很多后,也与永中集成了文档转换,发现,要想完成预览的品质,必须的做二次渲染。毕竟永中做了十几年文档转换我们不能比的,自己琢磨后,发现一个勉强靠谱的思路,doc和docx都转换为pdf实现预览。都是在基于poi的基础上。
2.1. Doc转换pdf
1) Doc转换为xml
/**
* doc转xml
*/
public String toXML(String filePath){ try{ POIFSFileSystem nPOIFSFileSystem = new POIFSFileSystem(new File(filePath)); HWPFDocument nHWPFDocument = new HWPFDocument(nPOIFSFileSystem);
WordToFoConverter nWordToHtmlConverter = new WordToFoConverter(
DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
PicturesManager nPicturesManager = new PicturesManager() { public String savePicture(byte[] arg0, PictureType arg1,String arg2, float arg3, float arg4) {
//file:///F://20.vscode//iWorkP//temp//images//0.jpg
//System.out.println("file:///"+PathMaster.getWebRootPath()+ java.io.File.separator + "temp"+java.io.File.separator+"images" + java.io.File.separator + arg2);
// return "file:///"+PathMaster.getWebRootPath()+java.io.File.separator +"temp"+java.io.File.separator+"images" + java.io.File.separator + arg2;
return "file:///"+PathMaster.getWebRootPath()+java.io.File.separator +"temp"+java.io.File.separator+"images" + java.io.File.separator + arg2;
}
}; nWordToHtmlConverter.setPicturesManager(nPicturesManager);
nWordToHtmlConverter.processDocument(nHWPFDocument);
String nTempPath = PathMaster.getWebRootPath() + java.io.File.separator + "temp" + java.io.File.separator + "images" + java.io.File.separator;
File nFile = new File(nTempPath); if (!nFile.exists()) {
nFile.mkdirs();
}
for (Picture nPicture : nHWPFDocument.getPicturesTable().getAllPictures()) {
nPicture.writeImageContent(new FileOutputStream(nTempPath + nPicture.suggestFullFileName()));
}
Document nHtmlDocument = nWordToHtmlConverter.getDocument();
OutputStream nByteArrayOutputStream = new FileOutputStream(OUTFILEFO);
DOMSource nDOMSource = new DOMSource(nHtmlDocument);
StreamResult nStreamResult = new StreamResult(nByteArrayOutputStream); TransformerFactory nTransformerFactory = TransformerFactory.newInstance();
Transformer nTransformer = nTransformerFactory.newTransformer(); nTransformer.setOutputProperty(OutputKeys.ENCODING, "GBK");
nTransformer.setOutputProperty(OutputKeys.INDENT, "YES");
nTransformer.setOutputProperty(OutputKeys.METHOD, "xml"); nTransformer.transform(nDOMSource, nStreamResult); nByteArrayOutputStream.close(); return ""; }catch(Exception e){
e.printStackTrace();
}
return "";
}
2) Xml转换为pdf
这里我是使用fop通过xml转换为pdf,也是最近欣喜的一个发现,poi官网推荐的我一直没去仔细看,里面的架包和永中的很多高清包,一模一样,现在貌似路子对了。有兴趣者研究去吧。我的源码已经在githubhttps://github.com/liuxufeijidian/file.convert.master/tree/master上面,环境已经配置好,需要准备好doc和docx文档即可。
/*
* xml 转pdf
*/
public void xmlToPDF() throws SAXException, TransformerException{
// Step 1: Construct a FopFactory by specifying a reference to the configuration file
// (reuse if you plan to render multiple documents!)
FopFactory fopFactory = null;
new URIResolverAdapter(new URIResolver(){
public Source resolve(String href, String base) throws TransformerException {
try {
URL url = new URL(href);
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", "whatever");
return new StreamSource(connection.getInputStream());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
OutputStream out = null;
try { fopFactory = FopFactory.newInstance(new File(CONFIG)); // Step 2: Set up output stream.
// Note: Using BufferedOutputStream for performance reasons (helpful with FileOutputStreams). out = new BufferedOutputStream(new FileOutputStream(OUTFILEPDF)); // Step 3: Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out); // Step 4: Setup JAXP using identity transformer
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(); // identity transformer // Step 5: Setup input and output for XSLT transformation
// Setup input stream
Source src = new StreamSource(OUTFILEFO); // Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
// Step 6: Start XSLT transformation and FOP processing
transformer.transform(src, res); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//Clean-up
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
2.1.3
很多时候我们是使用word直接转的html,但是需要自己写二次渲染的代码,较为复杂,我是使用迂回方法,doc转xml,再用xml转换pdf,转换出来的pdf用pdfjs渲染即可实现和浏览器打开一样的预览,pdfjs预览方法详情见https://blog.csdn.net/liuxufeijidian/article/details/82260199
ending:大家都想看效果如何,https://github.com/litter-fish/transform,github获取改源码,配置好doc和docx文档即可实现转换,接下来会继续努力不间断优化和更新文档转换。
文件在线预览doc,docx转换pdf(一)的更多相关文章
- 怎么在线预览.doc,.docx,.ofd,.pdf,.wps,.cad文件以及Office文档的在线解析方式。
前言 Office文件在线预览是目前移动化办公的一种新趋势.Office在线预览指的是Office系列的文件在线查看而不依附域客户端的存在.在浏览器或者浏览器控件中可以预览查看Word.PDF.Exc ...
- 【ASP.NET 进阶】仿百度文库文档在线预览(支持格式.pdf,.doc,docx,xls,xlsx,.ppt,pptx)
在[ASP.NET]PDF文件在线预览(类似百度文库)基础上进行了office文件到pdf文件的转换,然后在显示出来,效果如下: 问题说明: 1.请通过以下方式添加 Office COM 组件. 2. ...
- 网页中动态嵌入PDF文件/在线预览PDF内容https://www.cnblogs.com/xgyy/p/6119459.html
#网页中动态嵌入PDF文件/在线预览PDF内容# 摘要:在web开发时我们有时会需要在线预览PDF内容,在线嵌入pdf文件: 问题1:如何网页中嵌入PDF: 在网页中: 常用的几种PDF预览代码片段如 ...
- 使用PDF.JS实现pdf文件在线预览时,报文件被损坏的错误
首先大概说明一下问题出现的背景:我用PDF.JS实现文件在线预览,参考网上的办法,在jsp文件中使用 <iframe src="<c:url value="js/gen ...
- C# WebAPI 文件在线预览
最近在写一个移动端API接口,其中有一个需求:接口返回附件url地址让手机端调用实现文件在线预览.大体实现思路:把doc.xls等文本格式文件转换为pdf,转换后的pdf文件存放在服务器上面,方便第二 ...
- kkfileview v2.0 发布,文件在线预览项目方案
kkfileview文件在线预览 此项目为文件文档在线预览项目解决方案,项目使用流行的spring boot搭建,易上手和部署,部署好后可以独立提供预览服务,使用http接口访问,不需要和应用集成,具 ...
- 基于开源方案构建统一的文件在线预览与office协同编辑平台的架构与实现历程
大家好,又见面了. 在构建业务系统的时候,经常会涉及到对附件的支持,继而又会引申出对附件在线预览.在线编辑.多人协同编辑等种种能力的诉求. 对于人力不是特别充裕.或者项目投入预期规划不是特别大的公司或 ...
- asp.net word ecxel类型文件在线预览
asp.net word ecxel类型文件在线预览 首先得引用COM: Microsoft Excel 10 Object Library Microsoft Word 10 Object Libr ...
- 前端实现文件在线预览txt,pdf,doc,xls,ppt几种格式
做法就是使用iframe标签 1.text,pdf的文件预览 <iframe class="filename" :src="文件的地址" width='1 ...
随机推荐
- JS数组的基本操作方法
一.concat()concat() 方法用于连接两个或多个数组.该方法不会改变现有的数组,仅会返回被连接数组的一个副本. var arr1 = [1,2,3];var arr2 = [4,5];va ...
- jmeter学习笔记--线程组
jmeter组件:线程组 是什么? 进程:一个正在执行的程序对应一个进程 线程:进程中的执行线索(一个进程中有多个执行线索) 线程组:按照线程性质对线程进行分组 并发执行:多个线程同时进行 特点 ...
- 开源虚拟化KVM(一)搭建部署与概述
一,KVM概述 1.1 虚拟化概述 在计算机技术中,虚拟化意味着创建设备或资源的虚拟版本,如服务器,存储设备,网络或者操作系统等等 [x] 虚拟化技术分类: 系统虚拟化(我们主要讨论的反向) 存储虚拟 ...
- file常用功能
构造方法 File(String pathname):将指定的路径名转换成一个File对象 File f = new File("D:\\a\\b.txt"); File(Stri ...
- Excel基本操作
一.excel公式下拉 1.选择最长一列excel ,按CTRL+↓移到最后一个单元格,2光标移到下拉公式的那一列,3.再按CTRL+SHIFT+↑,4.再按CTRL+D 二.输入身份证号等长数字 方 ...
- MongoDB集群的搭建
一.环境准备 1.Centos7 2.mongodb3.4.10 3.三台机器IP分别是:192.168.1.100.192.168.1.135.192.168.1.136 二.mongdb数据库的安 ...
- 用eclipse创建动态web项目手动生成web.xml方法
建一个web项目,后来在用到web.xml文件时,才发现项目创建时没有自动创建web.xml文件. 在创建的项目上单击右键,然后单击java EE Tools下的用红线圈住的地方,然后查看你的WEB- ...
- poj2182(线段树求序列第k小)
题目链接:https://vjudge.net/problem/POJ-2182 题意:有n头牛,从1..n编号,乱序排成一列,给出第2..n个牛其前面有多少比它编号小的个数,记为a[i],求该序列的 ...
- Git简单生成生成公钥和私钥方法
Git简单生成生成公钥和私钥方法 Git配置 Git安装完之后,需做最后一步配置.打开git bash,分别执行以下两句命令 git config --global user.name “用户名” g ...
- CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-5安装JDK及安装mysql数据库
1.安装JDK 可以不用卸载自带的openjdk,配好环境变量即可. 下载文件:jdk-8u151-linux-x64.tar.gz 附:JDK各版本下载地址:https://www.oracle.c ...