代码:
public class HtmlToPdfUtil {
private Logger logger = Logger.getLogger(this.getClass());
OpenOfficeConnection con = null;
//文件之间的转换
public void convert(File sourceFile, File _targetFile) {
try {
if (con == null) {
con = new SocketOpenOfficeConnection(ip, port);
}
con.connect();
DocumentConverter converter = new StreamOpenOfficeDocumentConverter(con);
//DocumentConverter converter = new OpenOfficeDocumentConverter(con);
converter.convert(sourceFile, _targetFile);
con.disconnect();
} catch (Exception e) {
logger.error("OpenOffice异常", e);
try {
if (con != null) {
con.disconnect();
con = null;
}
} catch (Exception e1) {
logger.error(e);
}
}
}
// html文件生成pdf文件
public boolean htmlToPdf(String htmlPath,String pdfPath) {
boolean flag = false;
HtmlToPdfUtil openoffice = new HtmlToPdfUtil();
openoffice.convert(new File(htmlPath), new File(pdfPath));
flag = true;
return flag;
}
/**
* 删除文件的工具
* @param url
*/
public void deleFile(String url){
File file = new File(url);
if(file.exists()){
// file.delete();
}
}
/**
* 思路:
* 1、生成静态页面落地,返回生成静态页面的url;
* 2、指定生成pdf文件的url;
* 3、讲html转换为pdf;
* 4、删除生成的静态页面,留取需要的pdf文件;
* 结束
*/
public static void main(String[] args) {
//生成的静态页的文件目录
String inPath = "C:\\html\\hello.html";//输入路径(输入html文件的路径)
//转换成pdf文件的目录
String outPath = "C:\\pdf\\hello.pdf";//输出路径(转换为pdf的路径)
HtmlToPdfUtil htw = new HtmlToPdfUtil();
htw.convert(new File(inPath), new File(outPath));
//删除静态页
htw.deleFile(inPath);
}
/****************************** 测试**********************************************/
public void setIp(String ip) {
this.ip = ip;
}
public void setPort(int port) {
this.port = port;
}
private String ip = "172.16.2.49";
//是不是用队列模拟生成端口号,因为主要是连接端口号的问题。
private int port = 8100;
}
报错: