Skip to Sample Code
- HTML-style Tables - auto-sized nested tables with full control over padding, margins and borders
- Support for CSS2 Style sheets
- Create inline graphs and charts with XML, direct from the database
- Includes a servlet and a servlet filter for simple generation of PDF's from XML or JSP pages.
- Auto pagination of content with page headers and footers
- Familiar HTML syntax - <a>, <ul>, <p>, <table>, <td> and so on - simpler and faster than FOP
- Unicode support, Encryption, TrueType fonts, Barcodes and more
- Load existing PDF documents as templates
- Digitally sign documents
- Create and edit interactive Forms, or "AcroForms"
- Download Big Faceless Report generator from here. You only have to include the bforeport.jar file in your classpath.
- Create the input XML file as shown below:
<?xml version="1.0" encoding="UTF-8"?>
<Org>
<Employee>
<firstName>Asif</firstName>
<lastName>Mandvi</lastName>
<dateOfBirth>1/1/1900</dateOfBirth>
</Employee>
<Employee>
<firstName>James</firstName>
<lastName>Baker</lastName>
<dateOfBirth>1/1/1900</dateOfBirth>
</Employee>
<Employee>
<firstName>Jon</firstName>
<lastName>Stewart</lastName>
<dateOfBirth>1/1/1900</dateOfBirth>
</Employee>
<Employee>
<firstName>Stephen</firstName>
<lastName>Colbert</lastName>
<dateOfBirth>1/1/1900</dateOfBirth>
</Employee>
<Employee>
<firstName>Samantha</firstName>
<lastName>Bee</lastName>
<dateOfBirth>1/1/1900</dateOfBirth>
</Employee>
<Employee>
<firstName>Jon</firstName>
<lastName>Oliver</lastName>
<dateOfBirth>1/1/1900</dateOfBirth>
</Employee>
</Org>test.xml - Create the template for the PDF(XSL) as shown below.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<xsl:preserve-space elements="true" />
<xsl:template match="Org">
<pdf>
<body>
<table font-size="12pt" width="700px">
<tr font-weight="bold">
<td>First Name</td>
<td>Last Name</td>
<td>Date of Birth</td>
</tr>
<xsl:for-each select="/Org/Employee">
<tr>
<td>
<xsl:value-of select="firstName" />
</td>
<td>
<xsl:value-of select="lastName" />
</td>
<td>
<xsl:value-of select="dateOfBirth" />
</td>
</tr>
</xsl:for-each>
</table>
</body>
</pdf>
</xsl:template>
</xsl:stylesheet>test.xsl This XSL file will be used to create the final XML file that will sent as input to the Report generator. - Transform the XML to the format required by the report generator: The XML file cannot be used as input to the report generator directly. It has to be first transformed into the required format. That is reason for using the XSL.
public class ConvertXML {
public void renderReport() {
try {
TransformerFactory factory = TransformerFactory.newInstance();
StreamSource stylesheet = new StreamSource(new FileInputStream("c:\\pdf\\test.xsl"));
Transformer transformer = factory.newTransformer(stylesheet);
Source src = new StreamSource(new FileInputStream("c:\\pdf\\test.xml"));
Result res = new StreamResult(new FileOutputStream("c:\\pdf\\intermediate.xml"));
transformer.transform(src, res);
} catch (Exception e) {
e.printStackTrace();
}
}
}ConvertXML.java - The following code can be used to generate the final PDF: The PDFGenerator first uses the ConvertXML to transform the input to the report generator input format (intermediate.xml), and then invokes the report generator.
public class PDFGenerator {
public void createPDF(String xmlfile, OutputStream out) {
ReportParser parser;
try {
parser = ReportParser.getInstance();
InputSource src = new InputSource(new FileInputStream(xmlfile));
PDF pdf = (PDF) parser.parse(src);
pdf.render(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
PDFGenerator gen = new PDFGenerator();
OutputStream os = null;
try {
ConvertXML cx = new ConvertXML();
cx.renderReport();
os = new FileOutputStream("c:\\pdf\\test.pdf");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
gen.createPDF("C:\\pdf\\intermediate.xml", os);
}
}PDFGenerator.java
No comments:
Post a Comment