FopFactory fopFactory = FopFactory.newInstance(); FOUserAgent userAgent = fopFactory.newFOUserAgent(); TransformerFactory factory = org.apache.xalan.processor.TransformerFactoryImpl.newInstance(); Transformer transformer; OutputStream out = new BufferedOutputStream(pdfOut[0].setBinaryStream(1L)); try { Fop fop = fopFactory.newFop(MimeConstants.MIME_PNG /* or MIME_TIFF */, userAgent, out); transformer = factory.newTransformer(new StreamSource(xsltIn.getCharacterStream())); Source src = new StreamSource(xmlIn.getCharacterStream()); Result res = new SAXResult(fop.getDefaultHandler()); transformer.transform(src, res); } catch ...
protected FopFactory createFopFactory() { return FopFactory.newInstance(); }
fopFactory.setUserConfig(fopConfiguration.getFile()); fopFactory.setBaseURL(basePathRef.getFile().getParentFile().getCanonicalPath()); FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); foUserAgent.setBaseURL(fopFactory.getBaseURL()); fopFactory.getFontManager().setFontBaseURL(fontBase.getFile().getParentFile().getCanonicalPath());
// configure fopFactory as desired FopFactory fopFactory = FopFactory.newInstance(); FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); fopFactory.setUserConfig(new File("fop.xml"));
final FopFactory fopFactory = FopFactory.newInstance(); fopFactory.getFontManager().setFontBaseURL(getFontResourcePath()); } catch (final MalformedURLException ex) { log.error(ex.getMessage(), ex); final FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); try { foUserAgent.getFactory().getFontManager().setFontBaseURL(getFontResourcePath()); } catch (final MalformedURLException ex) { log.error(ex.getMessage(), ex); try { final Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, baos); throw new InternalErrorException(); xltStreamSource.setSystemId(url); final Transformer transformer = factory.newTransformer(xltStreamSource); transformer.setParameter(entry.getKey(), entry.getValue()); final Source src = new StreamSource(xmlDataReader); final Result res = new SAXResult(fop.getDefaultHandler());
fopFactory.setURIResolver(new LibraryURIResolver()); FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); ClassPathResource xsltCPR = new ClassPathResource("org/sakaiproject/sitestats/config/fop/"+XML_FO_XSL_FILE); InputStream xslt = xsltCPR.getInputStream(); TransformerFactory factory = TransformerFactory.newInstance(); cachedXmlFoXSLT = factory.newTemplates(new StreamSource(xslt)); Result res = new SAXResult(fop.getDefaultHandler()); transformer.transform(src, res);
protected void transformToStream(InputStream templateStream, ParamsTree<String, String> documentParams, OutputStream outStream) throws ExportServiceException { // the XML file from which we take the name Source source = this.mapParamsToSource(documentParams); // creation of transform source StreamSource transformSource = new StreamSource(templateStream); // create an instance of fop factory FopFactory fopFactory = FopFactory.newInstance(); // a user agent is needed for transformation FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); // to store output try { Transformer xslfoTransformer = this.getTransformer(transformSource); // Construct fop with desired output format Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, outStream); // Resulting SAX events (the generated FO) // must be piped through to FOP Result res = new SAXResult(fop.getDefaultHandler()); // Start XSLT transformation and FOP processing // everything will happen here.. xslfoTransformer.transform(source, res); } catch (FOPException e) { FOPPDFTransformer.log.error(e.getMessage(), e); throw new ExportServiceException("Error using FOP to open the template", e); } catch (TransformerException e) { FOPPDFTransformer.log.error(e.getMessage(), e); throw new ExportServiceException("Error generating pdf from template and data source", e); } }
fop = fopFactory.newFop(MimeConstants.MIME_PDF, userAgent, out); } else { fop = fopFactory.newFop(MimeConstants.MIME_PDF, out); Result res = new SAXResult(fop.getDefaultHandler()); transformer.transform(source, res);
foUserAgent = fopFactory.newFOUserAgent(); PDFEncryptionParams pdfEncryptionParams = new PDFEncryptionParams(userPassword, ownerPassword, allowPrint, allowCopyContent, allowEditContent, allowEditAnnotations, encryptMetadata); pdfEncryptionParams.setAllowFillInForms(allowFillInForms); StreamSource src = new StreamSource(reader); ByteArrayOutputStream out = new ByteArrayOutputStream();
/** * Generate a document, given an initialized Fop object * @param userAgent the user agent * @param outputFormat the output format to generate (MIME type, see MimeConstants) * @param out the output stream to write the generated output to (may be null if not applicable) * @throws FOPException in case of an error during processing */ public void renderTo(FOUserAgent userAgent, String outputFormat, OutputStream out) throws FOPException { String baseURL = null; try { baseURL = new File(sourcefile.getAbsolutePath()) .getParentFile().toURI().toURL().toExternalForm(); } catch (Exception e) { baseURL = ""; } FopFactory factory = new FopFactoryBuilder(URI.create(baseURL)) .build(); Fop fop; if (out != null) { fop = factory.newFop(outputFormat, userAgent, out); } else { fop = factory.newFop(outputFormat, userAgent); } // Resulting SAX events (the generated FO) must be piped through to FOP Result res = new SAXResult(fop.getDefaultHandler()); transformTo(res); }
.getResourceAsStream(configfile); Configuration cfg = cfgBuild.build(stream); final FopFactory ff = FopFactory.newInstance(); ff.setUserConfig(cfg); FOUserAgent userAgent = ff.newFOUserAgent(); .getString("serverUrl")); fop = ff.newFop(mimeType, userAgent, outputStream); dh = fop.getDefaultHandler();
private ContentHandler getFOPContentHandler(OutputStream out) throws FOPException { Fop fop = fopFactory.newFop(this.outputMime, out); return fop.getDefaultHandler(); }
/** Returns a new Fop instance. Note: FOP documentation recommends using * a Fop instance for one transform run only. * @param out The target (result) OutputStream instance * @param outputFormat Optional output format, defaults to "application/pdf" * @param foUserAgent FOUserAgent object which may contains encryption-params in render options * @return Fop instance */ public static Fop createFopInstance(OutputStream out, String outputFormat, FOUserAgent foUserAgent) throws FOPException { if (UtilValidate.isEmpty(outputFormat)) { outputFormat = MimeConstants.MIME_PDF; } if (UtilValidate.isEmpty(foUserAgent)) { FopFactory fopFactory = getFactoryInstance(); foUserAgent = fopFactory.newFOUserAgent(); } Fop fop; if (out != null) { fop = fopFactory.newFop(outputFormat, foUserAgent, out); } else { fop = fopFactory.newFop(outputFormat, foUserAgent); } return fop; }
@Override public void run() { OutputStream outstream = null; try { InputHandler input = new InputHandler(inputFile); FOUserAgent userAgent = FopFactory.newInstance().newFOUserAgent(); userAgent.setBaseURL(resourceBaseDir); outstream = new BufferedOutputStream(new FileOutputStream(outputFile)); input.renderTo(userAgent, MimeConstants.MIME_PDF, outstream); logger.info("Successfully rendered {}", outputFile); } catch (Exception e) { logger.error("Unknown Exception in FOP", e); throw new RuntimeException(e); } finally { try { if (outstream != null) outstream.close(); } catch (Exception e) { logger.error("Unknown Exception in FOP", e); } } } }
/** * @see com.isotrol.impe3.palette.pdf.fop.FopService#newUserAgent() */ public FOUserAgent newUserAgent() { return fopFactory.newFOUserAgent(); }
/** * @param factory * @param agent * @param out * @return * @throws FOPException */ protected Fop createFop(FopFactory factory, FOUserAgent agent, OutputStream out) throws FOPException { return factory.newFop(getContentType(), agent, out); }
File userConfigFile = FileUtil.getFile(configFilePath.getFile()); if (userConfigFile.exists()) { fopFactory = FopFactory.newInstance(userConfigFile); } else { Debug.logWarning("FOP configuration file not found: " + userConfigFile, module); fopFactory.getFontManager().setResourceResolver(ResourceResolverFactory.createDefaultInternalResourceResolver(fontBaseFile.toURI())); } else { Debug.logWarning("FOP font base URL not found: " + fontBaseFile, module); Debug.logInfo("FOP FontBaseURL: " + fopFactory.getFontManager().getResourceResolver().getBaseURI(), module); } catch (Exception e) { Debug.logWarning(e, "Error reading FOP configuration: ", module);
private FontInfo setupFonts(FopFactory fopFactory, String mime, FontEventListener listener) throws FOPException { FOUserAgent userAgent = fopFactory.newFOUserAgent(); //The document handler is only instantiated to get access to its configurator! IFDocumentHandler documentHandler = fopFactory.getRendererFactory().createDocumentHandler(userAgent, mime); IFDocumentHandlerConfigurator configurator = documentHandler.getConfigurator(); FontInfo fontInfo = new FontInfo(); configurator.setupFontInfo(documentHandler.getMimeType(), fontInfo); documentHandler.setFontInfo(fontInfo); return fontInfo; }
/** @see FopFactory#getFontManager() */ public FontManager getFontManager() { return factory.getFontManager(); }
/** * {@inheritDoc} */ protected void render(Source src, Transformer transformer, HttpServletResponse response) throws FOPException, TransformerException, IOException { FOUserAgent foUserAgent = getFOUserAgent(); //Setup FOP Fop fop = fopFactory.newFop(MimeConstants.MIME_FOP_PRINT, foUserAgent); //Make sure the XSL transformation's result is piped through to FOP Result res = new SAXResult(fop.getDefaultHandler()); //Start the transformation and rendering process transformer.transform(src, res); //Return the result reportOK(response); }