How to - Update existing JasperReports demos to use Aspose.Pdf for JasperReports
Contents
[
Hide
]
Aspose.PDF for JasperReports includes a number of demo projects to help you get started exporting reports to PDF. These demos are based on standard JasperReports demos that have been modified to demonstrate how to use new exporters. This tutorial, goes through the steps required to update the existing JasperReports demos to use Aspose.PDF for JasperReports.
Updating Demos to use Aspose.PDF
The following steps explains how to update existing demos to use Aspose.PDF for JasperReports export extension rather than using JasperReport’s standard PDF export feature.
- Download JasperReports from http://sourceforge.net/project/showfiles.php?group_id=36382&package_id=28579. Make sure to download the entire archived project with the source code and demos, not just a single JAR. This tutorial was prepared using JasperReports-3.5.2.
- Unpack the archived project to some location on your hard disk, for example C:.
- Copy aspose.pdf.jasperreports.jar from the \lib folder in Aspose.PDF.JasperReports.zip to
<InstallDir>
\jasperreports\lib. - Open
<InstallDir>
\jasperreports\demo\samples, (where<InstallDir>
is the location you have unpacked JasperReports) to update an existing demo. If you have selected the fonts demo, for example, to use with Aspose.PDF for JasperReports, create a copy of it so that the original demo remains the same. For the purpose of this example, we have named the new folder fonts.ap. Note: demos will run from<InstallDir>
\jasperreports\demo\samples because the demo build scripts rely on the JasperReports’ folder structure. If you change the sample folder, you have to modify build scripts. - Open the FontsApp.java file from the src folder and add a reference to Aspose.PDF for JasperReports: import com.aspose.pdf.jr3_7_0.jasperreports.*; (We are using jr3_7_0 because this tutorial was prepared with JasperReports 3.5.2.)
- Add a new string: private static final String TASK_ASPOSE_PDF = “aspose_pdf”; along with existing variables as an export option via Aspose.PDF for JasperReports.
- Locate the for else if (TASK_PDF.equals(taskName)) code segment and copy the whole segment.
- Paste the code snippet under same segment.
else if (TASK_PDF.equals(taskName))
{
File sourceFile = new File(fileName);
JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".pdf");
JRPdfExporter exporter = new JRPdfExporter();
HashMap fontMap = new HashMap();
FontKey key = new FontKey("DejaVu Serif", true, false);
PdfFont font = new PdfFont("DejaVuSerif-Bold.ttf", "Cp1252", true);
fontMap.put(key, font);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
exporter.setParameter(JRExporterParameter.FONT_MAP, fontMap);
exporter.exportReport();
System.err.println("PDF creation time : " + (System.currentTimeMillis() - start));
}
update
else if (TASK_PDF.equals(taskName))
as
else if (TASK_ASPOSE_PDF.equals(taskName))
replace
JRPdfExporter exporter = new JRPdfExporter();
with
com.aspose.pdf.jr3_7_0.jasperreports.JrPdfExporter exporter = new
com.aspose.pdf.jr3_7_0.jasperreports.JrPdfExporter();
- Open the build.xml file.
- Make a copy of the following segment and place it inside the same file:
<target name="pdf" description="Generat PDF via Aspose.PDF for JasperReports.">
<java classname="${class.name}">
<arg value="pdf"/>
<arg value="${file.name}.jrprint"/>
<classpath refid="classpath"/>
</java>
</target>
update name="pdf" as name="aspose_pdf"
update <arg value="pdf"/> as <arg value="aspose_pdf"/>
- To run the demo:
- Download the ANT tool from http://ant.apache.org/bindownload.cgi.
- Unpack the ANT tool and set up environment variables as described in the tool’s manual.
- Change the current directory to
\demo\hsqldb and run the following command line: ant runServer
- Open a new command prompt instance and change the current directory to
\demo\samples\fonts.ap and run the following commands in the command line: - ant javac – to compile the Java source files of the test application
- ant compile – to compile the XML report design and produce the .jasper file
- ant fill – to fill the compiled report design with data and produce the .jrprint file
- ant aspose_ pdf – to produce a PDF file using Aspose.PDF for JasperReports.
- Open the resultant PDF (FontsReport.pdf) from the
\demo\samples\ fonts.ap\build\reports\ folder.