Search in sources :

Example 1 with Configuration

use of org.apache.fop.configuration.Configuration in project megameklab by MegaMek.

the class PrintRecordSheet method exportPDF.

public InputStream exportPDF(int pageNumber, PageFormat pageFormat) throws TranscoderException, SAXException, IOException, ConfigurationException {
    createDocument(pageNumber + firstPage, pageFormat, true);
    DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
    Configuration cfg = cfgBuilder.build(getClass().getResourceAsStream("fop-config.xml"));
    PDFTranscoder transcoder = new PDFTranscoder();
    transcoder.configure(cfg);
    transcoder.addTranscodingHint(PDFTranscoder.KEY_AUTO_FONTS, false);
    TranscoderInput input = new TranscoderInput(getSVGDocument());
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    TranscoderOutput transOutput = new TranscoderOutput(output);
    // The default for the transcoder is 96 dpi, but the source document is 72 dpi.
    transcoder.addTranscodingHint(PDFTranscoder.KEY_PIXEL_UNIT_TO_MILLIMETER, 0.352778f);
    transcoder.transcode(input, transOutput);
    if (callback != null) {
        callback.accept(pageNumber + firstPage);
    }
    return new ByteArrayInputStream(output.toByteArray());
}
Also used : DefaultConfigurationBuilder(org.apache.fop.configuration.DefaultConfigurationBuilder) TranscoderOutput(org.apache.batik.transcoder.TranscoderOutput) Configuration(org.apache.fop.configuration.Configuration) TranscoderInput(org.apache.batik.transcoder.TranscoderInput) PDFTranscoder(org.apache.fop.svg.PDFTranscoder)

Example 2 with Configuration

use of org.apache.fop.configuration.Configuration 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);
    }
}
Also used : IoParseException(org.n52.io.IoParseException) DefaultConfigurationBuilder(org.apache.fop.configuration.DefaultConfigurationBuilder) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) Configuration(org.apache.fop.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) URI(java.net.URI) 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) FopFactoryBuilder(org.apache.fop.apps.FopFactoryBuilder) FOPException(org.apache.fop.apps.FOPException) SAXResult(javax.xml.transform.sax.SAXResult) ConfigurationException(org.apache.fop.configuration.ConfigurationException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) XmlException(org.apache.xmlbeans.XmlException) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Aggregations

Configuration (org.apache.fop.configuration.Configuration)2 DefaultConfigurationBuilder (org.apache.fop.configuration.DefaultConfigurationBuilder)2 File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Result (javax.xml.transform.Result)1 Source (javax.xml.transform.Source)1 Transformer (javax.xml.transform.Transformer)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 SAXResult (javax.xml.transform.sax.SAXResult)1 StreamResult (javax.xml.transform.stream.StreamResult)1 StreamSource (javax.xml.transform.stream.StreamSource)1 TranscoderInput (org.apache.batik.transcoder.TranscoderInput)1 TranscoderOutput (org.apache.batik.transcoder.TranscoderOutput)1 FOPException (org.apache.fop.apps.FOPException)1 Fop (org.apache.fop.apps.Fop)1 FopFactory (org.apache.fop.apps.FopFactory)1 FopFactoryBuilder (org.apache.fop.apps.FopFactoryBuilder)1