View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.application;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  import org.apache.fop.apps.Driver;
25  import org.apache.myfaces.tobago.util.Slf4jLogger;
26  import org.xml.sax.InputSource;
27  
28  import javax.faces.FacesException;
29  import javax.servlet.ServletOutputStream;
30  import javax.servlet.ServletResponse;
31  import java.io.StringReader;
32  
33  public class FopConverter {
34  
35    private static final Logger LOG = LoggerFactory.getLogger(FopConverter.class);
36  
37    public static void fo2Pdf(ServletResponse servletResponse, String buffer) {
38      if (LOG.isDebugEnabled()) {
39        LOG.debug("buffer = '{}'", buffer);
40      }
41      try {
42        Driver driver = new Driver();
43        driver.setLogger(new Slf4jLogger(LOG));
44        driver.setRenderer(Driver.RENDER_PDF);
45        driver.setErrorDump(true);
46  //      driver.setInputSource(new InputSource(new FileInputStream("C:/simple.fo")));
47  //      driver.setInputSource(new InputSource(new StringReader(fo)));
48  
49        if (LOG.isDebugEnabled()) {
50          LOG.debug("bufferString = '" + buffer + "'");
51  
52        }
53        LOG.error("bufferString = '" + buffer + "'");
54        driver.setInputSource(new InputSource(new StringReader(buffer)));
55  //      driver.setOutputStream(new FileOutputStream("C:/simple.pdf"));
56        ServletOutputStream outputStream = servletResponse.getOutputStream();
57  //      FileOutputStream outputStream = new FileOutputStream("c:/simple.pdf");
58  //      ResponseStream outputStream = facesContext.getResponseStream();
59        driver.setOutputStream(outputStream);
60  //      Map rendererOptions = new java.util.HashMap();
61  //      rendererOptions.put("ownerPassword", "mypassword");
62  //      rendererOptions.put("allowCopyContent", "FALSE");
63  //      rendererOptions.put("allowEditContent", "FALSE");
64  //      rendererOptions.put("allowPrint", "FALSE");
65  //      driver.getRenderer().setOptions(rendererOptions);
66        driver.run();
67        outputStream.flush();
68        outputStream.close();
69      } catch (Exception e) {
70        LOG.error("", e);
71        throw new FacesException(e);
72      }
73    }
74  }