diff --git a/README.adoc b/README.adoc index 700b5a9..8ff6977 100644 --- a/README.adoc +++ b/README.adoc @@ -11,7 +11,7 @@ Add the following to your http://github.com/technomancy/leiningen[Leiningen's] ` .project.clj [source,clojure] ---- -[net.tbt-post/clj-jasper "0.0.6"] +[net.tbt-post/clj-jasper "0.0.7"] ---- .example.clj @@ -35,6 +35,6 @@ Add the following to your http://github.com/technomancy/leiningen[Leiningen's] ` == License -Copyright © 2018-2019 AI a.k.a. MelKori +Copyright © 2018-2020 AI a.k.a. MelKori Distributed under the MIT License. diff --git a/project.clj b/project.clj index 7fee6f9..210eb3c 100644 --- a/project.clj +++ b/project.clj @@ -1,15 +1,14 @@ -(defproject net.tbt-post/clj-jasper "0.0.6" +(defproject net.tbt-post/clj-jasper "0.0.7" :description "A Clojure library to work with Jasper Reports" :url "https://github.com/source-c/clj-jasper" :license {:name "MIT License"} :dependencies [;; Jasper Libs - [net.sf.jasperreports/jasperreports "6.8.0" + [net.sf.jasperreports/jasperreports "6.12.2" :exclusions [com.lowagie/itext]] [net.sf.jasperreports/jasperreports-fonts "6.0.0"] - ;[com.lowagie/itext "4.2.2"] [com.lowagie/itext "2.1.7"] [net.sourceforge.barbecue/barbecue "1.5-beta1"] - [com.google.zxing/core "3.3.3"] + [com.google.zxing/core "3.4.0"] [net.sf.barcode4j/barcode4j "2.1"] - [org.apache.xmlgraphics/batik-bridge "1.11"]]) + [org.apache.xmlgraphics/batik-bridge "1.13"]]) diff --git a/src/clj_jasper/core.clj b/src/clj_jasper/core.clj index 18e17b5..9516cfc 100644 --- a/src/clj_jasper/core.clj +++ b/src/clj_jasper/core.clj @@ -2,15 +2,18 @@ (:require [clojure.java.io :as io] [clojure.walk :as walk] [clojure.string :as string]) - - (:import (java.util HashMap Map) - (net.sf.jasperreports.engine JasperCompileManager - JasperFillManager - JasperExportManager - JRDataSource - JRRewindableDataSource - JRField - JasperReport))) + (:import + (clojure.lang ExceptionInfo) + (java.io ByteArrayOutputStream OutputStream) + (java.util HashMap Map ArrayList) + (net.sf.jasperreports.engine JasperCompileManager + JasperFillManager + JRDataSource + JRRewindableDataSource + JRField + JasperReport JasperPrint) + (net.sf.jasperreports.engine.export JRPdfExporter) + (net.sf.jasperreports.export SimpleExporterInput SimpleOutputStreamExporterOutput))) (defn- data->jr [coll] (let [initial (cons nil coll) @@ -40,18 +43,39 @@ read-template)] (some-> template io/input-stream JasperCompileManager/compileReport))) -(defn data->report [{:keys [name data mtype filename ops report] - :or {mtype :pdf}}] +(defn- fill [^JasperReport report data parameters] + (JasperFillManager/fillReport + report + ^Map (HashMap. ^Map (walk/stringify-keys (or parameters {}))) + ^JRDataSource (data->jr (or data [])))) - (let [report (or report (template->object name)) - j-data (data->jr (or data {:empty true})) - filled (JasperFillManager/fillReport - ^JasperReport report - ^Map (HashMap. ^Map (walk/stringify-keys (or ops {}))) - ^JRDataSource j-data) - bytes (case mtype - :pdf (JasperExportManager/exportReportToPdf filled))] +(defn- exporter [mtype] + (case mtype + :pdf (JRPdfExporter.) + (throw (ExceptionInfo. (format "Unknown mtype: '%s'" mtype) {:mtype mtype})))) +(defn data->report [{:keys [name data mtype filename ops report] + :or {mtype :pdf}}] + (let [report (or report (template->object name)) + print (fill report data ops) + exporter (exporter mtype) + baos (ByteArrayOutputStream.)] + (.setExporterInput exporter (SimpleExporterInput. ^JasperPrint print)) + (.setExporterOutput exporter (SimpleOutputStreamExporterOutput. baos)) + (.exportReport exporter) {:name (or filename (format "%s.%s" (last (string/split name #"/")) (clojure.core/name mtype))) :type (get mime-types mtype) - :file bytes})) + :file (.toByteArray baos)})) + +(defn render-multiple [^OutputStream output-stream + reports + {:keys [compile mtype common-parameters] + :or {compile template->object + mtype :pdf}}] + (let [prints (ArrayList.) + exporter (exporter mtype)] + (doseq [{:keys [name data parameters]} reports] + (.add prints (fill (compile name) data (merge parameters common-parameters)))) + (.setExporterInput exporter (SimpleExporterInput/getInstance prints)) + (.setExporterOutput exporter (SimpleOutputStreamExporterOutput. output-stream)) + (.exportReport exporter)))