AbstractFopReportGenerator implements ReportGenerator { @Override public void generateReport(final ReportDescriptor descriptor, final Map<String, Object> parameters, final Object data, final String lang, final OutputStream outputStream) { if (data == null || data instanceof Collection && ((Collection) data).isEmpty()) { LOG.debug("No data, no report will be generated"); return; } try { final InputStream config = getFopUserConfigInputStream(descriptor, parameters, data, lang); if (config == null) { LOG.error("FOP config file not found, " + "please put the fop-userconfig.xml file into the classpath of the server, UTF-8 characters won't be displayed correctly"); return; } final URI base = getBaseReportURI(descriptor, parameters, data, lang); final ResourceResolver rr = ResourceResolverFactory.createTempAwareResourceResolver( getTempResourceResolver(descriptor, parameters, data, lang), getResourceResolver(descriptor, parameters, data, lang) ); final FopFactoryBuilder confBuilder = new FopConfParser( config, EnvironmentalProfileFactory.createRestrictedIO(base, rr) ).getFopFactoryBuilder(); final Source xsltfile = getXsltFile(descriptor, parameters, data, lang); if (xsltfile == null) { LOG.error("Unable to read XSLT-FO file for {} in {}", descriptor, lang); return; } final FopFactory fopFactory = confBuilder.build(); final FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); final Source source = convertToSource(descriptor, parameters, data, lang); final String mime = getOutputMimeType(descriptor, parameters, data, lang); final Fop fop = fopFactory.newFop(mime, foUserAgent, outputStream); final TransformerFactory factory = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", null); final Transformer transformer = factory.newTransformer(xsltfile); transformer.setParameter("versionParam", "2.0"); transformer.setOutputProperty("encoding", "UTF-8"); final Result res = new SAXResult(fop.getDefaultHandler()); transformer.transform(source, res); } catch (Exception exp) { LOG.error("Unable to generate report for " + descriptor + " in " + lang, exp); } }  @Override boolean supports(final ReportDescriptor reportDescriptor); @Override void generateReport(final ReportDescriptor descriptor,
                               final Map<String, Object> parameters,
                               final Object data,
                               final String lang,
                               final OutputStream outputStream);  }
@Test public void testGeneratePdfReport() throws Exception { final AbstractFopReportGenerator generator = new AbstractFopReportGenerator() { @Override protected InputStream getFopUserConfigInputStream(final ReportDescriptor descriptor, final Map<String, Object> parameters, final Object data, final String lang) { assertEquals("v1", parameters.get("p1")); assertEquals("en", lang); try { return new FileInputStream(new File(BASE + "fop-userconfig.xml")); } catch (FileNotFoundException e) { fail("Unable to read " + BASE + "fop-userconfig.xml"); return null; } } @Override protected Source getXsltFile(final ReportDescriptor descriptor, final Map<String, Object> parameters, final Object data, final String lang) { assertEquals("v1", parameters.get("p1")); assertEquals("en", lang); return new StreamSource(new File(BASE + ((ReportDescriptorPDF) descriptor).getLangXslfo(null))); } @Override protected Source convertToSource(final ReportDescriptor descriptor, final Map<String, Object> parameters, final Object data, final String lang) { try { return new StreamSource( new InputStreamReader(new FileInputStream("src/test/resources/testdata/" + data), "UTF-8") ); } catch (Exception e) { fail(e.getMessage()); return null; } } @Override protected TempResourceResolver getTempResourceResolver(final ReportDescriptor descriptor, final Map<String, Object> parameters, final Object data, final String lang) { return new TestTempResourceResolver(); } @Override protected ResourceResolver getResourceResolver(final ReportDescriptor descriptor, final Map<String, Object> parameters, final Object data, final String lang) { return new TestResourceResolver(); } }; final ReportDescriptorPDF availableStock = new ReportDescriptorPDF(); availableStock.setReportId("available-stock"); availableStock.setXslfoBase("stock-report/available-stock"); availableStock.setVisible(true); final ByteArrayOutputStream baosAvailableStock = new ByteArrayOutputStream(); generator.generateReport(availableStock, new HashMap<String, Object>() {{ put("p1", "v1"); }}, "available-stock-report.xml", "en", baosAvailableStock); assertTrue(baosAvailableStock.toByteArray().length > 0); try(FileOutputStream fos = new FileOutputStream("target/test-pdf-report.pdf")) { fos.write(baosAvailableStock.toByteArray()); } }