Skip to content
Donat Müller edited this page Dec 9, 2015 · 9 revisions

Desktop & Android API

The single point of entry into the PDFReporter library for desktop and Android is the class

org.oss.pdfreporter.PdfReporter

Scenarios

JSON

getExporter(DESIGN_REPORT_JSON_CUSTOMERS, "jsondatasource","extra-fonts")
    	    .addSubreport("JsonOrdersReport", "JsonOrdersReport.jasper")
    	    .addJSONParams("yyyy-MM-dd", "#,##0.##", Locale.ENGLISH, Locale.US)
            .setJsonSource()
    	    .exportPdf();

SQL

getExporter(DESIGN_REPORT_HORIZONTALLIST, "list", "extra-fonts")
	    .setSqlSource(testProvider.databasePath(), SQL_USERNAME, SQL_PASSWORD)
	    .exportPdf();

XML

getExporter(DESIGN_REPORT_CDBOOCKLET, "cdbooklet","extra-fonts")
		.setXmlSource(XML_DATA_CDBOOKLET, XPATH_DATA_CDBOOKLET)
		.exportPdf();

XML with Subreport XML Document

getExporter(DESIGN_REPORT_CUSTOMERS, "xmldatasource","extra-fonts")
	.setXmlSource(XML_DATA_NORTHWIND, XPATH_DATA_NORTHWIND_CUSTOMERS)
	.addSubReportXMLDocument(XML_DATA_NORTHWIND)
	.addXMLParams("yyyy-MM-dd", "#,##0.##", Locale.ENGLISH, Locale.US)
	.exportPdf();

Multilanguage

getExporter(DESIGN_REPORT_I18N, "i18n")
           .addFillParameter("number", new Double(1234567 + Math.random()))
           .newResourceBundle("test.org.oss.pdfreporter.resourcebundle.i18n", locale)
           .exportPdf();

Encryption

getExporter(DESIGN_REPORT_PDFCRYPT, "misc", null)
.addEncryption(true, "pdfreporter", "reports", IDocument.PERMISSION_COPY | IDocument.PERMISSION_PRINT)
.exportPdf();

Resource handling - variant 1

PdfReporter getExporter(String jrxml, String reportFolder, String extraFolder) {
    RepositoryManager repo = PdfReporter.getRepositoryManager();
    repo.reset();
    repo.setDefaultResourceFolder(resourceFolder);
    repo.setDefaulReportFolder(rootFolder + RepositoryManager.PATH_DELIMITER + "jrxml" +        RepositoryManager.PATH_DELIMITER + reportFolder);
    if (null != extraFolder) {
        repo.addExtraReportFolder(resourceFolder + RepositoryManager.PATH_DELIMITER + extraFolder);
    }
    repo.addExtraReportFolder(resourceFolder);
    return new PdfReporter(jrxmlPath, getOuputPdfFolder(), getFilenameFromJrxml(jrxmlPath));
}

Resource handling - variant 2

PdfReporter getExporter(String jrxml, String reportFolder, String extraFolder) {
    RepositoryManager repo = RepositoryManager.getInstance();
    repo.setDefaultResourceFolder(inputPath(JRXML_RESOURCE_FOLDER));
    repo.setDefaulReportFolder(
          inputPath(JRXML_REPORT_FOLDER + RepositoryManager.PATH_DELIMITER + reportFolder));
    if (null != extraFolder) {
        repo.addExtraReportFolder(
             inputPath(JRXML_REPORT_FOLDER + RepositoryManager.PATH_DELIMITER + extraFolder));
    }
    repo.addExtraReportFolder(inputPath(XML_DATASOURCE_FOLDER));
    return new PdfReporter(jrxml, outputPath(PDF_OUTPUT_FOLDER), getFilenameFromJrxml(jrxml));
}

Configuration

The class

DefaultIRegistryExtensionsRegistryFactory

is the registry for implementations that can be substituted based on platform and and even if another for example another xml library for parsing should be used. Here the implementation of such a class:

public class DefaultIRegistryExtensionsRegistryFactory implements ExtensionsRegistryFactory {
    private final static Logger logger = 
    Logger.getLogger(DefaultIRegistryExtensionsRegistryFactory.class.getName());
    private static boolean isInitialized = false;

@Override
public ExtensionsRegistry createRegistry(String registryId,
		JRPropertiesMap properties) {
	initializeIRegistry();
	return new NullExtensionsRegistry();
}

private static class NullExtensionsRegistry implements ExtensionsRegistry {

	@Override
	public <T> List<T> getExtensions(Class<T> extensionType) {
		return null;
	}
}

synchronized private void initializeIRegistry() {
	if (!isInitialized) {
		Logger.getLogger("").setLevel(Level.FINEST);
		XmlParserFactory.registerFactory();
		NetFactory.registerFactory();
		SimpleFormatFactory.registerFactory();
		DefaultFormatFactory.registerFactory();
		FallbackFormatFactory.registerFactory();
		JsonDataSourceFactory.registerFactory();
		BeansFactory.registerFactory();
		FontFactory.registerFactory();
		ImageFactory.registerFactory();
		GeometryFactory.registerFactory();
		PdfFactory.registerFactory();
		isInitialized = true;
		logger.info("Initialized IRegistry");
	}
}
}

Properties

All properties should be provided in a folder so that the PDFReporter library will find them.

default.jasperreports.properties

All default settings that comes with a typical installation. You will find examples at the project pdfreporter-testdata or at our connector for bonitasoft.

jasperreports_extension.properties

Basically any extension like the configuration class from above and any font that need to be shipped with the app.

iOS API

Create a ReporterConfiguration object then call setSqlSource or setXmlSource. Optionally call addExportParameterValue, addSubreport or addEncryption then call PDFReporter.exportPdf(reporterConfiguration). The code snipets below show how the ReporterConfiguration is used to call the ReportExporter directly.

XML

[ReportExporter exportReportToPdf:_reporterConfiguration.outputPdfFilePath
withJrxml:_reporterConfiguration.jrxmlFileName withResourceFolders:_reporterConfiguration.resourceFolders
withXml:_reporterConfiguration.dataSourceFileName 
andXPath:_reporterConfiguration.selectExpression
withParameters:_reporterConfiguration.exportParameters 
withSubreports:_reporterConfiguration.subreportsDefinition];

SQL

[ReportExporter exportReportToPdf:_reporterConfiguration.outputPdfFilePath
withJrxml:_reporterConfiguration.jrxmlFileName 
withResourceFolders:_reporterConfiguration.resourceFolders 
andSqlite3:_reporterConfiguration.dataSourceFileName 
withParameters:_reporterConfiguration.exportParameters 
withSubreports:_reporterConfiguration.subreportsDefinition];

No Datasource

[ReportExporter exportReportToPdf:_reporterConfiguration.outputPdfFilePath 
withJrxml:_reporterConfiguration.jrxmlFileName 
withResourceFolders:_reporterConfiguration.resourceFolders 
withParameters:_reporterConfiguration.exportParameters 
withSubreports:_reporterConfiguration.subreportsDefinition];

JSON

(coming soon)