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 ...
/** * Returns a new {@link Fop} instance. Use this factory method if your output type * requires an output stream and you want to configure this very rendering run, * i.e. if you want to set some metadata like the title and author of the document * you want to render. In that case, create a new {@link FOUserAgent} instance * using {@link org.apache.fop.apps.FopFactory#newFOUserAgent() newFOUserAgent()}. * <p> * MIME types are used to select the output format (ex. "application/pdf" for PDF). You can * use the constants defined in {@link MimeConstants}. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf"). * @param stream the output stream * @return the new Fop instance * @throws FOPException when the constructor fails */ public Fop newFop(String outputFormat, OutputStream stream) throws FOPException { return new Fop(outputFormat, this, stream); }
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()); 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()); transformer.transform(src, res); } catch (final FOPException ex) { log.error(ex.getMessage(), ex);
/** 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); } }
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); } }
/** * {@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); }
Fop fop = fopFactory.newFop(MimeConstants.MIME_POSTSCRIPT, outStream); Transformer transformer = transformerFactory.newTransformer(); Source source = new StreamSource(new StringReader(foAsString)); Result result = new SAXResult(fop.getDefaultHandler());
/** * 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); }
/** * 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 { Fop fop; if (out != null) { fop = userAgent.newFop(outputFormat, out); } else { fop = userAgent.newFop(outputFormat); } // 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(); dh = fop.getDefaultHandler();
private ContentHandler getFOPContentHandler(OutputStream out) throws FOPException { Fop fop = fopFactory.newFop(this.outputMime, out); return fop.getDefaultHandler(); }
/** * @see org.pivot4j.ui.RenderCallback#startRender(org.pivot4j.ui.RenderContext) */ @Override public void startRender(TableRenderContext context) { this.fopFactory = createFopFactory(); this.userAgent = createUserAgent(fopFactory); try { Fop fop = createFop(getFopFactory(), getUserAgent(), getOutputStream()); this.documentHandler = fop.getDefaultHandler(); this.documentHandler.startDocument(); this.documentHandler.startElement(FOElementMapping.URI, "root", "root", new AttributesImpl()); startLayoutDefinition(context); endLayoutDefinition(context); startPageSequence(context); } catch (SAXException e) { throw new PivotException(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);
@Override public void setOutputStream(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); } }
public void generatePdf(final StreamSource source, final File outputFile) throws IOException, FOPException, TransformerException { try (final FileOutputStream fOut = new FileOutputStream(outputFile); final BufferedOutputStream bOut = new BufferedOutputStream(fOut)) { FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); final Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, bOut); final TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); // identity transformer // Resulting SAX events (the generated FO) must be piped through to FOP final Result res = new SAXResult(fop.getDefaultHandler()); // Start XSLT transformation and FOP processing transformer.transform(source, res); } }
/** * Renders an input file (XML or XSL-FO) into a PDF file. It uses the JAXP * transformer given to optionally transform the input document to XSL-FO. * The transformer may be an identity transformer in which case the input * must already be XSL-FO. The PDF is written to a byte array that is * returned as the method's result. * @param src Input XML or XSL-FO * @param transformer Transformer to use for optional transformation * @param response HTTP response object * @throws FOPException If an error occurs during the rendering of the * XSL-FO * @throws TransformerException If an error occurs during XSL * transformation * @throws IOException In case of an I/O problem */ protected void render(Source src, Transformer transformer, HttpServletResponse response) throws FOPException, TransformerException, IOException { FOUserAgent foUserAgent = getFOUserAgent(); //Setup output ByteArrayOutputStream out = new ByteArrayOutputStream(); //Setup FOP Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); //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 sendPDF(out.toByteArray(), response); }
@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); } }
public void export(OutputStream out) throws FOPException, IOException, TransformerException { FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI()); Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out); // Setup XSLT TransformerFactory Factory = TransformerFactory.newInstance(); Transformer transformer = Factory.newTransformer(xsltSource); // Set the value of a <param> in the stylesheet transformer.setParameter("versionParam", "2.0"); // Setup input for XSLT transformation Reader reader = composeXml(); Source src = new StreamSource(reader); // Resulting SAX events (the generated FO) must be piped through to // FOP Result res = new SAXResult(fop.getDefaultHandler()); // Start XSLT transformation and FOP processing transformer.transform(src, res); reader.close(); out.flush(); }
public void export(OutputStream out) throws FOPException, IOException, TransformerException { FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI()); Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out); // Setup XSLT TransformerFactory Factory = TransformerFactory.newInstance(); Transformer transformer = Factory.newTransformer(xsltSource); // Set the value of a <param> in the stylesheet transformer.setParameter("versionParam", "2.0"); // Setup input for XSLT transformation Reader reader = composeXml(); Source src = new StreamSource(reader); // Resulting SAX events (the generated FO) must be piped through to // FOP Result res = new SAXResult(fop.getDefaultHandler()); // Start XSLT transformation and FOP processing transformer.transform(src, res); reader.close(); out.flush(); }