took code for on-the-fly pdf generation out
authorjohn <john>
Tue, 28 May 2002 16:25:31 +0000 (16:25 +0000)
committerjohn <john>
Tue, 28 May 2002 16:25:31 +0000 (16:25 +0000)
of OutputMir servlet and put it into OpenMir

source/mircoders/servlet/ServletModuleOpenIndy.java

index 4148b35..2c04c82 100755 (executable)
@@ -13,6 +13,15 @@ import freemarker.template.*;
 import com.oreilly.servlet.multipart.*;
 import com.oreilly.servlet.*;
 
+import org.xml.sax.InputSource;
+import org.xml.sax.XMLReader;
+
+import org.apache.fop.apps.Driver;
+import org.apache.fop.apps.Version;
+import org.apache.fop.apps.XSLTInputHandler;
+
+import org.apache.log.*;
+
 import mir.servlet.*;
 import mir.module.*;
 import mir.misc.*;
@@ -507,6 +516,69 @@ public class ServletModuleOpenIndy extends ServletModule
     deliver(req, res, mergeData, postingFormDoneTemplate);
   }
 
+/**
+  *  Method for dynamically generating a pdf from a fo file
+  */
+
+
+  public void getpdf(HttpServletRequest req, HttpServletResponse res)
+    throws ServletModuleException, ServletModuleUserException {
+    String ID_REQUEST_PARAM = "id";
+    
+    String generateFO=MirConfig.getProp("GenerateFO");
+    String generatePDF=MirConfig.getProp("GeneratePDF");
+
+    //don't do anything if we are not making FO files, or if we are pregenerating PDF's
+    if (generateFO.equals("yes") && generatePDF.equals("no")){
+    
+      //fop complains unless you do the logging this way
+      Logger log = null;
+      Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
+      log = hierarchy.getLoggerFor("fop");
+      log.setPriority(Priority.WARN);
+    
+      String producerStorageRoot=MirConfig.getProp("Producer.StorageRoot");
+      String producerDocRoot=MirConfig.getProp("Producer.DocRoot");
+      String templateDir = MirConfig.getPropWithHome("HTMLTemplateProcessor.Dir");
+      String xslSheet=templateDir + "/" 
+                    + MirConfig.getProp("Producer.PrintableContent.html2foStyleSheetName");
+      try {
+        String idParam = req.getParameter(ID_REQUEST_PARAM);
+        if (idParam != null){
+          EntityContent contentEnt = (EntityContent)contentModule.getById(idParam);
+         String publishPath = contentEnt.getValue("publish_path");
+         String foFile = producerStorageRoot + producerDocRoot + "/" 
+                        + publishPath + "/" + idParam + ".fo";
+          XSLTInputHandler input = new XSLTInputHandler(new File(foFile), 
+                                                       new File(xslSheet));
+          
+         ByteArrayOutputStream out = new ByteArrayOutputStream();
+          res.setContentType("application/pdf");
+
+          Driver driver = new Driver();
+          driver.setLogger(log);
+          driver.setRenderer(Driver.RENDER_PDF);
+          driver.setOutputStream(out);
+          driver.render(input.getParser(), input.getInputSource());
+
+          byte[] content = out.toByteArray();
+          res.setContentLength(content.length);
+          res.getOutputStream().write(content);
+          res.getOutputStream().flush();
+       } 
+        else {
+         throw new ServletModuleUserException("Can't generate a PDF without an id parameter.");
+       }
+      } 
+      catch (Exception ex) {
+       throw new ServletModuleException(ex.toString());
+      }
+    }
+    else {
+      throw new ServletModuleUserException("Can't generate a PDF because the config tells me not to.");
+    }
+  }
+  
   private void _throwBadContentType (String fileName, String contentType)
     throws ServletModuleUserException {
 
@@ -531,3 +603,4 @@ public class ServletModuleOpenIndy extends ServletModule
 }
 
 
+