文件在线预览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, .xls, .xlsx, .ppt, .pptx 直接批量转换成pdf文件,完成绿色和免费,转换成pdf的页面格式和源文件打印预览格式相同,win 7/win 10 .net 4.0 以上版本运行。
支持常见的doc、docx、txt、ppt、pptx、xls、xlsx等转换成PDF文件预览 需要两个参数 oldPath newPath. oldPath为需要转换的文件所在位置,newPath为转换后存放的地址,文件内有main方法
源码可以帮助那些需要在线预览文件或正在寻找 office 转 pdf 文件预览的同学 实现功能如下: 多格式转换为 PDF 格式 OFFICE 转换为 PNG 格式 在线预览文件 手机预览查看文件 已支持格式如下: 图片预览:.gif、bmp、...
通过快捷稳定的全能PDF转Word转换器,用户可以轻松将不可编辑的PDF文件转换成可编辑的doc或docx格式。转换效果好,可以完整保留原PDF文件内文本格式。 全能PDF转Word转换器使用方法: 1. 安装并且登录全能PDF转...
不需要打开.doc, .docx, .xls, .xlsx, .ppt, .pptx 直接批量转换成pdf文件,完成绿色和免费,转换成pdf的页面格式和源文件打印预览格式相同,win 7/win 10 .net 4.0 以上版本运行。
可转换doc,docx,ppt,pptx,txt,xls,xlsx,pdf类型的文件格式,转换类自动打开openoffice,在线预览功能,代码齐全。可套用在需要的项目上。
以极高的速度转换PDF文件。它可以转换大型PDF文件,没有任何问题。 内置的PDF Reader可让您在转换前快速预览应用程序中的PDF文件。 输出格式的丰富选择 使用所有常见的输出文档格式满足所有转换需求,包括: ...
doc | docx | xlsx | xls | ppt | pptx 等 pdf文件 使用方法 拉取项目到本地 下载并安装openoffice,在项目application.yml文件中配置openoffice的安装路径 造一个文件的下载地址,例如七牛云外链: (文件上传到七...
该转换工具用来将pdf文件转换成swf文件。改工具既可以安装使用实现文件转换,也拷贝安装后Program Files下的Swftools文件夹放到工程中,以绿色软件方式来使用。转换命令将在FileConverterUtil.java中特别指明。 第四...
文档格式转换 支持 wps、wpt、doc、docx、dot、txt、ppt、pps、pptx、ppsx、dps、dpt、pot、uof、et、ett、xls、xlsx、xlt、uof、prn、csv、pdf
预览和提取文件附件和注释 记录和播放音频注释 添加注释,评论和云注释 使用辅助线将对象对齐 添加页码,书签,页眉和页脚,行号和水印 对扫描的文档执行OCR(光学字符识别) 可见的OCR文本层,用于对扫描页面中的...
Adobe Acrobat pdf,Microsoft Office 文档 doc、xls、ppt (Office 97-2003)、docx、xlsx、pptx,图片文件(参见列表),视频和音频文件(如果系统内安装有合适的解码器,则支持所有文件)均可预览,以评价恢复能力...
Java程序中可以利用OpenOffice,将办公文档(包括doc、docx、xls、xlsx等)转换为PDF格式的文档。由于PDF格式的文档可以达到在线预览的效果,而Office办公文档实现在线预览相对来说会比较麻烦,但曲线救国,通过文档...
1. 支持多种文档转换预览,文档被转换为H5或图片格式,文字放大无失真,响应速度更快速对SEO更友好,排名更强势,格式支持 doc,docx,ppt,pptx,pdf,txt,zip,mp3,mp4,jpg,png,bmp,gif 等多种文档及多媒体文件格式 ...
打开和保存 Microsoft Word 6.0 到 2013 版本的 DOC 和 DOCX 文件,与 Word 效果完全一致,支持有密码保护的文件。 文件选择框中实时预览格式更改 支持 PDF 文件中的表格、标签和书签 导出为 EPUB 格式电子书 版本...