/**
*
*/
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
LHao0529
- 粉丝: 1
- 资源: 12
最新资源
- 基于贵兰在线平台开展《数字素养通识课》教学-学生端操作手册.pdf
- Fluent 脚本编译并加载UDF
- YOLOv10在智能语音识别中的创新应用与代码实现
- 使用Flet0.24实现的自定义圆形头像加载本地图片的自定义组件CustomCircleAvata示例源代码IDE运行和调试通过
- SY6970参考设计程序
- NTD20N06LG-VB一款N-Channel沟道TO252的MOSFET晶体管参数介绍与应用说明
- 处理DOTA数据集所需的全部代码(DOTA的YOLO格式)
- MATLAB中创建动态或实时更新图形的全面指南
- YOLOv10在智能内容推荐中的创新应用与代码实现
- FQD20N06LTF-VB一款N-Channel沟道TO252的MOSFET晶体管参数介绍与应用说明
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
前往页