Search in sources :

Example 1 with Fop

use of org.apache.fop.apps.Fop in project opennms by OpenNMS.

the class OnmsPdfViewResolver method resolveView.

@Override
public void resolveView(ServletRequest request, ServletResponse response, Preferences preferences, Object viewData) throws Exception {
    InputStream is = new ByteArrayInputStream(((String) viewData).getBytes(StandardCharsets.UTF_8));
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    FopFactory fopFactory = FopFactory.newInstance();
    fopFactory.setStrictValidation(false);
    FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
    TransformerFactory tfact = TransformerFactory.newInstance();
    Transformer transformer = tfact.newTransformer();
    Source src = new StreamSource(is);
    Result res = new SAXResult(fop.getDefaultHandler());
    transformer.transform(src, res);
    byte[] contents = out.toByteArray();
    response.setContentLength(contents.length);
    response.getOutputStream().write(contents);
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) FOUserAgent(org.apache.fop.apps.FOUserAgent) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Fop(org.apache.fop.apps.Fop) StreamSource(javax.xml.transform.stream.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FopFactory(org.apache.fop.apps.FopFactory) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) SAXResult(javax.xml.transform.sax.SAXResult) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 2 with Fop

use of org.apache.fop.apps.Fop in project pcgen by PCGen.

the class FopTask method run.

/**
	 * Run the FO to PDF/AWT conversion. This automatically closes any provided OutputStream for
	 * this FopTask.
	 */
@Override
public void run() {
    try (OutputStream out = outputStream) {
        userAgent.setProducer("PC Gen Character Generator");
        userAgent.setAuthor(System.getProperty("user.name"));
        userAgent.setCreationDate(new Date());
        userAgent.getEventBroadcaster().addEventListener(new FOPEventListener());
        String mimeType;
        if (renderer != null) {
            userAgent.setKeywords("PCGEN FOP PREVIEW");
            mimeType = MimeConstants.MIME_FOP_AWT_PREVIEW;
        } else {
            userAgent.setKeywords("PCGEN FOP PDF");
            mimeType = MimeConstants.MIME_PDF;
        }
        Fop fop;
        if (out != null) {
            fop = FOP_FACTORY.newFop(mimeType, userAgent, out);
        } else {
            fop = FOP_FACTORY.newFop(mimeType, userAgent);
        }
        Transformer transformer;
        if (xsltSource != null) {
            transformer = TRANS_FACTORY.newTransformer(xsltSource);
        } else {
            // identity transformer		
            transformer = TRANS_FACTORY.newTransformer();
        }
        transformer.setErrorListener(new FOPErrorListener());
        transformer.transform(inputSource, new SAXResult(fop.getDefaultHandler()));
    } catch (TransformerException | FOPException | IOException e) {
        errorBuilder.append(e.getMessage()).append(Constants.LINE_SEPARATOR);
        Logging.errorPrint("Exception in FopTask:run", e);
    } catch (RuntimeException ex) {
        errorBuilder.append(ex.getMessage()).append(Constants.LINE_SEPARATOR);
        Logging.errorPrint("Unexpected exception in FopTask:run: ", ex);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) Fop(org.apache.fop.apps.Fop) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Date(java.util.Date) FOPException(org.apache.fop.apps.FOPException) SAXResult(javax.xml.transform.sax.SAXResult) TransformerException(javax.xml.transform.TransformerException)

Example 3 with Fop

use of org.apache.fop.apps.Fop in project series-rest-api by 52North.

the class PDFReportGenerator method encodeAndWriteTo.

@Override
public void encodeAndWriteTo(DataCollection<QuantityData> data, OutputStream stream) throws IoParseException {
    try {
        generateOutput(data);
        DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
        Configuration cfg = cfgBuilder.build(document.newInputStream());
        FopFactory fopFactory = new FopFactoryBuilder(baseURI).setConfiguration(cfg).build();
        final String mimeType = MimeType.APPLICATION_PDF.getMimeType();
        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 (SAXException | ConfigurationException | IOException 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);
    }
}
Also used : IoParseException(org.n52.io.IoParseException) DefaultConfigurationBuilder(org.apache.avalon.framework.configuration.DefaultConfigurationBuilder) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) Configuration(org.apache.avalon.framework.configuration.Configuration) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Fop(org.apache.fop.apps.Fop) StreamSource(javax.xml.transform.stream.StreamSource) FopFactory(org.apache.fop.apps.FopFactory) IOException(java.io.IOException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) SAXException(org.xml.sax.SAXException) FopFactoryBuilder(org.apache.fop.apps.FopFactoryBuilder) FOPException(org.apache.fop.apps.FOPException) SAXResult(javax.xml.transform.sax.SAXResult) ConfigurationException(org.apache.avalon.framework.configuration.ConfigurationException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) XmlException(org.apache.xmlbeans.XmlException) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 4 with Fop

use of org.apache.fop.apps.Fop in project camel by apache.

the class FopProducer method transform.

private OutputStream transform(FOUserAgent userAgent, String outputFormat, Source src) throws FOPException, TransformerException {
    OutputStream out = new ByteArrayOutputStream();
    Fop fop = fopFactory.newFop(outputFormat, userAgent, out);
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    Result res = new SAXResult(fop.getDefaultHandler());
    transformer.transform(src, res);
    return out;
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) SAXResult(javax.xml.transform.sax.SAXResult) Fop(org.apache.fop.apps.Fop) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult)

Example 5 with Fop

use of org.apache.fop.apps.Fop in project opennms by OpenNMS.

the class PDFReportRenderer method render.

/**
 * <p>render</p>
 *
 * @param in a {@link java.io.Reader} object.
 * @param out a {@link java.io.OutputStream} object.
 * @param xslt a {@link java.io.Reader} object.
 * @throws org.opennms.reporting.availability.render.ReportRenderException if any.
 */
public void render(final Reader in, final OutputStream out, final Reader xslt) throws ReportRenderException {
    try {
        final FopFactory fopFactory = FopFactory.newInstance();
        fopFactory.setStrictValidation(false);
        final Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
        final TransformerFactory tfact = TransformerFactory.newInstance();
        final Transformer transformer = tfact.newTransformer(new StreamSource(xslt));
        transformer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
        final StreamSource streamSource = new StreamSource(in);
        transformer.transform(streamSource, new SAXResult(fop.getDefaultHandler()));
    } catch (final Exception e) {
        throw new ReportRenderException(e);
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) SAXResult(javax.xml.transform.sax.SAXResult) Fop(org.apache.fop.apps.Fop) StreamSource(javax.xml.transform.stream.StreamSource) FopFactory(org.apache.fop.apps.FopFactory)

Aggregations

Fop (org.apache.fop.apps.Fop)24 StreamSource (javax.xml.transform.stream.StreamSource)17 SAXResult (javax.xml.transform.sax.SAXResult)16 Transformer (javax.xml.transform.Transformer)15 Result (javax.xml.transform.Result)12 FopFactory (org.apache.fop.apps.FopFactory)11 Source (javax.xml.transform.Source)10 TransformerFactory (javax.xml.transform.TransformerFactory)10 File (java.io.File)8 FileOutputStream (java.io.FileOutputStream)7 IOException (java.io.IOException)7 OutputStream (java.io.OutputStream)7 FOPException (org.apache.fop.apps.FOPException)7 BufferedOutputStream (java.io.BufferedOutputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 StringReader (java.io.StringReader)5 TransformerException (javax.xml.transform.TransformerException)5 FOUserAgent (org.apache.fop.apps.FOUserAgent)5 SAXException (org.xml.sax.SAXException)5 TemplateException (freemarker.template.TemplateException)4