2008-10-30

iReport e ColdFusion 8 - II

No post anterior apenas mostrei o caminho das pedras, mas agora vamos ver como ficaram as coisas no meu ColdFusion 8.

Primeiro devemos configurar o servidor ColdFusion, diretamente no arquivo jvm.config:

# JVM classpath
java.class.path={application.home}/servers/lib,
{application.home}/../lib/macromedia_drivers.jar,
{application.home}/lib/cfmx_mbean.jar,
{application.home}/lib,{application.home}/../lib/cfusion.jar,
{application.home}/../lib/iText.jar,
{application.home}/../lib/iTextAsian.jar,
{application.home}/../lib/commons-beanutils-1.6.jar,
{application.home}/../lib/commons-collections-2.1.jar,
{application.home}/../lib/commons-digester-1.7.jar,
{application.home}/../lib/commons-logging.1.0.4.jar,
{application.home}/../lib/commons-logging-api.1.0.4.jar,
{application.home}/../lib/postgresql-8.1-407.jdbc3.jar,
{application.home}/../lib/ojdbc14.jar,
{application.home}/../lib/mysql-connector-java-5.1.7-bin.jar,
{application.home}/../lib/sqljdbc4.jar



Passamos ao ireport, aqui eu usei o Classic 3.0.0 com MySQL, para gerar o layout do relatório (jrxml), que ficou assim (teste.jrxml):


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="classic" >
<property name="ireport.scriptlethandling" value="0" />
<property name="ireport.encoding" value="UTF-8" />
<import value="java.util.*" />
<import value="net.sf.jasperreports.engine.*" />
<import value="net.sf.jasperreports.engine.data.*" />
<queryString><![CDATA[select username from users]]></queryString>
<field name="username" class="java.lang.String"/>
<title>
<band height="50" isSplitAllowed="true" >
<staticText>
<reportElement x="61" y="5" width="412" height="40" key="staticText"/>
<textElement textAlignment="Center">
<font size="28" isBold="true"/>
</textElement>
<text><![CDATA[Pedro Claudio]]></text>
</staticText>
</band>
</title>
<detail>
<band height="19" isSplitAllowed="true" >
<textField >
<reportElement x="0" y="1" width="519" height="15" forecolor="#000000" key="textField"/>
<textElement>
<font fontName="Times-Roman" size="12"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{username}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>


E por fim a chamada cfml para compilação do relatório:


<cfscript>
DriverManager = Server.getClass().forName("coldfusion.sql.JdbcImpl").newInstance();
DriverManager.setDriver("com.mysql.jdbc.Driver");
DriverManager.setUrl("jdbc:mysql://localhost:3306/pcsilva");
DriverManager.setPassword("root");
DriverManager.setUsername("root");
Connection = DriverManager.getConnection();
Connection.setUseUnicode(true);
Connection.setCharacterEncoding("utf8");
parameters = {ReportTitle="pcsilva Teste"};
JasperManager = Server.getClass().forName("net.sf.jasperreports.engine.JasperManager").newInstance();
JasperDesign = JasperManager.loadXmlDesign("#ExpandPath('teste.jrxml')#");
JasperReport = JasperManager.compileReport(JasperDesign);
JasperPrint = JasperManager.fillReport(JasperReport,parameters,Connection);
JasperManager.printReportToPdfFile(JasperPrint,"#ExpandPath('.')#\teste.pdf");
</cfscript>


Que resultará em aquivo teste.pdf.

Nenhum comentário: