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的更多相关文章

  1. java调用com组件将office文件转换成pdf

    在非常多企业级应用中都涉及到将office图片转换成pdf进行保存或者公布的场景,由于pdf格式的文档方便进行加密和权限控制(类似于百度文库).总结起来眼下将office文件转换 成pdf的方法主要有 ...

  2. dvi文件和将dvi文件转换成pdf格式

    dvi文件和将dvi文件转换成pdf格式 Latex只能把tex文件编译成dvi文件, 在cmd 中: 使用xdvi查看dvi格式的文件 若用texstudio编辑tex文件,则可直接将已编译成功的. ...

  3. 在Linux下将HTML文件转换成PDF文件

    今天要写一个上交的作业,本来是想用Office Word来写的,但是,我的Office貌似不能用了,但是,Linux下的LibreOffice写出的文档,在打印的时候是经常出现乱码的.所以,后来想到可 ...

  4. C# 将PowerPoint文件转换成PDF文件

    PowerPoint的优势在于对演示文档的操作上,而用PPT查看资料,反而会很麻烦.这时候,把PPT转换成PDF格式保存,再浏览,不失为一个好办法.在日常编程中和开发软件时,我们也有这样的需要.本文旨 ...

  5. 关于DWG文件转换成PDF

    最近有这样一个需求,客户会提供DWG文件,因为DWG文件是不能直接在网页上显示的,所以必须对他做处理,要求是转换成PDF格式.我查了很久的资料,很多都是基于C#和.NET的方法,而且都是说的很模糊,不 ...

  6. ofd格式文件转换成pdf格式的方法

    ofd格式文件很多人还比较陌生,很多人接收到文件都不知如何打开阅读,把文件发给对方,还需要对方安装个专门的阅读软件,我们还有另一个办法,就是将OFD文件转换为PDF格式文件,然后把PDF格式文件发给对 ...

  7. 【文件】使用jacob将word转换成pdf格式

    使用jacob将word转换成pdf格式   1.需要安装word2007或以上版本,若安装07版本学确保该版本已安装2downbank0204MicrosoftSaveasPDF_ XPS,否则安装 ...

  8. C#.net word excel powerpoint (ppt) 转换成 pdf 文件

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  9. ASP.NET将word文档转换成pdf的代码

    一.添加引用 using Microsoft.Office.Interop.Word; 二.转换方法 1.方法 C# 代码 /// <summary> /// 把Word文件转换成pdf文 ...

随机推荐

  1. java php 3des实现

    php.java.android.ios通用的3des方法:http://blog.csdn.net/zcjwsrf/article/details/47659137 PHP使用3DES算法加密解密字 ...

  2. org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [cn.facekee.cms.entity.CmsFansgroup#195]

    刚开始报错还是报的稀奇古怪的错误,让我纠结了好久,再三检查报错的位置,发现并没有错误,最后认真分析查看每行报错的信息才找到如题所述的错误!!!!! 报这种错误的原因可能是POJO映射文件中的字段和数据 ...

  3. Unicode与UTF-8互转(c语言和lua语言)

    1. 基础 1.1 ASCII码 我们知道, 在计算机内部, 全部的信息终于都表示为一个二进制的字符串. 每个二进制 位(bit)有0和1两种状态, 因此八个二进制位就能够组合出 256种状态, 这被 ...

  4. A guide to analyzing Python performance

    来源:http://www.huyng.com/posts/python-performance-analysis/ While it's not always the case that every ...

  5. Python 爬取盗墓笔记的标题,章节,章节名称

    # coding:utf-8import requestsimport jsonfrom bs4 import BeautifulSoup user_agent = 'Mozilla/5.0 (Win ...

  6. IOS时间与日期处理

    本文转载至 http://blog.sina.com.cn/s/blog_9cd1705d0102v5x4.html   主要有以下类: NSDate -- 表示一个绝对的时间点NSTimeZone ...

  7. filter和find区别,元素遍历

    转 filter和find区别 find()会在当前指定元素中查找符合条件的子元素,是对它的子集操作,而filter()则是在当前指定的元素集合中查找符合条件的元素,是对自身集合元素进行筛选. HTM ...

  8. Sass mixin 使用css border属性画三角形

    To be finished. //triangle@mixin css-triangle ($direction: "down", $size: 20px, $color: #0 ...

  9. <2013 06 29> In Deutschland. Thinking in Mechanism, EE, CS, etc.

    一 在德国近一个月了,主要去了慕尼黑周边的几个景点,天鹅城堡啊,国王湖啊,然后就是在市区逛,玛丽安广场,伊萨河,英国公园,德意志博物馆... ... 总体的印象是非常好的,只是自己不怎么懂德语,但这里 ...

  10. InnoDB的三个关键特性

    一.插入缓冲(insert buffer) 对于非聚集索引的插入和更新,不是每一次直接插入索引页中,而是首先判断插入的非聚集索引页是否在缓冲池中,如果在,则直接插入,否则, 先放入一个插入缓冲区中.好 ...