use of org.apache.fop.apps.FOPException in project play-cookbook by spinscale.
the class RenderPDF method apply.
@Override
public void apply(Request request, Response response) {
Template template = TemplateLoader.load(templateFile);
response.setHeader("Content-Disposition", "inline; filename=\"" + request.actionMethod + ".pdf\"");
setContentTypeIfNotSet(response, "application/pdf");
try {
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, response.out);
Transformer transformer = tFactory.newTransformer();
Scope.RenderArgs args = Scope.RenderArgs.current();
String content = template.render(args.data);
Source src = new StreamSource(IOUtils.toInputStream(content));
javax.xml.transform.Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
} catch (FOPException e) {
Logger.error(e, "Error creating pdf");
} catch (TransformerException e) {
Logger.error(e, "Error creating pdf");
}
}
use of org.apache.fop.apps.FOPException in project series-rest-api by 52North.
the class PDFReportGenerator method encodeAndWriteTo.
@Override
public void encodeAndWriteTo(DataCollection<Data<QuantityValue>> data, OutputStream stream) throws IoHandlerException {
try {
generateOutput(data);
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
Configuration cfg = cfgBuilder.build(document.newInputStream());
URI baseURI = new File(".").toURI();
FopFactory fopFactory = new FopFactoryBuilder(baseURI).setConfiguration(cfg).build();
final String mimeType = Constants.APPLICATION_PDF;
Fop fop = fopFactory.newFop(mimeType, stream);
// FopFactory fopFactory = FopFactory.newInstance(cfg);
// Fop fop = fopFactory.newFop(APPLICATION_PDF.getMimeType(), stream);
// FopFactory fopFactory = fopFactoryBuilder.build();
// Fop fop = fopFactory.newFop(APPLICATION_PDF.getMimeType(), stream);
// Create PDF via XSLT transformation
TransformerFactory transFact = TransformerFactory.newInstance();
StreamSource transformationRule = getTransforamtionRule();
Transformer transformer = transFact.newTransformer(transformationRule);
Source source = new StreamSource(document.newInputStream());
Result result = new SAXResult(fop.getDefaultHandler());
if (LOGGER.isDebugEnabled()) {
try {
File tempFile = File.createTempFile(TEMP_FILE_PREFIX, ".xml");
StreamResult debugResult = new StreamResult(tempFile);
transformer.transform(source, debugResult);
String xslResult = XmlObject.Factory.parse(tempFile).xmlText();
LOGGER.debug("xsl-fo input (locale '{}'): {}", i18n.getTwoDigitsLanguageCode(), xslResult);
} catch (IOException | TransformerException | XmlException e) {
LOGGER.error("Could not debug XSL result output!", e);
}
}
// XXX debug, diagram is not embedded
transformer.transform(source, result);
} catch (FOPException e) {
throw new IoParseException("Failed to create Formatting Object Processor (FOP)", e);
} catch (ConfigurationException e) {
throw new IoParseException("Failed to read config for Formatting Object Processor (FOP)", e);
} catch (TransformerConfigurationException e) {
throw new IoParseException("Invalid transform configuration. Inspect xslt!", e);
} catch (TransformerException e) {
throw new IoParseException("Could not generate PDF report!", e);
}
}
use of org.apache.fop.apps.FOPException in project mycore by MyCoRe-Org.
the class MCRFoFormatterFOP method transform.
@Override
public void transform(MCRContent input, OutputStream out) throws TransformerException, IOException {
try {
final FOUserAgent userAgent = fopFactory.newFOUserAgent();
userAgent.setProducer(MessageFormat.format("MyCoRe {0} ({1})", MCRCoreVersion.getCompleteVersion(), userAgent.getProducer()));
final Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, userAgent, out);
final Source src = input.getSource();
final Result res = new SAXResult(fop.getDefaultHandler());
Transformer transformer = getTransformerFactory().newTransformer();
transformer.transform(src, res);
} catch (FOPException e) {
throw new TransformerException(e);
} finally {
out.close();
}
}
Aggregations