Search in sources :

Example 1 with FOPException

use of org.apache.fop.apps.FOPException 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 2 with FOPException

use of org.apache.fop.apps.FOPException 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 3 with FOPException

use of org.apache.fop.apps.FOPException in project grafikon by jub77.

the class DrawOutput method processPdf.

private void processPdf(Collection<Image> images, OutputStream stream, DrawLayout layout) throws OutputException {
    PDFDocumentGraphics2D g2d;
    try {
        g2d = new PDFDocumentGraphics2D();
        g2d.setGraphicContext(new GraphicContext());
        FopFactory fopFactory = PdfTransformer.createFopFactory();
        FOUserAgent userAgent = fopFactory.newFOUserAgent();
        FontConfig fc = userAgent.getRendererConfig(MimeConstants.MIME_PDF, new PDFRendererConfigParser()).getFontInfoConfig();
        FontManager fontManager = fopFactory.getFontManager();
        DefaultFontConfigurator fontInfoConfigurator = new DefaultFontConfigurator(fontManager, null, false);
        List<EmbedFontInfo> fontInfoList = fontInfoConfigurator.configure(fc);
        FontInfo fontInfo = new FontInfo();
        FontSetup.setup(fontInfo, fontInfoList, userAgent.getResourceResolver(), fontManager.isBase14KerningEnabled());
        g2d.setFontInfo(fontInfo);
        g2d.setFont(new Font("SansCondensed", Font.PLAIN, g2d.getFont().getSize()));
        List<Dimension> sizes = this.getSizes(images, g2d);
        Dimension size = this.getTotalSize(sizes, layout);
        g2d.setupDocument(stream, size.width, size.height);
        this.drawImages(sizes, images, g2d, layout);
        g2d.finish();
    } catch (IOException | FOPException e) {
        throw new OutputException(e.getMessage(), e);
    }
}
Also used : EmbedFontInfo(org.apache.fop.fonts.EmbedFontInfo) GraphicContext(org.apache.xmlgraphics.java2d.GraphicContext) PDFRendererConfigParser(org.apache.fop.render.pdf.PDFRendererConfig.PDFRendererConfigParser) FOUserAgent(org.apache.fop.apps.FOUserAgent) FontConfig(org.apache.fop.fonts.FontConfig) PDFDocumentGraphics2D(org.apache.fop.svg.PDFDocumentGraphics2D) FopFactory(org.apache.fop.apps.FopFactory) Dimension(java.awt.Dimension) IOException(java.io.IOException) FontManager(org.apache.fop.fonts.FontManager) Font(java.awt.Font) FOPException(org.apache.fop.apps.FOPException) OutputException(net.parostroj.timetable.output2.OutputException) EmbedFontInfo(org.apache.fop.fonts.EmbedFontInfo) FontInfo(org.apache.fop.fonts.FontInfo) DefaultFontConfigurator(org.apache.fop.fonts.DefaultFontConfigurator)

Example 4 with FOPException

use of org.apache.fop.apps.FOPException in project syncope by apache.

the class FopSerializer method setOutputStream.

@Override
public void setOutputStream(final OutputStream outputStream) {
    try {
        Fop fop = FOP_FACTORY.newFop(this.outputFormat, outputStream);
        ContentHandler fopContentHandler = fop.getDefaultHandler();
        this.setContentHandler(fopContentHandler);
    } catch (FOPException e) {
        throw new ProcessingException("Impossible to initialize FOPSerializer", e);
    }
}
Also used : FOPException(org.apache.fop.apps.FOPException) Fop(org.apache.fop.apps.Fop) ContentHandler(org.xml.sax.ContentHandler) ProcessingException(org.apache.cocoon.pipeline.ProcessingException)

Example 5 with FOPException

use of org.apache.fop.apps.FOPException in project ofbiz-framework by apache.

the class ApacheFopWorker method transform.

/**
 * Transform an xsl-fo StreamSource to the specified output format.
 * @param src The xsl-fo StreamSource instance
 * @param stylesheet Optional stylesheet StreamSource instance
 * @param fop
 */
public static void transform(StreamSource src, StreamSource stylesheet, Fop fop) throws FOPException {
    Result res = new SAXResult(fop.getDefaultHandler());
    try {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer;
        if (stylesheet == null) {
            transformer = factory.newTransformer();
        } else {
            transformer = factory.newTransformer(stylesheet);
        }
        transformer.setURIResolver(new LocalResolver(transformer.getURIResolver()));
        transformer.transform(src, res);
    } catch (Exception e) {
        throw new FOPException(e);
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) FOPException(org.apache.fop.apps.FOPException) SAXResult(javax.xml.transform.sax.SAXResult) TransformerException(javax.xml.transform.TransformerException) FOPException(org.apache.fop.apps.FOPException) IOException(java.io.IOException) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult)

Aggregations

FOPException (org.apache.fop.apps.FOPException)8 Transformer (javax.xml.transform.Transformer)6 TransformerException (javax.xml.transform.TransformerException)6 SAXResult (javax.xml.transform.sax.SAXResult)6 Fop (org.apache.fop.apps.Fop)6 IOException (java.io.IOException)5 Result (javax.xml.transform.Result)4 Source (javax.xml.transform.Source)4 TransformerFactory (javax.xml.transform.TransformerFactory)3 StreamSource (javax.xml.transform.stream.StreamSource)3 FopFactory (org.apache.fop.apps.FopFactory)3 File (java.io.File)2 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)2 StreamResult (javax.xml.transform.stream.StreamResult)2 FOUserAgent (org.apache.fop.apps.FOUserAgent)2 FopFactoryBuilder (org.apache.fop.apps.FopFactoryBuilder)2 XmlException (org.apache.xmlbeans.XmlException)2 IoParseException (org.n52.io.IoParseException)2 Dimension (java.awt.Dimension)1 Font (java.awt.Font)1