htmlpdf

一个是xhtmlrenderer<br>https://xhtmlrenderer.dev.java.net/<br>lgpl协议<br><br>另外一个是<br>http://www.allcolor.org/yahpconverter/<br>lgpl协议<br><br>都是基于itext的
 
2010-9-19注:
开始用yahpconverter发现好像不支持标签,而且用起来很费劲,改用xhtmlrenderer感觉很舒服。
 
public class htmltopdfutil {	private htmltopdfutil() {	}	private static configuration cfg = new configuration();// freemarks	static {		cfg.setobjectwrapper(new defaultobjectwrapper());		cfg.setdefaultencoding("utf-8");		try {			cfg.setdirectoryfortemplateloading(new file(servletactioncontext					.getrequest().getrealpath("/web-inf/template/")));		} catch (ioexception e) {			throw new runtimeexception("设置模板路径错误!");		}	}	public static inputstream convert(string template, map data)			throws documentexception, ioexception, templateexception {		stringwriter sout = new stringwriter();		template t = cfg.gettemplate(template);		t.setencoding("utf-8");		t.process(data, sout);		itextrenderer renderer = new itextrenderer();		// 解决中文支持问题		itextfontresolver fontresolver = renderer.getfontresolver();		fontresolver.addfont(servletactioncontext.getrequest().getrealpath(				"/fonts/arialuni.ttf"), basefont.identity_h,				basefont.not_embedded);		// fontresolver.addfontdirectory(servletactioncontext.getrequest()		// .getrealpath("/fonts/"), basefont.not_embedded);		renderer.setdocumentfromstring(sout.getbuffer().tostring());		// 解决图片的相对路径问题		renderer.getsharedcontext().setbaseurl(				new file(servletactioncontext.getrequest().getrealpath("/"))						.touri().tourl().tostring());		system.out.println(new file(servletactioncontext.getrequest()				.getrealpath("/")).touri().tourl().tostring());		bytearrayoutputstream out = new bytearrayoutputstream();		renderer.layout();		renderer.createpdf(out, true);		return new bytearrayinputstream(out.tobytearray());	}	}
 
模板:
 
<html>	<head>		<meta http-equiv="content-type" content="text/html; charset=utf-8" />		<style type="text/css">body {	font-family: arial unicode ms;}</style>		<title>书皮</title>		<style type="text/css">		@page {	size: a4 portrait;	margin: 20mm 10mm;	padding: 1em;	@top-left {content: element(header);}	@bottom-left{content: element(footer);}	@bottom-center{content: counter(page) " / " counter(pages);}	@bottom-right{content: "www.quidos.co.uk";}           }@page blank {	size: a4 portrait;	margin: 20mm 10mm;	padding: 1em;	@top-left { content: normal } 	@bottom-left { content: normal } 	@top-right { content: normal }   	@bottom-right {content: normal }	@bottom-center{content: normal }} /*header as footer will automatically icluded in each pdf printed page*/#header { position: running(header);}#footer { display: block; position: running(footer);}#pagenumber:before {content: counter(page);}#pagecount:before {content: counter(pages);}/* used for generating table of content */#toc a::after { content: leader('.') target-counter(attr(href), page); }/* use this class for first level titles */.page_break_before{ page-break-before: always; }/* use this class for forcing page break inside pdf */.page_breaker{page-break-after:always;}.blank { page : blank; }</style><bookmarks>	<bookmark name="test1" href="#test1" />	<bookmark name="test2" href="#test2">				<bookmark name="test3" href="#test3">		</bookmark>				<bookmark name="test4" href="#test4">		</bookmark>			</bookmark></bookmarks>			</head>	<body>		<div style="font-family: arial unicode ms;" id="test1">			<input name="testinput" type="hidden" value="test value" />			<p>				项目编号:				<span style="border-bottom: black solid 1px;">{xiangmubianhao}</span>			</p>			<p>				<span style=""><br/> </span>			</p>					</div>	</body></html>
 参考[url=http://www.iteye.com/topic/509417]http://www.iteye.com/topic/509417[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值