the all purpose pdf generation class.
authorjohn <john>
Sat, 15 Jun 2002 23:28:38 +0000 (23:28 +0000)
committerjohn <john>
Sat, 15 Jun 2002 23:28:38 +0000 (23:28 +0000)
takes two arguments, the first is a path to an FO file.
the second is either a file path or a servlet response object,
the pdf is generated and shoved down the appropriate hole.

source/mir/misc/PDFUtil.java [new file with mode: 0755]

diff --git a/source/mir/misc/PDFUtil.java b/source/mir/misc/PDFUtil.java
new file mode 100755 (executable)
index 0000000..4e72f19
--- /dev/null
@@ -0,0 +1,70 @@
+package mir.misc;
+
+import java.io.*;
+import javax.servlet.http.*;
+
+import mircoders.global.*;
+
+import org.apache.fop.apps.* ;
+import org.xml.sax.InputSource;
+import org.xml.sax.XMLReader;
+import org.apache.log.*;
+
+public class PDFUtil {
+    
+  public static void makePDF(String foFilePath,Object pdfDestination) throws Exception
+  {
+    try{
+      Driver driver = new Driver();
+      
+      //stupid logging that fop wants to use, needs to be changed
+      Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
+      Logger fopLog=null;
+      fopLog = hierarchy.getLoggerFor("fop");
+      fopLog.setPriority(Priority.WARN);
+      driver.setLogger(fopLog);
+      
+      driver.setRenderer(Driver.RENDER_PDF);
+  
+      File foFile=new File(foFilePath);
+      
+      String html2foStyleSheetPath=MirGlobal.getConfigProperty("Home") 
+         + MirGlobal.getConfigProperty("HTMLTemplateProcessor.Dir")
+          + "/" 
+          + MirGlobal.getConfigProperty("Producer.PrintableContent.html2foStyleSheetName"); 
+      File html2foStyleSheet=new File(html2foStyleSheetPath);
+      InputHandler inputHandler =
+         new XSLTInputHandler(foFile, html2foStyleSheet);
+      XMLReader parser = inputHandler.getParser();
+      
+      if (pdfDestination instanceof String) {
+       String filePath = (String) pdfDestination;
+       driver.setOutputStream(new FileOutputStream(filePath));
+       driver.render(parser, inputHandler.getInputSource());
+      }
+      else if (pdfDestination instanceof HttpServletResponse){
+       HttpServletResponse res = (HttpServletResponse) pdfDestination; 
+       ByteArrayOutputStream out = new ByteArrayOutputStream();
+       driver.setOutputStream(out);
+       res.setContentType("application/pdf");
+       
+       driver.render(parser, inputHandler.getInputSource());
+       
+       byte[] content = out.toByteArray();
+       res.setContentLength(content.length);
+       res.getOutputStream().write(content);
+       res.getOutputStream().flush();
+      }
+      else {
+       throw new Exception("I'm sorry but I don't know how to output a pdf to an object of type" + pdfDestination.getClass().getName());
+      }
+    }
+  
+    catch (Exception ex){
+       throw(ex);
+    }
+  }
+}
+
+
+