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文 ...
随机推荐
- PHP实现自己活了多少岁
1.mktime()函数的功能 2.代码: $birth = mktime(0,0,0,10,2,1992);//出生的时间戳 $time = time();//当前的时间戳 $age = floor ...
- linux 复制粘贴
如果想把文件内的第三行内容黏贴到第十五行: 1.进入你的文件,(处于命令模式,而不是编辑模式) 2.将你的光标移到你的第三行的开头(用方向键) 3,按下v,(你发现切换到可视模式了),按方向键,知道选 ...
- WINDOWS 7.1 SDK 安装失败
错误提示: Please refer to Samples\Setup\HTML\ConfigDetails.htm document for further information. 原因:本机上安 ...
- APP全局异常捕获,并保存本地文件
public class CrashHandler implements Thread.UncaughtExceptionHandler { public static final String TA ...
- 深入理解line
什么是行间距? 古时候我们使用印刷机来出来文字.印刷出来的每个字,都位于独立的一个块中. 行间距,即传说中控制两行文字垂直距离的东东.在CSS中,line-height被用来控制行与行之间垂直距离. ...
- hibernate createQuery查询传递参数的两种方式
String hql = "from InventoryTask it where it.orgId=:orgId"; Session session = getSession() ...
- 【BZOJ2384】[Ceoi2011]Match KMP
[BZOJ2384][Ceoi2011]Match Description 作为新一轮广告大战的一部分,格丁尼亚的一家大公司准备在城市的某处设置公司的标志(logo).公司经理决定用一些整栋的建筑来构 ...
- Python——用正则求时间差
如有求时间差的需求,可直接套用此方法: import time true_time=time.mktime(time.strptime('2017-09-11 08:30:00','%Y-%m-%d ...
- Linux考试题附答案
一.选择题 1.在登录Linux时,一个具有唯一进程ID号的shell将被调用,这个ID是什么(B)? A.NID B.PID C.UID D.CID 2.下面哪个目录存放用户密码信息(B) A./b ...
- Android系统移植与调试之------->如何修改Android手机显示的4G信号强度的格子数
在修改显示的信号强度之前,先了解一下什么是dB,什么是dBm? 1.dB dB是一个表征相对值的值,纯粹的比值,只表示两个量的相对大小关系,没有单位,当考虑甲的功率相比于乙功率大或小多少个dB时, 按 ...