Skip to content

Commit

Permalink
refactored entry function
Browse files Browse the repository at this point in the history
  • Loading branch information
yogthos committed Jul 22, 2012
1 parent 8e2e73e commit d9a5079
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion project.clj
@@ -1,5 +1,5 @@
(defproject clj-pdf
"0.9.3-SNAPSHOT"
"0.9.4-SNAPSHOT"
:description "PDF generation library"
:dependencies [[org.clojure/clojure "1.4.0"]
[jfree/jfreechart "1.0.13"]
Expand Down
29 changes: 15 additions & 14 deletions src/clj_pdf/core.clj
Expand Up @@ -565,31 +565,33 @@
document consists of a vector containing a map which defines the document metadata and the contents of the document
out can either be a string which will be treated as a filename or an output stream"
[[doc-meta & content] out]

(let [[doc width height temp-stream output-stream] (setup-doc doc-meta out)]
(doseq [item content]
(append-to-doc (:font doc-meta) width height (if (string? item) [:paragraph item] item) doc))
(.close doc)
(when (:pages doc-meta) (write-total-pages doc width (:footer doc-meta) temp-stream output-stream))))


(defn to-pdf [input-reader r out]
(let [doc-meta (input-reader r)
[doc width height temp-stream output-stream] (clj-pdf.core/setup-doc doc-meta out)]
(loop []
(if-let [item (input-reader r)]
(do
(clj-pdf.core/append-to-doc (:font doc-meta) width height (if (string? item) [:paragraph item] item) doc)
(recur))
(do
(.close doc)
(when (:pages doc-meta) (clj-pdf.core/write-total-pages doc width (:footer doc-meta) temp-stream output-stream)))))))

(defn stream-doc
"reads the document from an input stream one form at a time and writes it out to the output stream
NOTE: setting the :pages to true in doc meta will require the entire document to remain in memory for
post processing!"
[in out]
(with-open [r (new PushbackReader (new InputStreamReader in))]
(binding [*read-eval* false]
(let [doc-meta (read r nil nil)
[doc width height temp-stream output-stream] (setup-doc doc-meta out)]
(loop []
(if-let [item (read r nil nil)]
(do
(append-to-doc (:font doc-meta) width height (if (string? item) [:paragraph item] item) doc)
(recur))
(do
(.close doc)
(when (:pages doc-meta) (write-total-pages doc width (:footer doc-meta) temp-stream output-stream)))))))))
(to-pdf (fn [r] (read r nil nil)) out))))


(defn pdf
Expand All @@ -600,5 +602,4 @@
[in out]
(if (instance? InputStream in)
(stream-doc in out)
(write-doc in out)))

(write-doc in out)))

0 comments on commit d9a5079

Please sign in to comment.