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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations