⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dynamic pdf creation from database.txt

📁 This a sample JSP which creates an XML definition for a PDF file.
💻 TXT
📖 第 1 页 / 共 2 页
字号:
createxml.jsp

<?xml version="1.0"?>
<!DOCTYPE pdf PUBLIC "-//big.faceless.org//report" "http://big.faceless.org/products/report/report-1.0.dtd">

<%@ page language="java" import="java.sql.*, java.util.*, java.text.*, javax.sql.*, javax.naming.*" contentType="text/xml; charset=UTF-8"%>


<%--

This is a sample JSP which creates an XML definition for a PDF file.

The JSP is broken into three main stages

1. Headers.
   The correct XML headers need to be sent to the PDF conversion servlet,
   so that the XML parser knows how to process the file. This needs to
   be done right at the start, so the first two lines of this file are
   the XML headers to send, followed by two lines for the JSP engine -
   one language directive and the other loading a simple database bean.

2. Initialization.
   Next we read the parameters passed in the URL, load the account info
   from the database and set up a few basic variables, like number
   formatters, date formatters, the tax rate, pagesize and so on. If
   the specified account isn't found, we throw an error.

3. The XML.
   This is mostly sent verbatim to the PDF conversion servlet, with the
   exception of a few embedded JSP tags to substitute the account info,
   call breakdown and so on. This is the meat of the example, and we'll
   go into more detail on this further down in the source code

The database bean we're using is a custom one we use in-house, which
handles connection pooling and so on. We only use it to establish the
initial database connection here, so you can easily substitute your own.

--%>

<%
// 1. Initialization phase
//

// Set some basic information
//
double tax=0.175;

String pagesize = request.getParameter("pagesize");
if (pagesize==null) pagesize="A4";

// Much of the JSP is localized to the locale of the requester (that's you).
// We take whatever settings your web browser has specified, but if no country
// is specified force it to something so we get a value for the currency symbol.
// This "country-guessing" algorithm isn't very intelligent. If you get funny
// currency values, it's guessed wrong.
//
// This has nothing to do with PDF generation at all, but we think localization
// is one of Java's best kept secrets, and we use it whenever we have an excuse.
//
Locale loc = new Locale(request.getLocale().getLanguage(), (request.getLocale().getCountry().length()==0 ? (request.getLocale().getLanguage().equals("en") ? "GB" : request.getLocale().getLanguage().toUpperCase()) : request.getLocale().getCountry()));

// Create some formats to use throughout the PDF
//
NumberFormat amtf = NumberFormat.getCurrencyInstance(loc);
DateFormat datef = DateFormat.getDateInstance(DateFormat.LONG, loc);
DateFormat datetimef = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, loc);


// Load account information from the database
//
String account=null, name=null, address=null;
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/sample");
Connection c = ds.getConnection();
Statement s = c.createStatement();
ResultSet r = s.executeQuery("select accno, name, address from account where accno="+Integer.parseInt(request.getParameter("account")));

while (r.next()) {
    account=request.getParameter("account");
    name=r.getString("name");
    address=r.getString("address");
}

// Load total amount for this bill. Doesn't really matter, it's
// just an example.... but why not?
//
double amount=0;
r = s.executeQuery("select sum(seconds*charges.persecond) from calls, charges where charges.type=chargetype and accno="+Integer.parseInt(request.getParameter("account")));
if (r.next()) {
    amount=r.getDouble(1);
}

r.close();
s.close();


// If the account doens't exist, throw an error, otherwise......
//
if (account==null) {
    response.sendError(500, "No account number specified");
} else {
%>


<%--

ACTUAL XML BEGINS HERE

First, we'd like to say that under normal circumstances this would
be split into a number of files. We'd have jsp:includes all over the
place to make this file smaller and more manageable.

In particular, a proper implementation would have an external
stylesheet which could be changed as necessary - for example,
customers could select their statements in color or black and white,
and all we need to do is load the appropriate stylesheet.



However, this is just an example and it's useful to have everything
in the one place to look at.



First, we start with the head. We use the "header" and "footer"
macros heavily in this example, to place a large standard header
on each page, and to put a payment slip on the first page. So in
this case, the head actually makes up most of the document.

--%>


<pdf>
<head>
<meta value="15s"/>
<meta value="Mike Bremford"/>
<meta value="Sample Dynamic Report"/>
<meta value="An example of a dynamic report created by the Big Faceless Report Library from a database"/>
<meta value="one-column"/>

<style type="text/css">

/*
 * Some styles that are used throughout most of the document
 * search for 'class="info"' to see where these are used.
 */

.info { font-style:italic; }
.heading { font-family:helvetica; font-weight:bold; font-size:12; }
.address { width:3in; corner-radius:10; margin:10; padding:10; border:1; }
.amounttable { padding-left:0.5in; margin-right:0.5in; align:right; }

/*
 * The table with class="breakdown" is the main table in the document,
 * listing the itemized calls.
 *
 * We make fairly heavy use of styles here so that we can swap a new
 * style in quickly if we change our mind. Plus it's neater to do
 * it here than scatter attributes about the XML
 */

.breakdown { width:100% }
.breakdown thead tr { background-color:gray(20%); color:white; border-top:1; border-bottom:1; margin-bottom:4; font-weight:bold; font-family:helvetica; }
.breakdown thead td td { border:0; }
.breakdown thead tr td:firstchild { border-left:1; }
.breakdown thead tr td:lastchild { border-right:1; }
.breakdown tbody { border:1; }

/*
 * These are the two graphs - numcalls the piegraph, callcost the bargraph
 */
#numcalls { xrotation:20; zrotation:20; display-key:rotated-inner-flat-outer; }
#callcost { xrotation:30; yrotation:20; xaxis-align:right; xaxis-valign:top;
yaxis-formatter:currency(); zwall-border-color:gray(70%); bar-width:80%;
bar-depth:80%; ywall-border-color:gray(70%); floor-border-color:gray(70%); }

/*
 * For both graphs, set some B+W printer friendly default colors.
 */
#callcost, #numcalls { default-colors:'#D0D0D0, pattern(stripes,#C0C0C0,#E0E0E0,4,2,30), pattern(spots,#C0C0C0,#E0E0E0,3), #A0A0A0, pattern(stripes,#E0E0E0,#C0C0C0,4,1.5,90)'; }

/*
 * The first page will always be given an ID of "page1". We can then
 * reference this in a stylesheet to create a custom footer - a "giro"
 * payment slip.
 */
#page1 { footer:girofooter; footer-height:240; }

/*
 * The following styles all apply to the "Giro" payment slip
 * at the end of the first page.
 */
#giroslip { width:100%; font-size:9; font-family:Times; font-style:normal; }
#giroslip .smallprint { font-size:6; font-family:Times; margin-top:2em; }
#giroslip .info { font-size:9; font-family:Times; font-style:italic; }
#giroslip .total { font-size:14; font-family:Times; font-weight:bold; margin-top:6; }
#giroslip .giro { font-size:16; font-family:Helvetica; font-weight:normal; }
#giroslip .sortcode { border:1; padding:1em; font-size:10; font-family:Helvetica; }
#giroslip td.poundbox { border:1; height:0.4in; width:0.9in; }
#giroslip td.pennybox { border:1; height:0.4in; width:0.4in; }
</style>

<macrolist>
  <macro id="girofooter">
    <!--

    This section creates a Giro payment slip (a Giro is a method of
    paying bills at the local post office, used in the UK). The author
    has never actually paid a bill at the post office with one of these,
    but believes it's possible.

    Rather than make this up, we copied it from an old British Telecom
    bill (a dreaded sight for computer people until flat-rate internet
    finally made it to England).

    Notice the nice dotted line at the top of the following <div>. Power
    users could place an absolutely positioned layer over this with one
    of the "pairs of scissors" characters in the predefined ZapfDingbats
    font, for added effect.

    -->

    <div margin-top="20">
      <table width="100%">
<tr>

<!-- The left column. Logo, little boxes for the cashier to write in -->

<td valign="bottom">
<table cellpadding="0">
<tr>
<td margin-bottom="10">
<img dpi="300"/>
<p align="right"><big>Payment slip</big></p> 
</td>
</tr>
<tr>
<td>
<p class="smallprint">Cashier stamp and initials</p>

<!-- Create the corners-only box. -->

<shape height="80">
<shapepath>
<lineto y="0%"/>
<moveto y="0%"/>
<lineto y="0%"/>
<lineto y="10%"/>
<moveto y="90%"/>
<lineto y="100%"/>
<lineto y="100%"/>
<moveto y="100%"/>
<lineto y="100%"/>
<lineto y="90%"/>
<moveto y="10%"/>
<lineto y="0%"/>
</shapepath>
</shape>
</td>
</tr>
<tr>
<td> 
<table cellpadding="0">
<tr>
<td margin-right="20">
<p class="smallprint"><nobr>No. cheques</nobr></p>
<div height="30"/>
</td>
<td>
<p class="smallprint">Fee</p>
<div height="30"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>

<!-- The right column. The main part of the Giro -->

<td>

<!-- First part of the right column. The amount, customer number
and Giro logo
-->

<table width="100%">
<tr>
<td valign="bottom">
<p margin-right="0.5in"><nobr>Your Customer No.</nobr></p>
</td>
<td valign="bottom">
<p margin-right="1in"><nobr><%=account%></nobr></p>
</td>
<td align="right">

<!-- The "Bank Giro Credit" bit. On our bill the Giro symbol of
three circles doesn't fit inside the row the way normal HTML
table data would. This isn't a problem. We simply offset the
<div> containing the logo by 5 points vertically and create
the logo normally. Even though it's too big for the row,
it will display in full, overlapping the edge of it's
containing box due to the default overflow attribute value
of "visible".

Note the position="absolute" attributes on the various
elements.
-->

<table cellpadding="0"><tr>
<td>
<p class="giro"><nobr>Bank Giro Credit</nobr></p>
</td>
<td padding-bottom="0">
<div y="-5">
<circle border="0.5"/>
<circle border="0.5"/>
<circle border="0.5"/>
<shape border="2">
<shapepath>
<moveto y="6"/>
<lineto y="6"/>
<lineto y="18"/>
<moveto y="13.1"/>
<lineto y="18"/>
<lineto y="18"/>
<moveto y="22.9"/>
<lineto y="18"/>
<lineto y="6"/>
</shapepath>
</shape>
</div>
</td>
</tr></table>
</td>
</tr>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -