Flying Saucer生成pdf报表

本文介绍了如何使用Flying Saucer这个Java库来将XML或XHTML内容渲染成PDF,并提供了简单的使用步骤。首先,概述了Flying Saucer的功能,它能使用CSS 2.1布局和格式化XML,输出到Swing面板、PDF和图像。然后,给出了在业务场景中应用Flying Saucer生成带有用户信息的表格,并提供下载的解决方案,包括使用Freemarker模板和处理中文显示的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Flying Saucer生成pdf报表:


1.首先看一下Flying Saucer的官方介绍:

https://github.com/flyingsaucerproject/flyingsaucer

OVERVIEW

Flying Saucer is a pure-Java library for rendering arbitrary well-formed XML (or XHTML) using CSS 2.1 for layout and formatting, output to Swing panels, PDF, and images.

Comprehensive documentation available in our user's guide, linked from our website at http://code.google.com/p/flying-saucer/ For information on our development releases, please contact us on our mailing lists.

If you end up using Flying Saucer for your own projects, please drop us an email and tell us about it; it helps inform where we go next, and is interesting and inspiring to other developers.

可以看到Flying Saucer可以完美的解析html,并且可以使用css2.1的样式,添加图片等功能。


2.简单的使用:

业务上经常有这样的场景:

将用户信息查询出来并且显示在页面上设计好的table上。并且可以选择下载。

解决方法:

1.)使用java模板(velocity、freemarker等)定义好表格样式,这里我们使用freemarker

引入freemarker

<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.23</version>
</dependency>

创建ftl模板

<!DOCTYPE html>
<html>
	<head>
		<style type="text/css">
			.tg {
				border-collapse: collapse;
				border-spacing: 0;
			}
			
			.tg td {
				font-family: 'Arial Unicode MS';
				font-size: 14px;
				padding: 16px 5px;
				border-style: solid;
				border-width: 1px;
				overflow: hidden;
				word-br/eak: normal;
			}
			
			.tg th {
				font-family:'Arial Unicode MS';
				font-size: 14px
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值