2

我需要从 java/scala 调用一个 clojure 函数,它需要一个向量或一个输入流作为它的第一个参数。

这样做总是会产生以下异常:

执行异常[[UnsupportedOperationException: pdf (clj-pdf.main/-pdf not defined?)]]

我正在使用clj-pdf并且需要调用 pdf-function

(defn pdf
  "usage:
   in can be either a vector containing the document or an input stream. If in is an input stream then the forms will be read sequentially from it.
   out can be either a string, in which case it's treated as a file name, or an output stream.
   NOTE: using the :pages option will cause the complete document to reside in memory as it will need to be post processed."
  [in out]
  (if (instance? InputStream in)
    (stream-doc in out)
    (write-doc in out)))

我已经修改了源代码,通过

leiningen uberjar

对 cjl-pdf 的 project.clj 的修改可以在最后 2 行看到:

(defproject clj-pdf 
  "1.0.6"
  :description "PDF generation library"
  :url "https://github.com/yogthos/clj-pdf"
  :license {:name "GNU Lesser General Public License - v 3"
            :url "http://www.gnu.org/licenses/lgpl.html"
            :distribution :repo
            :comments "same as  iText and JFreeChart"}
  :dependencies [[org.clojure/clojure "1.5.0"]
                 [jfree/jfreechart "1.0.13"]                 
                 [itext-min "0.2"]]

  :aot [clj-pdf.main]
  :main clj-pdf.main)

在我添加的 main.clj 中:

(ns clj-pdf.main
  (:gen-class
   ;; neither java.io.InputStream nor ArrayList work:
   :methods [#^{:static true} [pdf [java.util.ArrayList, java.io.OutputStream] void]])
  (:use clj-pdf.core))

(defn -main [& args])

我正在使用我的 scala 代码中的 lib,如下所示:

val output = new ByteArrayOutputStream()
val list = new java.util.ArrayList[String]
list.add( """[:list {:roman true} 
             [:chunk {:style :bold} "a bold item"] "another item" "yet another item"]
             [:phrase "some text"]                                   
             [:paragraph "yet more text"]]""")

clj_pdf.main.pdf(list, output)

有没有办法解决这个问题?

4

1 回答 1

2

它适用于 Java:

    java.util.ArrayList a = new java.util.ArrayList();
    java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
    clj_pdf.main.pdf (a, b);

如果我添加到 main.clj:

 (defn -pdf [in out] (pdf in out))

并使用lein uberjar来构建项目。

另一种选择是clojure.lang.RT按照https://stackoverflow.com/a/6410926/151650使用

于 2013-03-11T22:27:02.437 回答