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. Genymotion Android模拟器与fiddler 数据包拦截

    Genymotion: https://www.genymotion.com/fun-zone/ https://www.genymotion.com/account/create/ cls清空记录 ...

  2. [ACM] hdu 3923 Invoker (Poyla计数,高速幂运算,扩展欧几里得或费马小定理)

    Invoker Problem Description On of Vance's favourite hero is Invoker, Kael. As many people knows Kael ...

  3. o.a.catalina.core.AprLifecycleListener : An incompatible version [1.2.7] of the APR based Apache Tomcat Native library is installed, while Tomcat requires version [1.2.14]

    1.错误信息提示: 2019-04-16 22:02:05.811 ERROR 18112 --- [           main] o.a.catalina.core.AprLifecycleLi ...

  4. Android无线测试之—UiAutomator UiDevice API介绍二

    按键与KEYCODE使用 一.手机常见按键: 1)HOME 主屏幕键 2) MENU 菜单键 3) BACK 返回键 4) VOLUME_UP 音量加键 5) VOLUME_DOWN 音量减键 6) ...

  5. hihocoder 1279(状压)

    坑爹的题目.不过不能说不是一道挺好的题目. 坑主要坑在,妹的我一样的复杂度,写的姿势略差了点然后就一直超时. 比赛的时候我还直接就看错题目,把AND运算看成了OR...还敲完交了一发. 这题很容易想到 ...

  6. golang 模板(template)的常用基本语法

    1. 模板 在写动态页面的网站的时候,我们常常将不变的部分提出成为模板,可变部分通过后端程序的渲染来生成动态网页,golang提供了html/template包来支持模板渲染. 这篇文章不讨论gola ...

  7. 基于jquery 移动插件的实现

    引用谢灿勇 地址  http://www.cnblogs.com/st-leslie/p/6002148.html 一个思路分析:大致上实现的思路有以下两种. 一.判断块是否被按下(mousedown ...

  8. ASIHttprequest 报错

    (void)requestReceivedResponseHeaders:(NSMutableDictionary *)newResponseHeaders { if ([self error] || ...

  9. 【转】【Spring实战】Spring注解配置工作原理源码解析

    一.背景知识 在[Spring实战]Spring容器初始化完成后执行初始化数据方法一文中说要分析其实现原理,于是就从源码中寻找答案,看源码容易跑偏,因此应当有个主线,或者带着问题.目标去看,这样才能最 ...

  10. 洛谷 P2721 小Q的赚钱计划

    洛谷 这大概是我见过最水的紫题吧- 洛谷标签赞一个! 题意:你有一年时间,把10w元存银行变成更多钱,在特定时间区间内,你会有一些利息,不过不可中途退出. 直接dp:st[i]表示区间左端点,ed[i ...