Jump to content
We've recently updated our Privacy Statement, available here ×

How to export report into PDF file


Recommended Posts

By: Winston Chen - winstonchen

How to export report into PDF file

2003-10-29 19:53

Hi all,

 

I have problem to export the report into PDF file.

I can print the report and I can view the report, but I can not print it into PDF file.

Here is my code

 

Map parameters = new HashMap();

parameters.put(jTextField_parameterName.getText(), new Double(jTextField_value.getText()));

 

try {

JasperDesign jDesign = JasperManager.loadXmlDesign(SystemInfo.DEFAULT_PATH + "//xml//" + jTextField_xmlFile.getText() +".xml");

Connection conn = SystemInfo.getConnection();

JasperReport jReport = JasperManager.compileReport(jDesign);

JasperPrint jPrint = JasperManager.fillReport(jReport, parameters,conn);

//JasperViewer.viewReport(jPrint);

JasperRunManager.runReportToPdf("Jasper.pdf", parameters, conn);

//JasperManager.printReport(jPrint, true);

}

catch (Exception ex) {

JOptionPane.showMessageDialog( (JFrame)null, ex.getMessage());

}

 

 

 

 

By: Brett - bsutton

RE: How to export report into PDF file

2003-11-02 13:48

Here is how we do it:

 

import java.io.File;

import java.io.IOException;

import java.io.OutputStream;

import java.io.Writer;

import java.sql.Connection;

import java.sql.SQLException;

import java.util.HashMap;

import java.util.Map;

 

import dori.jasper.engine.JRException;

import dori.jasper.engine.JRExporterParameter;

import dori.jasper.engine.JRParameter;

import dori.jasper.engine.JasperFillManager;

import dori.jasper.engine.JasperPrint;

import dori.jasper.engine.JasperRunManager;

import dori.jasper.engine.export.JRHtmlExporter;

import dori.jasper.engine.export.JRHtmlExporterParameter;

import dori.jasper.engine.util.JRLoader;

 

/*

* Created on 31/10/2003

*

* To change the template for this generated file go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

 

/**

* @author bsutton

*

* To change the template for this generated type comment go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

public class JasperReport

{

static public final int FORMAT_HTML = 1;

static public final int FORMAT_PDF = 2;

 

private File m_filReport;

private File m_filImagePath;

private String m_strTitle;

private dori.jasper.engine.JasperReport m_report;

private Map m_mapParameters = new HashMap();

 

JasperReport(

String strReportPath,

String strImagePath,

String strReportTitle) throws JRException, IOException

{

this(

new File(strReportPath),

new File(strImagePath),

strReportTitle);

}

 

JasperReport(

File filReport,

File filImagePath,

String strReportTitle) throws JRException, IOException

{

m_filReport = filReport;

m_filImagePath = filImagePath;

m_strTitle = strReportTitle;

 

// Load the report.

m_report =

(dori.jasper.engine.JasperReport) JRLoader.loadObject(

filReport.getPath());

m_mapParameters.put("ReportTitle", m_strTitle);

m_mapParameters.put("DocumentBase", filImagePath.getCanonicalPath());

 

}

 

void setParameter(String strKey, Object oValue)

{

m_mapParameters.put(strKey, oValue);

}

 

void setParameters(Map mapParameters)

{

m_mapParameters.putAll(mapParameters);

}

 

void sendToHTML(Connection conn, Writer out)

throws IOException, SQLException, JRException

{

// Determine the location of report images

HTMLFormatter formatter =

new HTMLFormatter(m_report, m_strTitle, m_filImagePath, m_mapParameters);

 

//validate();

formatter.format(conn);

formatter.output(out);

}

 

void sendToPDF(Connection conn, OutputStream out)

throws IOException, SQLException, JRException

{

// Determine the location of report images

// Formatter formatter =

// Formatter.getInstance(m_filReport, m_strTitle, m_filImagePath, m_nFormat);

PDFFormatter formatter =

new PDFFormatter(m_report, m_strTitle, m_filImagePath, m_mapParameters);

 

formatter.format(conn);

formatter.output(out);

}

 

void validate() throws JRException

{

// Check that all of the reports required parameters have been set.

HashMap mapParameters = new HashMap(m_mapParameters);

 

JRParameter[] aParams = m_report.getParameters();

for (int i = 0; i < aParams.length; i++)

{

String strName = aParams.getName();

if (!m_mapParameters.containsKey(strName))

throw new JRException("Required Jasper Parameter " + strName

+ " not passed to report.");

mapParameters.remove(strName);

}

 

// Check that there is a JRParameter for every parameter passed in

if (mapParameters.size() != 0)

throw new JRException("The following parameters were not found in the Report: "

+ mapParameters.toString());

}

abstract static class Formatter

{

final protected File m_filImagePath;

final protected dori.jasper.engine.JasperReport m_report;

final protected Map m_parameters;

 

abstract protected void format(Connection conn)

throws SQLException, JRException;

 

Formatter(dori.jasper.engine.JasperReport report, String strTitle, File filImagePath, Map mapParameters)

throws JRException, IOException

{

m_report = report;

m_filImagePath = filImagePath;

m_parameters = mapParameters;

}

}

 

static class HTMLFormatter extends Formatter

{

JasperPrint m_jasperPrint;

HTMLFormatter(dori.jasper.engine.JasperReport report, String strTitle, File filImagePath, Map mapParameters)

throws JRException, IOException

{

super(report, strTitle, filImagePath, mapParameters);

}

 

protected void format(Connection conn) throws SQLException, JRException

{

m_jasperPrint =

JasperFillManager.fillReport(

m_report,

m_parameters,

conn);

//new JRResultSetDataSource(getResultSet(conn)));

}

 

protected void output(Writer out) throws JRException, IOException

{

JRHtmlExporter exporter = new JRHtmlExporter();

 

StringBuffer sbuffer = new StringBuffer();

 

exporter.setParameter(

JRExporterParameter.JASPER_PRINT,

m_jasperPrint);

 

exporter.setParameter(

JRExporterParameter.OUTPUT_STRING_BUFFER,

sbuffer);

 

Map imagesMap = new HashMap();

exporter.setParameter(

JRHtmlExporterParameter.IMAGES_MAP,

imagesMap);

 

exporter.setParameter(

JRHtmlExporterParameter.IMAGES_URI,getImageURI());

 

// Export the report to the string buffer sbuffer.

exporter.exportReport();

 

// Write report to browser.

out.write(sbuffer.toString());

}

 

protected String getImageURI()

{

return "/"

+ idatam.common.servlet.Settings.getServletPrefix()

+ "/JasperPrint?Action=showImage&image=";

}

}

 

static class PDFFormatter extends Formatter

{

byte[] m_bytes = null;

 

PDFFormatter(dori.jasper.engine.JasperReport report, String strTitle, File filDocumentBase, Map mapParameters)

throws JRException, IOException

{

super(report, strTitle, filDocumentBase, mapParameters);

}

 

protected void format(Connection conn) throws SQLException, JRException

{

m_bytes =

JasperRunManager.runReportToPdf(

m_report,

m_parameters,

conn);

//w JRResultSetDataSource(getResultSet(conn)));

 

}

 

protected void output(java.io.OutputStream out)

throws JRException, IOException

{

if (m_bytes != null && m_bytes.length > 0)

{

out.write(m_bytes, 0, m_bytes.length);

out.flush();

}

else

{

throw new JRException(

"The report contained no content. "

+ "If this problem persists please contact your system administratror.");

}

}

}

 

}

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...