Search in sources :

Example 6 with FopFactory

use of org.apache.fop.apps.FopFactory in project tutorials by eugenp.

the class ApacheFOPConvertHTMLIntegrationTest method fromFOFileToPDF.

private void fromFOFileToPDF() throws Exception {
    final FopFactory fopFactory = FopFactory.newInstance();
    final OutputStream outStream = new BufferedOutputStream(new FileOutputStream(new File(output_html2fo)));
    final Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, outStream);
    final TransformerFactory factory = TransformerFactory.newInstance();
    final Transformer transformer = factory.newTransformer();
    final Source src = new StreamSource(new FileInputStream(foFile));
    final Result res = new SAXResult(fop.getDefaultHandler());
    transformer.transform(src, res);
    outStream.close();
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) Fop(org.apache.fop.apps.Fop) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) StreamSource(javax.xml.transform.stream.StreamSource) FopFactory(org.apache.fop.apps.FopFactory) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) FileInputStream(java.io.FileInputStream) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) DOMResult(javax.xml.transform.dom.DOMResult) SAXResult(javax.xml.transform.sax.SAXResult) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File)

Example 7 with FopFactory

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

the class ScreenFopViewHandler method render.

/**
 * @see org.apache.ofbiz.webapp.view.ViewHandler#render(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
@Override
public void render(String name, String page, String info, String contentType, String encoding, HttpServletRequest request, HttpServletResponse response) throws ViewHandlerException {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    VisualTheme visualTheme = UtilHttp.getVisualTheme(request);
    ModelTheme modelTheme = visualTheme.getModelTheme();
    // render and obtain the XSL-FO
    Writer writer = new StringWriter();
    try {
        ScreenStringRenderer screenStringRenderer = new MacroScreenRenderer(modelTheme.getType(getName()), modelTheme.getScreenRendererLocation(getName()));
        FormStringRenderer formStringRenderer = new MacroFormRenderer(modelTheme.getFormRendererLocation(getName()), request, response);
        // TODO: uncomment these lines when the renderers are implemented
        // TreeStringRenderer treeStringRenderer = new MacroTreeRenderer(modelTheme.getTreeRendererLocation(getName()), writer);
        // MenuStringRenderer menuStringRenderer = new MacroMenuRenderer(modelTheme.getMenuRendererLocation(getName()), writer);
        ScreenRenderer screens = new ScreenRenderer(writer, null, screenStringRenderer);
        screens.populateContextForRequest(request, response, servletContext);
        // this is the object used to render forms from their definitions
        screens.getContext().put("formStringRenderer", formStringRenderer);
        screens.getContext().put("simpleEncoder", UtilCodec.getEncoder(modelTheme.getEncoder(getName())));
        screens.render(page);
    } catch (IOException | GeneralException | SAXException | ParserConfigurationException | TemplateException e) {
        renderError("Problems with the response writer/output stream", e, "[Not Yet Rendered]", request, response);
        return;
    }
    // set the input source (XSL-FO) and generate the output stream of contentType
    String screenOutString = writer.toString();
    if (!screenOutString.startsWith("<?xml")) {
        screenOutString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + screenOutString;
    }
    if (Debug.verboseOn())
        Debug.logVerbose("XSL:FO Screen Output: " + screenOutString, module);
    if (UtilValidate.isEmpty(contentType)) {
        contentType = modelTheme.getContentType(getName());
    }
    // get encryption related parameters
    FOUserAgent foUserAgent = null;
    String userPassword = request.getParameter("userPassword");
    String ownerPassword = request.getParameter("ownerPassword");
    boolean allowPrint = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowPrint")) ? ApacheFopWorker.getAllowPrintDefault() : request.getParameter("allowPrint"));
    boolean allowCopyContent = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowCopyContent")) ? ApacheFopWorker.getAllowCopyContentDefault() : request.getParameter("allowCopyContent"));
    boolean allowEditContent = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowEditContent")) ? ApacheFopWorker.getAllowEditContentDefault() : request.getParameter("allowEditContent"));
    boolean allowEditAnnotations = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowEditAnnotations")) ? ApacheFopWorker.getAllowEditAnnotationsDefault() : request.getParameter("allowEditAnnotations"));
    if (UtilValidate.isNotEmpty(userPassword) || UtilValidate.isNotEmpty(ownerPassword) || !allowPrint || !allowCopyContent || allowEditContent || !allowEditAnnotations) {
        int encryptionLength = 128;
        try {
            encryptionLength = Integer.parseInt(request.getParameter("encryption-length"));
        } catch (NumberFormatException e) {
            try {
                encryptionLength = Integer.parseInt(ApacheFopWorker.getEncryptionLengthDefault());
            } catch (NumberFormatException e1) {
            // ignore
            }
        }
        boolean encryptMetadata = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("encrypt-metadata")) ? ApacheFopWorker.getEncryptMetadataDefault() : request.getParameter("encrypt-metadata"));
        boolean allowFillInForms = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowFillInForms")) ? ApacheFopWorker.getAllowFillInFormsDefault() : request.getParameter("allowFillInForms"));
        boolean allowAccessContent = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowAccessContent")) ? ApacheFopWorker.getAllowAccessContentDefault() : request.getParameter("allowAccessContent"));
        boolean allowAssembleDocument = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowAssembleDocument")) ? ApacheFopWorker.getAllowAssembleDocumentDefault() : request.getParameter("allowAssembleDocument"));
        boolean allowPrintHq = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowPrintHq")) ? ApacheFopWorker.getAllowPrintHqDefault() : request.getParameter("allowPrintHq"));
        FopFactory fopFactory = ApacheFopWorker.getFactoryInstance();
        foUserAgent = fopFactory.newFOUserAgent();
        PDFEncryptionParams pdfEncryptionParams = new PDFEncryptionParams(userPassword, ownerPassword, allowPrint, allowCopyContent, allowEditContent, allowEditAnnotations, encryptMetadata);
        pdfEncryptionParams.setAllowFillInForms(allowFillInForms);
        pdfEncryptionParams.setAllowAccessContent(allowAccessContent);
        pdfEncryptionParams.setAllowAssembleDocument(allowAssembleDocument);
        pdfEncryptionParams.setAllowPrintHq(allowPrintHq);
        pdfEncryptionParams.setEncryptionLengthInBits(encryptionLength);
        foUserAgent.getRendererOptions().put(PDFEncryptionOption.ENCRYPTION_PARAMS, pdfEncryptionParams);
    }
    Reader reader = new StringReader(screenOutString);
    StreamSource src = new StreamSource(reader);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    /* Debug area, uncomment this to view the xml file generate before analyse by fop
        try { 
                java.io.FileWriter fw = new java.io.FileWriter(new java.io.File("/tmp/temp.xsl.fo"));
                fw.write(screenOutString);
                fw.close();
            } catch (IOException e) {
                Debug.logError(e, "Couldn't save xls debug file: " + e.toString(), module);
            }
        */
    try {
        Fop fop = ApacheFopWorker.createFopInstance(out, contentType, foUserAgent);
        ApacheFopWorker.transform(src, null, fop);
    } catch (Exception e) {
        renderError("Unable to transform FO file", e, screenOutString, request, response);
        return;
    }
    // set the content type and length
    response.setContentType(contentType);
    response.setContentLength(out.size());
    // write to the browser
    try {
        out.writeTo(response.getOutputStream());
        response.getOutputStream().flush();
    } catch (IOException e) {
        renderError("Unable to write to OutputStream", e, screenOutString, request, response);
    }
}
Also used : FOUserAgent(org.apache.fop.apps.FOUserAgent) MacroScreenRenderer(org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer) ScreenRenderer(org.apache.ofbiz.widget.renderer.ScreenRenderer) MacroFormRenderer(org.apache.ofbiz.widget.renderer.macro.MacroFormRenderer) Reader(java.io.Reader) StringReader(java.io.StringReader) FopFactory(org.apache.fop.apps.FopFactory) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) GeneralException(org.apache.ofbiz.base.util.GeneralException) TemplateException(freemarker.template.TemplateException) Fop(org.apache.fop.apps.Fop) StreamSource(javax.xml.transform.stream.StreamSource) ModelTheme(org.apache.ofbiz.widget.model.ModelTheme) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ScreenStringRenderer(org.apache.ofbiz.widget.renderer.ScreenStringRenderer) MacroScreenRenderer(org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer) TemplateException(freemarker.template.TemplateException) ViewHandlerException(org.apache.ofbiz.webapp.view.ViewHandlerException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) GeneralException(org.apache.ofbiz.base.util.GeneralException) Delegator(org.apache.ofbiz.entity.Delegator) FormStringRenderer(org.apache.ofbiz.widget.renderer.FormStringRenderer) VisualTheme(org.apache.ofbiz.widget.renderer.VisualTheme) StringWriter(java.io.StringWriter) Writer(java.io.Writer) PDFEncryptionParams(org.apache.fop.pdf.PDFEncryptionParams)

Example 8 with FopFactory

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

the class ApacheFopWorker method createFopInstance.

/**
 * 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;
}
Also used : Fop(org.apache.fop.apps.Fop) FopFactory(org.apache.fop.apps.FopFactory)

Example 9 with FopFactory

use of org.apache.fop.apps.FopFactory in project Digital by hneemann.

the class DocuTest method testDocu.

public void testDocu() throws IOException, NodeException, PinException, TransformerException, SAXException {
    FopFactory fopFactory = FopFactory.newInstance(new File(Resources.getRoot(), "docu/fop.xconf"));
    File maven = Resources.getRoot().getParentFile().getParentFile().getParentFile();
    File target = new File(maven, "target/docu");
    File target2 = new File(maven, "target/docuDist");
    File images = new File(target, "img");
    images.mkdirs();
    target2.mkdirs();
    final File library = new File(target, "library.xml");
    write74xx(library);
    for (Language l : Lang.getBundle().getSupportedLanguages()) {
        // set language
        Lang.setActualRuntimeLanguage(l);
        final String basename = "Documentation_" + l.getName();
        // write xml
        File xml = new File(target, basename + ".xml");
        try (Writer w = new OutputStreamWriter(new FileOutputStream(xml), "UTF-8")) {
            writeXML(w, images, l.getName(), library);
        }
        // start xslt transformation
        File xslFO = new File(target, basename + ".fo");
        File xslt = new File(Resources.getRoot(), "docu/elem2fo.xslt");
        startXalan(xml, xslt, xslFO);
        // write pdf
        File pdf = new File(target, basename + ".pdf");
        startFOP(fopFactory, xslFO, pdf);
        copy(pdf, new File(target2, "Doc_" + l + ".pdf"));
    }
}
Also used : Language(de.neemann.gui.language.Language) FopFactory(org.apache.fop.apps.FopFactory)

Example 10 with FopFactory

use of org.apache.fop.apps.FopFactory in project opentheso by miledrousset.

the class ApacheFOP method test_basic.

public void test_basic() throws SAXException, IOException, TransformerConfigurationException, TransformerException, ConfigurationException, URISyntaxException {
    // Step 1: Construct a FopFactory by specifying a reference to the configuration file
    // (reuse if you plan to render multiple documents!)
    DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
    Configuration cfg = cfgBuilder.buildFromFile(new File("fop-config.xml"));
    URI baseURI = new URI("https://www.testUri.com");
    FopFactoryBuilder builder;
    builder = new FopFactoryBuilder(baseURI).setConfiguration(cfg);
    FopFactory fopFactory = builder.build();
    // Step 2: Set up output stream.
    // Note: Using BufferedOutputStream for performance reasons (helpful with FileOutputStreams).
    OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("test-fop.pdf")));
    try {
        // Step 3: Construct fop with desired output format
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
        // Step 4: Setup JAXP using identity transformer
        Source xslt = new StreamSource(new File("skos-alpha.xsl"));
        TransformerFactory factory = TransformerFactory.newInstance();
        // identity transformer
        Transformer transformer = factory.newTransformer(xslt);
        // Step 5: Setup input and output for XSLT transformation
        // Setup input stream
        Source src = new StreamSource(new File("test_unesco.rdf"));
        // Resulting SAX events (the generated FO) must be piped through to FOP
        Result res = new SAXResult(fop.getDefaultHandler());
        // Step 6: Start XSLT transformation and FOP processing
        transformer.transform(src, res);
    } finally {
        // Clean-up
        out.close();
    }
}
Also used : DefaultConfigurationBuilder(org.apache.avalon.framework.configuration.DefaultConfigurationBuilder) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) Configuration(org.apache.avalon.framework.configuration.Configuration) Fop(org.apache.fop.apps.Fop) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) StreamSource(javax.xml.transform.stream.StreamSource) FopFactory(org.apache.fop.apps.FopFactory) 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) SAXResult(javax.xml.transform.sax.SAXResult) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

FopFactory (org.apache.fop.apps.FopFactory)14 Fop (org.apache.fop.apps.Fop)11 Transformer (javax.xml.transform.Transformer)9 TransformerFactory (javax.xml.transform.TransformerFactory)9 SAXResult (javax.xml.transform.sax.SAXResult)9 Result (javax.xml.transform.Result)8 StreamSource (javax.xml.transform.stream.StreamSource)8 File (java.io.File)7 Source (javax.xml.transform.Source)6 BufferedOutputStream (java.io.BufferedOutputStream)5 FileOutputStream (java.io.FileOutputStream)5 IOException (java.io.IOException)5 OutputStream (java.io.OutputStream)5 FOPException (org.apache.fop.apps.FOPException)4 FOUserAgent (org.apache.fop.apps.FOUserAgent)4 URI (java.net.URI)3 DOMResult (javax.xml.transform.dom.DOMResult)3 DOMSource (javax.xml.transform.dom.DOMSource)3 StreamResult (javax.xml.transform.stream.StreamResult)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2