/**
*
*/
package com.test;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.pdf.BaseFont;
/**
* @author L.Hao
*
*/
public class HtmltoPDF {
public static void main( String[] args ) throws Exception{
URL url = new URL("http://www.baidu.com");
String urlsource = getURLSource(new File("D:/TDDOWNLOAD/SunWater1.html"));
//String urlsource = getURLSource(new File("D:/TDDOWNLOAD/1111.html"));
//htmlToPdf();
//htmlToPdf2(urlsource);
StringBuffer html = new StringBuffer();
// DOCTYPE 必需写否则类似于 这样的字符解析会出现错误
html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">").
append("<head>")
.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />")
.append("<style type=\"text/css\" mce_bogus=\"1\">body {font-family: SimSun;}</style>")
.append("</head>")
.append("<body>");
html.append("<div>你好</div>");
html.append("</body></html>");
String jsonStr="{\"Qw_Value\":\"CN_1\",\"Cw_Value\":\"哈尔滨\",\"HotWaterTemp\":\"15838\",\"ColdWaterTemp\":\"45.68\",\"Jt_Value\":\"1.14\",\"f_value" +
"\":\"12703\",\"CollectingEfficiency\":\"7.79\",\"HeatLossRate\":\"3\",\"CollectorType\":\"3.57\",\"Ac_Value\":\"3.53337\"}";
Map<String, String> map =new HashMap<String, String>(); //JSONObject.fromObject(jsonObject);
map=json2Map(jsonStr, map);
urlsource = parseHtml(urlsource,map);
htmlToPdf2(urlsource);
//htmlToPdf3();
}
// 不支持中文
public static void htmlToPdf() throws Exception
{
String inputFile = "D:/TDDOWNLOAD/SunWater1.html";
String url = new File(inputFile).toURI().toURL().toString();
String outputFile = "D:/TDDOWNLOAD/SunWater1.pdf";
OutputStream os = new FileOutputStream( outputFile );
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();
}
// 支持中文
public static void htmlToPdf2(String str) throws Exception {
String outputFile = "D:/TDDOWNLOAD/demo_3.pdf";
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("C:/Windows/fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
StringBuffer html = new StringBuffer();
// DOCTYPE 必需写否则类似于 这样的字符解析会出现错误
html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">").
append("<head>")
.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />")
.append("<style type=\"text/css\" mce_bogus=\"1\">body {font-family: SimSun;}</style>")
.append("</head>")
.append("<body>");
html.append("<div>"+str+"</div>");
html.append("</body></html>");
renderer.setDocumentFromString(str);
// 解决图片的相对路径问题
renderer.getSharedContext().setBaseURL("file:/F:/WorkSpace/sunshine/WebRoot/");
renderer.layout();
renderer.createPDF(os);
System.out.println("======转换成功!");
os.close();
}
public static void htmlToPdf3() throws Exception{
String inputFile = "D:/TDDOWNLOAD/SunWater1.html";
String outFile = "D:/TDDOWNLOAD/test1111.pdf";
OutputStream os = null;
os = new FileOutputStream(outFile);
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("C:/Windows/fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
String url = new File(inputFile).toURI().toURL().toString();
System.out.println("=============url: "+url);
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
System.out.println("======转换成功!");
os.close();
}
/**
* 通过网站域名URL获取该网站的源码
* @param url
* @return String
* @throws Exception
*/
public static String getURLSource(URL url) throws Exception {
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = conn.getInputStream(); //通过输入流获取html二进制数据
byte[] data = readInputStream(inStream); //把二进制数据转化为byte字节数据
String htmlSource = new String(data);
return htmlSource;
}
/**
* 通过网站域名URL获取该网站的源码
* @param url
* @return String
* @throws Exception
*/
public static String getURLSource(File url) throws Exception {
//HttpURLConnection conn = (HttpURLConnection)url.openConnection();
InputStream inStream = new FileInputStream(url);
/*conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = conn.getInputStream();*/ //通过输入流获取html二进制数据
byte[] data = readInputStream(inStream); //把二进制数据转化为byte字节数据
String htmlSource = new String(data);
return htmlSource;
}
/**
* 把二进制流转化为byte字节数组
* @param instream
* @return byte[]
* @throws Exception
*/
public static byte[] readInputStream(InputStream instream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1204];
int len = 0;
while ((len = instream.read(buffer)) != -1){
outStream.write(buffer,0,len);
}
instream.close();
return outStream.toByteArray();
}
// 支持中文
public static String parseHtml(String str,Map