html文件转换成pdf和word
1、html文件转成pdf
采用jar包有itext-asian.jar、itextpdf-5.5.5.jar、itext-pdfa-5.5.5.jar、itext-xtra-5.5.5.jar,为了保持html页面的全部格式,需要进行相关设置,代码如下:
private boolean convert2Pdf(InputStream htmlInputStream, String pdfFile, String padding) throws FileNotFoundException, DocumentException {
String[] paddings = padding.split(",");
float a1 = (float) (Float.valueOf(paddings[0]) * 0.77); //为了设置pdf的上下左右页边距
float a2 = (float) (Float.valueOf(paddings[1]) * 0.77);
float a3 = (float) (Float.valueOf(paddings[2]) * 0.77);
float a4 = (float) (Float.valueOf(paddings[3]) * 0.77);
com.itextpdf.text.Document document = new com.itextpdf.text.Document(
PageSize.A4, a1, a2, a3, a4);
PdfWriter pdfwriter = PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
document.open();
CSSResolver cssResolver = new StyleAttrCSSResolver();
// HTML
XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontProvider.register("resources/garial.ttf", "Linrial"); //前面的ttf字体需要系统支持,后一个参数是html文件中的字体格式
fontProvider.register("resources/fzst_gb18030_20101201.ttf", "FZSongTi_GB18030");
fontProvider.register("resources/fzfangsong.ttf", "FZFangSong");
fontProvider.register("resources/fzheiti.ttf", "FZHeiTi");
fontProvider.register("resources/fzkaiti.ttf", "FZKaiTi");
fontProvider.register("resources/gtimes.ttf", "LinTimes");
CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
final String IMG_PATH = pdfFile.substring(0, pdfFile.indexOf("/temp")) + "temp/"; //指定html文件的图片路径
htmlContext.setImageProvider(new AbstractImageProvider() {
public String getImageRootPath() {
return IMG_PATH;
}
});
// Pipelines
PdfWriterPipeline pdf = new PdfWriterPipeline(document, pdfwriter);
HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
// XML Worker
XMLWorker worker = new XMLWorker(css, true);
XMLParser p = new XMLParser(worker);
try {
p.parse(htmlInputStream, Charset.forName("UTF-8"));
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
document.close();
}
return true;
}
使用方式
convert2Pdf(new ByteArrayInputStream(html.getBytes()), "/root/123.pdf", padding),其中html为html文件,需要转换为inputstream
2、html转为word
采用openoffice或者中标office,需要最新版本,jar包为jodconverter-2.2.2.jar、jodconverter-cli-2.2.2.jar需要把openoffice和web服务安装在同一个机器上
private boolean startOfficeService() {
final String paramStr = "\'socket,host=localhost,port=8100;urp;\'";
String pro = "/opt/neoshineoffice/program/soffice.bin -headless -accept="
+ paramStr;
System.out.println("---中标Office后台服务启动中...>>>");
try {
Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", pro }); // 创建一个office服务进程;
} catch (Exception e) {
System.out.println("中标Office后台服务启动失败<<<");
e.printStackTrace();
return false;
}
System.out.println("中标Office后台服务启动成功<<<");
return true;
}
private com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection connectOfficeService() {
System.out.println("开始准备连接OFFICE服务...>>>");
com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection connection = new com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection(
"localhost", 8100);
Boolean flag = false;
int count = 0; // 初始连接OFFICE失败后再次连接的次数;
while (!flag) {
try {
count++;
Thread.sleep((count + 1) * 1000);
connection.connect();
flag = true;
System.out.println(connection);
} catch (Exception e) {
System.out.println("OFFICE服务连接失败<<<");
System.out.println("第" + count + "次重启OFFICE服务连接");
if (count > 5) { // 连接OFFICE失败5次后,停止连接;
e.printStackTrace();
System.out.println("-----无法启动OFFICE服务连接,请检查OFFICE安装情况!----错误信息:"+ e.getMessage());
return null;
}
}
}
System.out.println("OFFICE服务连接成功<<<");
return connection;
}
if (startOfficeService()) {
OpenOfficeConnection conn = connectOfficeService();
if (conn != null) {
DocumentConverter convert = new OpenOfficeDocumentConverter(conn);
convert.convert(new File(rootPath + "/temp/resulted.html"), new File(rootPath + "/temp/" + reportName + ".doc"));
conn.disconnect();
}
}
html文件转换成pdf和word的更多相关文章
- java调用com组件将office文件转换成pdf
在非常多企业级应用中都涉及到将office图片转换成pdf进行保存或者公布的场景,由于pdf格式的文档方便进行加密和权限控制(类似于百度文库).总结起来眼下将office文件转换 成pdf的方法主要有 ...
- dvi文件和将dvi文件转换成pdf格式
dvi文件和将dvi文件转换成pdf格式 Latex只能把tex文件编译成dvi文件, 在cmd 中: 使用xdvi查看dvi格式的文件 若用texstudio编辑tex文件,则可直接将已编译成功的. ...
- 在Linux下将HTML文件转换成PDF文件
今天要写一个上交的作业,本来是想用Office Word来写的,但是,我的Office貌似不能用了,但是,Linux下的LibreOffice写出的文档,在打印的时候是经常出现乱码的.所以,后来想到可 ...
- C# 将PowerPoint文件转换成PDF文件
PowerPoint的优势在于对演示文档的操作上,而用PPT查看资料,反而会很麻烦.这时候,把PPT转换成PDF格式保存,再浏览,不失为一个好办法.在日常编程中和开发软件时,我们也有这样的需要.本文旨 ...
- 关于DWG文件转换成PDF
最近有这样一个需求,客户会提供DWG文件,因为DWG文件是不能直接在网页上显示的,所以必须对他做处理,要求是转换成PDF格式.我查了很久的资料,很多都是基于C#和.NET的方法,而且都是说的很模糊,不 ...
- ofd格式文件转换成pdf格式的方法
ofd格式文件很多人还比较陌生,很多人接收到文件都不知如何打开阅读,把文件发给对方,还需要对方安装个专门的阅读软件,我们还有另一个办法,就是将OFD文件转换为PDF格式文件,然后把PDF格式文件发给对 ...
- 【文件】使用jacob将word转换成pdf格式
使用jacob将word转换成pdf格式 1.需要安装word2007或以上版本,若安装07版本学确保该版本已安装2downbank0204MicrosoftSaveasPDF_ XPS,否则安装 ...
- C#.net word excel powerpoint (ppt) 转换成 pdf 文件
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- ASP.NET将word文档转换成pdf的代码
一.添加引用 using Microsoft.Office.Interop.Word; 二.转换方法 1.方法 C# 代码 /// <summary> /// 把Word文件转换成pdf文 ...
随机推荐
- redis 集群出现的错误
1 解决方法: 不用 Jedis jed =new jedis("192.168.56.101"); jed.set(key,value); 用 Set<HostAndPor ...
- Android开发:《Gradle Recipes for Android》阅读笔记1.5
这节讲的是如何如何添加JAVA依赖库. 默认的android项目有两个build.gradle文件,分别位于顶级目录,和应用自己的目录下(通常放在一个叫app的目录下面). gradle支持多种方式列 ...
- 【BZOJ1880】[Sdoi2009]Elaxia的路线 最短路+DP
[BZOJ1880][Sdoi2009]Elaxia的路线 Description 最近,Elaxia和w**的关系特别好,他们很想整天在一起,但是大学的学习太紧张了,他们 必须合理地安排两个人在一起 ...
- jquery,日常 记录知识 点 (选择器的引用类型)
1.标签引用$("p").$("input")例子 $("p").append( $("input").map(func ...
- PHP与ASP转义双引号的区别
PHP: 转义双引号:\" ASP: 转义双引号:"" PHP与ASP转义双引号的区别
- 小程序 欢迎页面 navigateTo和tabBar不能同时指向一个路径
小程序navigateTo和tabBar不能同时指向一个路径 wx.navigateTo和wx.redirectTo不允许跳转到tabBar页面,只能用wx.switchTab跳转到tabBar页面. ...
- Ubuntu14.04下Nginx反向代理Odoo域名
安装nginx sudo apt-get install -y nginx 修改配置文件 vi /etc/nginx/nginx.conf #注释掉下面这行代码 #include /etc/nginx ...
- node.js及node-inspector的调试方法
1.先运行 $ node --debug-brk test.js 2.再在新的窗口运行: $ node-inspector 3.再打开Chrome浏览器输入node-inspector提示的地址,就会 ...
- 源码编译搭建LAMP
环境版本信息: RHEL 5.3 Apache / 2.4.16 PHP / 5.4.45 mysql-5.5.45 源代码编译 安装方式 1: configure 配置 以及定制我们的软件包 2: ...
- Android:日常学习笔记(2)——分析第一个Android应用程序
Android:日常学习笔记(2)——分析第一个Android应用程序 Android项目结构 整体目录结构分析 说明: 除了APP目录外,其他目录都是自动生成的.APP目录的下的内容才是我们的工作重 ...