Created
          March 25, 2014 09:58 
        
      - 
      
- 
        Save kawasima/9758405 to your computer and use it in GitHub Desktop. 
    MS Excel to SVG by Clojure
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | (ns pdf2svg | |
| (:require [clojure.java.io :as io]) | |
| (:import [org.apache.pdfbox.pdmodel PDDocument PDPageable] | |
| [org.apache.batik.dom GenericDOMImplementation] | |
| [org.apache.batik.svggen SVGGeneratorContext SVGGraphics2D])) | |
| (let [document (PDDocument/load "hoge.pdf") | |
| pageable (PDPageable. document) | |
| dom-impl (GenericDOMImplementation/getDOMImplementation) | |
| svg-document (.createDocument dom-impl "http://www.w3.org/2000/svg" "svg" nil) | |
| ctx (SVGGeneratorContext/createDefault svg-document)] | |
| (try | |
| (.setEmbeddedFontsOn ctx true) | |
| (doseq [i (range (.getNumberOfPages pageable))] | |
| (let [svg-filename (str "hoge_" i ".svg") | |
| svg-generator (SVGGraphics2D. ctx false)] | |
| (.createNewFile (io/file svg-filename)) | |
| (doto (.getPrintable pageable i) | |
| (.print svg-generator | |
| (.getPageFormat pageable i) | |
| i)) | |
| (.stream svg-generator svg-filename))) | |
| (finally (.close document)))) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | (ns xls2pdf | |
| (:import [com.jacob.activeX ActiveXComponent] | |
| [com.jacob.com Dispatch Variant]) | |
| (let [ms-excel (ActiveXComponent. "Excel.Application") | |
| workbooks (.. ms-excel (getProperty "workbooks") toDispatch) | |
| workbook (.. Dispatch (call workbooks "Open" | |
| (into-array Variant | |
| [(Variant. "hoge.xls") | |
| (Variant. "1")])) toDispatch)] | |
| (try | |
| (Dispatch/call workbook "ExportAsFixedFormat" | |
| (into-array Variant | |
| [(Variant. "0") | |
| (Variant. "hoge.pdf")])) | |
| (finally | |
| (Dispatch/call workbook "Close") | |
| (.invoke ms-excel "Quit")))) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment