一个是xhtmlrenderer
https://xhtmlrenderer.dev.java.net/
LGPL协议
另外一个是
http://www.allcolor.org/YaHPConverter/
LGPL协议
都是基于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>
参考http://www.iteye.com/topic/509417
分享到:
相关推荐
安卓下可用的HTML转PDF代码,支持中文和单独的图片 android_HTML转PDF android_HTML转PDF
go 语言实现 html 转 pdf
用Aspose.Pdf来实现HTML转PDF
html转pdf.zip
IText5实现Html转PDF(不含图片、表格) 含:字体文件、hmtl文件、jar包、Java代码 安装字体或将字体文件放到相应的目录下。 jar包中的 core-renderer-all.jar 为其他大牛重写的,解决了中文字体和换行的问题。 Html...
1、itext将静态html转pdf中文显示及换行问题 2、itext将静态html转pdf 字体问题 3、html转pdf 参考我的另外一篇博文https://blog.csdn.net/qq_38616723/article/details/125188407?spm=1001.2014.3001.5502
java实现html转pdf,包含后端代码,html测试页面,pom依赖,jar包,如页面过于复杂需调整页面,有些样式itextpdf不支持,请寻找其它样式替换。
html转pdf工具,可通过参数调用 实现 html页面 转换成pdf文件
Golang实现HTML转PDF
vue,html转pdf,下载导出及打印
xhtmlrenderer 进行HTML转PDF,修改了core-renderer-R8的源码,兼容中英文换行,且保持对英文单词完整性,亲测可用。
java实现html转换pdf
phantomjs html转pdf 完美版本 可以把 html 非常完美的转成pdf,与浏览器转换出来的一样完美
3、动态html拼接转pdf public static void htmlCodeComeString(String linkcss,String htmlCode, String outputFile,String title) throws Exception { OutputStream os = new FileOutputStream(outputFile); ...
1、解决HTML转pdf 中文、数字、英文不能换行的问题,修改的源码包 重新计算了右边距。 2、html转pdf 参考我的另外一篇博文https://blog.csdn.net/qq_38616723/article/details/125188407?spm=1001.2014.3001.5502
HTML转PDF-android,并且输出, HTML转PDF-android,并且输出
iText html转pdf jar包 用于Java类库 iText html转pdf jar包 用于Java类库 亲测可用
实现html字符串转pdf文件,支持中文,支持图片,不会有乱码的
itextsharp操作html转pdf,简单示例,中文也能正常转换。
HTML转PDF插件: 1. Windows和Linux均分别选择对应插件,然后通过程序异步调用; 2. 用过多款插件,这个能较好的将HTML中的样式最大化保留下来