html如何word转pdf文件,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) throwsFileNotFoundException, 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= newcom.itextpdf.text.Document(

PageSize.A4, a1, a2, a3, a4);

PdfWriter pdfwriter= PdfWriter.getInstance(document, newFileOutputStream(pdfFile));

document.open();

CSSResolver cssResolver= newStyleAttrCSSResolver();//HTML

XMLWorkerFontProvider fontProvider = newXMLWorkerFontProvider(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= newCssAppliersImpl(fontProvider);

HtmlPipelineContext htmlContext= newHtmlPipelineContext(cssAppliers);

htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());final String IMG_PATH = pdfFile.substring(0, pdfFile.indexOf("/temp")) + "temp/"; //指定html文件的图片路径

htmlContext.setImageProvider(newAbstractImageProvider() {publicString getImageRootPath() {returnIMG_PATH;

}

});//Pipelines

PdfWriterPipeline pdf = newPdfWriterPipeline(document, pdfwriter);

HtmlPipeline html= newHtmlPipeline(htmlContext, pdf);

CssResolverPipeline css= newCssResolverPipeline(cssResolver, html);//XML Worker

XMLWorker worker = new XMLWorker(css, true);

XMLParser p= newXMLParser(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 booleanstartOfficeService() {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后台服务启动成功<<

}

privatecom.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection connectOfficeService() {

System.out.println("开始准备连接OFFICE服务...>>>");

com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection connection= newcom.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服务连接成功<<

}

if(startOfficeService()) {

OpenOfficeConnection conn=connectOfficeService();if (conn != null) {

DocumentConverter convert= newOpenOfficeDocumentConverter(conn);

convert.convert(new File(rootPath + "/temp/resulted.html"), new File(rootPath + "/temp/" + reportName + ".doc"));

conn.disconnect();

}

}

原文:http://www.cnblogs.com/hongxf1990/p/4536917.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值