您的位置:首页 > Web前端 > HTML

html页面转换成PDF文件

2016-01-18 14:30 555 查看
package pdftest;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import com.lowagie.text.pdf.BaseFont;

/**
* 将html页面转换成PDF文件
* 应用包 itext-2.0.8.jar core-renderer-R8.jar
* 不支持中文,若支持中文需对内容进行改动
*
*/
public class RendererTest {

public static void main(String[] args) {
// TODO Auto-generated method stub

try{

ITextRenderer renderer = new ITextRenderer();

//			String blogURL = "http://;
String blogURL = "http://";
//指定模板地址
//			renderer.setDocument("http://");

//根据网址,获取网页内容
String htmlBody = getHtmlContent(blogURL);

//将网页内容进行格式校验及调整,以便于renderer能够解析,根据内容调整
htmlBody = htmlBody.replaceAll("<br>", "<br/>");
htmlBody = "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"zh-CN\"><head>"+htmlBody.substring(htmlBody.indexOf("-->")+3);
htmlBody = htmlBody.substring(0, htmlBody.indexOf("</html>")+7);
htmlBody = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" + htmlBody;

System.out.println(htmlBody);

//将调整完内容设置到renderer中
renderer.setDocumentFromString(htmlBody);

//字符处理,本处需要处理中文
ITextFontResolver fontResolver = renderer.getFontResolver();
//			if (StringUtils.isOSWindow())
//				fontResolver.addFont("C:/Windows/Fonts/ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//			else
fontResolver.addFont("/leo-work/workspace/mytest/lib/msyh.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
renderer.layout();

String pdfFile2 = "/Users/leo/Desktop/a6.pdf";
//			OutputStream os = response.getOutputStream();      //输出到网页
OutputStream os = new FileOutputStream(pdfFile2);  //输出到文件

//根据renderer内容,创建PDF文件
renderer.createPDF(os);

//关闭输出柳
os.close();

}catch(Exception e){
System.out.println(e.getMessage());
}
}

/**
* 通过网址获取网页内容
* @param htmlurl
* @return
*/
public static String getHtmlContent(String htmlurl) {
URL url;
String temp;
StringBuffer sb = new StringBuffer();
try {
url = new URL(htmlurl);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));// 读取网页全部内容
while ((temp = in.readLine()) != null) {
sb.append(temp);
}
in.close();
} catch (final MalformedURLException me) {
System.out.println("你输入的URL格式有问题!");
me.getMessage();
} catch (final IOException e) {
e.printStackTrace();
}
return sb.toString();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: