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. linux 学习笔记1

    1.IDE硬盘        /dev/hd[a-d]                   IDE1  主  /dev/hda   从 /dev/hdb          IDE2        主 ...

  2. mongo-connector来同步mongo

    个人博客:https://blog.sharedata.info/ 最近需要做mongo之间的同步,因此还是选择之前的工具mongo-connectorgitHub文档:https://github. ...

  3. T-SQL 合并多行数据显示到一行

    思路: 自连接,使用For XML Path('')和STUFF函数 SELECT * FROM STUDENT Name                      Team------------- ...

  4. Palindrome Function

    Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)Total Submissio ...

  5. es站内站内搜索笔记(一)

    es站内站内搜索笔记(一) 第一节: 概述 使用elasticsearch进行网站搜索,es是当下最流行的分布式的搜索引擎及大数据分析的中间件,搜房网的主要功能:强大的搜索框,与百度地图相结合,实现地 ...

  6. android菜鸟学习笔记26----Android广播消息及BroadcastReceiver

    1.广播类型: Android中的广播有两种类型:标准广播和有序广播.其中,标准广播是完全异步发送的广播,发出之后,几乎所有的广播接收者都会在同一时刻收到这条广播消息,因而,这种类型的广播消息是不可拦 ...

  7. 【转】Linux系统上安装MySQL 5.5 rpm

    1.准备工作 从MySQL官网上分别下载mysql服务器端于客户端包. 如: MySQL-server-5.5.15-1.linux2.6.x86_64.rpm和MySQL-client-5.5.15 ...

  8. LR回放https协议脚本失败:[GENERAL_MSG_CAT_SSL_ERROR]connect to host "XXX" failed:[10054] Connection reset by peer [MsgId:MERR-27780]

    最近做一个负载均衡项目的性能测试,使用LR录制脚本协议为https协议,回放脚本时出现报错: [GENERAL_MSG_CAT_SSL_ERROR]connect to host "XXX& ...

  9. mac 识别压缩文件类型

    file -z b.zip

  10. 使用远程管理工具Xshell

    介绍 目前常用的远程管理工具 Xshell -- 免费版/收费版 SecureCRT Putty 安装Xshell 选择免费版即可 连接服务器 连接成功 Xshell连接不上排查流程 还有可能是Lin ...