velocity制作pdf

velocity制作PDF

Velocity 是一个基于 java 的模板引擎(templateengine). 它可以让视图的设计者在 web 页面中引用 java 代码中定义的数据对象和命令。

1.Velocity

依赖

<dependency>
    <groupId>
org.apache.velocity</groupId>
    <artifactId>
velocity</artifactId>
    <version>
1.7</version>
</dependency>

1.  VelocityEngine

VelocityEngine可以通过构造器和init()方法获取实例,如果功能不够满意,可以通过properties配置新的功能,构造器为VelocityEngine() ,VelocityEngine(Properties p) ,VelocityEngine(StringpropsFilename) 。init方法为init() ,init(Properties p),init(String propsFilename) 。

相比于Velocity提供了更加强大的功能,框架开发者一般使用VelocityEngine这个类在框架中使用velocity模板渲染功能,内部也是是委托给RuntimeInstance来具体实现的,但是每个VelocityEngine都有一个自己的RuntimeInstance实例。也就是说在一个JVM中可以有多个VelocityEngine实例,每个实例都可以定制化自己的配置,这为在同一个应用中配置不同的模板路径和logger提供了方便。

VelocityEngine velocityEngine = new VelocityEngine();
Properties properties = new Properties();
properties.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SimpleLog4JLogSystem");
properties.setProperty("runtime.log.logsystem.log4j.category", "velocity");
properties.setProperty("runtime.log.logsystem.log4j.logger", "velocity");
velocityEngine.init(properties);

 

2.  Context模块

Context是一个数据中心,将数据传递给模板,以提供对模板渲染所需要的变量的封装管理,相当于一个Map。

VelocityContext 两个基本用法:

public Object put(String key, Objectvalue);

public Object get(String key);

3.  mergeTemplateVelocity生成html

有了velocity模板,有了数据源需要将数据渲染成html,VelocityEngine方法mergeTemplate(String templateName, String encoding, Context context,Writer writer) ,合并一个模板和一个渲染流写入writer。

Java代码:

public static void main(String[] args) {
        VelocityEngine ve = new VelocityEngine();
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        ve.setProperty("input.encoding", "UTF8");
        ve.setProperty("output.encoding", "UTF8");
        ve.init();
        Template t = ve.getTemplate("template/show.vm");
        VelocityContext ctx = new VelocityContext();
        ctx.put("data", "velocity");
        StringWriter sw = new StringWriter();
        t.merge(ctx, sw);
        System.out.println(sw.toString());
    }

vm模板:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
传递的信息为$data
</body>
</html>

生成的html

<!DOCTYPEhtml>

<html>

<head>

    <meta charset="UTF-8">

    <title>Title</title>

</head>

<body>

传递的信息为velocity

</body>

</html>

4.  生成pdf

生成html还是没有问题的,有时候需要生成pdf,这个时候需要其他的工具,最常用的是itext。

<dependency>
    <groupId>com.lowagie</groupId>
    <artifactId>itext</artifactId>
    <version>2.1.7</version>
</dependency>
<dependency>
    <groupId>org.xhtmlrenderer</groupId>
    <artifactId>flying-saucer-pdf-itext5</artifactId>
    <version>9.0.7</version>
</dependency>
 

iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。

生成pdf的案例:

Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E:/log/1234.pdf"));
document.open();
document.add(new Paragraph("我的名字是jingke"));
document.close();

这个时候可以查看pdf了,但是汉字不会被显示。

Document定义了文档的参数,Document的构造参数:

public document();
public document(Rectangle pageSize);
public document(Rectangle pageSize, int marginLeft, int marginRight, int marginTop, int marginBottom);
pageSize是指文档页面大小,public document();页面大小为A4,效果等同于Document(PageSize.A4)。
marginLeft、marginRight、marginTop、marginBottom分别为左、右、上、下的页边距。
 

Document还可以设定文档的标题、主题、作者、关键字、装订方式、创建者、生产者、创建日期等属性,可以设定页面的大小、书签、脚注(HeaderFooter)等信息,iText也提供了对PDF文件的安全保护,通过书写器(Writer)的setEncryption方法,可以设定文档的用户口令、只读、可打印等属性。

根据html生成pdf:

OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont(PDF_FONT_PATH, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
String content = VelocityUtils.mergeTemplateFileToString(ResourceLoader.FILE, vmTempName, map, "utf-8", baseDir);
renderer.setDocumentFromString(content);
renderer.layout();
renderer.createPDF(os);
os.close();

 

5. 图片背景问题

Html转pdf的时候,html图片背景最好配置成绝对路径。

Html转pdf对html要求很严格,根据不同的版本有不同的要求,例如有时候不能添加<title>Title</title>这个属性。

 


6.  字体问题

通过html生成PDF汉字不显示,可以将用到的字体库单独拿出来,

private static final String PDF_FONT_PATH = TemplateToPdfUtils.class.getClassLoader().getResource("font/simsun.ttc").getPath();
通过fontResolver.addFont(PDF_FONT_PATH, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);进行配置,可以配置多个字体。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Velocity的能力远不止web站点开发这个领域,例如,它可以从模板(template)产生SQL和PostScript、XML,它也可以被当作一个独立工具来产生源代码和报告,或者作为其他系统的集成组件使用。Velocity也可以为Turbine web开发架构提供模板服务(template service)。Velocity+Turbine提供一个模板服务的方式允许一个web应用以一个真正的MVC模型进行开发。 Velocity能为我们作什么? The Mud Store Example 假设你是一家专门出售Mud的在线商店的页面设计人员,让我们暂且称它为“在线MUD商店”。你们的业务很旺,客户下了各种类型和数量的mud订单。他们都是通过输入用户名和密码后才登陆到你的网站,登陆后就允许他们查看订单并购买更多的mud。现在,一种非常流行的mud正在打折销售。另外有一些客户规律性的购买另外一种也在打折但是不是很流行的Bright Red Mud,由于购买的人并不多所以它被安置在页面的边缘。所有用户的信息都是被跟踪并存放于数据库中的,所以某天有一个问题可能会冒出来:为什么不使用 velocity来使用户更好的浏览他们感兴趣的商品呢? Velocity使得web页面的客户化工作非常容易。作为一个web site的设计人员,你希望每个用户登陆时都拥有自己的页面。 你会见了一些公司内的软件工程师,你发现他们每个人都同意客户应该拥有具有个性化的信息。那让我们把软件工程师应该作的事情发在一边,看一看你应该作些什么吧。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值