Reputation: 899
I need to call a clojure-function from java/scala which expects a vector or an inputstream as its first argument.
Doing so always yields the following exception:
Execution exception[[UnsupportedOperationException: pdf (clj-pdf.main/-pdf not defined?)]]
I am using clj-pdf and need to call the 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)))
I have modified the source, built a jar via
leiningen uberjar
The modifications to cjl-pdf's project.clj can be seen in the last 2 lines:
(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)
and in my added 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])
I am using the lib from my scala code as follows:
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)
Is there any way to get around this?
Upvotes: 2
Views: 498
Reputation: 8591
It works from Java:
java.util.ArrayList a = new java.util.ArrayList();
java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream();
clj_pdf.main.pdf (a, b);
if I add to main.clj:
(defn -pdf [in out] (pdf in out))
And use the lein uberjar
to build the project.
Another alternative is to use the clojure.lang.RT
as per https://stackoverflow.com/a/6410926/151650
Upvotes: 2