Skip to content

Commit

Permalink
big performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ibarrick committed Dec 13, 2018
1 parent 9dd7a12 commit dadaa52
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 90 deletions.
4 changes: 2 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(defproject clj-pdf "2.2.33"
(defproject clj-pdf "2.2.34"
:description "PDF generation library"
:url "https://github.com/yogthos/clj-pdf"
:url "https://github.com/ibarrick/clj-pdf"

:license {:name "GNU Lesser General Public License - v 3"
:url "http://www.gnu.org/licenses/lgpl.html"
Expand Down
3 changes: 2 additions & 1 deletion src/clj/clj_pdf/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
(= [:clear-double-page] item) (clear-double-page stylesheet references font-style width height item doc pdf-writer)
:else (.add doc
(make-section
doc
(assoc font-style
:stylesheet stylesheet
:references references
Expand Down Expand Up @@ -140,7 +141,7 @@
;; :header and :footer are different for the root document meta map vs. the meta map
;; that is expected for :pdf-table (which is what 'x' here should be at this point)
;; TODO: remove other possible map key conflicts? i think these are the only 2 ...
(make-section (dissoc meta :header :footer) x)))
(make-section doc (dissoc meta :header :footer) x)))

;; FIXME: unused?
(defn table-header-footer-height [content meta ^Document doc page-numbers? footer?]
Expand Down
26 changes: 14 additions & 12 deletions src/clj/clj_pdf/section.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
(declare ^:dynamic *cache*)


(defmulti render (fn [tag meta & els] tag))
(defmethod render :default [tag meta & els]
(defmulti render (fn [tag doc meta & els] tag))
(defmethod render :default [tag doc meta & els]
(throw (ex-info (str "invalid tag: " tag) {:meta meta :content els})))


Expand All @@ -15,14 +15,14 @@


(defn make-section
([element]
([doc element]
(cond
(empty? element) ""
(every? sequential? element) (doseq [item element]
(make-section item))
element (make-section {} element)
(make-section doc item))
element (make-section doc {} element)
:else ""))
([meta element]
([doc meta element]
(try
(cond
(string? element) element
Expand All @@ -40,16 +40,18 @@
class-attrs (merge class-attrs)
attrs (merge attrs))]

(apply render tag new-meta elements)))
(apply render tag doc new-meta elements)))
(catch Exception e
(prn meta element)
(throw (ex-info "failed to parse element" {:meta meta :element element} e))))))
(println e)
(throw e)
(comment (prn meta element)
(throw (ex-info "failed to parse element" {:meta meta :element element} e)))))))


(defn make-section-or [if-string meta item]
(defn make-section-or [doc if-string meta item]
(if (string? item)
(render if-string meta item)
(make-section meta item)))
(render if-string doc meta item)
(make-section doc meta item)))


;; that require is here to overcome circular import
Expand Down
74 changes: 68 additions & 6 deletions src/clj/clj_pdf/section/cell.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
(ns clj-pdf.section.cell
(:require [clj-pdf.utils :refer [get-color get-alignment]]
[clj-pdf.section :refer [render make-section-or]])
(:require [clj-pdf.utils :refer [get-color get-alignment font]]
[clj-pdf.section :refer [render make-section-or]]
[clj-pdf.section.core :refer [set-background]])
(:import [cljpdf.text Cell Rectangle]
[cljpdf.text Chunk Font Paragraph Document]
[cljpdf.text.pdf PdfPCell]))


Expand All @@ -14,7 +16,7 @@


(defmethod render :cell
[_ {:keys [background-color
[_ ^Document doc {:keys [background-color
colspan
rowspan
border
Expand Down Expand Up @@ -54,7 +56,7 @@
(.setHorizontalAlignment c ^int (get-alignment align))

(doseq [item content]
(.addElement c (make-section-or :chunk meta item)))
(.addElement c (make-section-or doc :chunk meta item)))

c))

Expand All @@ -78,7 +80,7 @@


(defmethod render :pdf-cell
[_ {:keys [background-color
[_ ^Document doc {:keys [background-color
colspan
rowspan
border
Expand Down Expand Up @@ -132,6 +134,66 @@
(.setVerticalAlignment c ^int (get-alignment valign))

(doseq [item content]
(.addElement c (make-section-or :paragraph meta item)))
(.addElement c (make-section-or doc :paragraph meta item)))
c))

(defn create-chunk ^Chunk [meta ^String item]
(let [ch (new Chunk ^String item ^Font (font meta))]
(set-background ch meta)
ch))

(defn render-pdf-cell2
[^Document doc {:keys [background-color
colspan
rowspan
border
align
valign
set-border
border-color
border-width
border-width-bottom
border-width-left
border-width-right
border-width-top
padding
padding-bottom
padding-left
padding-right
padding-top
rotation
height
min-height] :as meta}
content]

(let [c (PdfPCell.)]

(when-let [color (get-color background-color)]
(.setBackgroundColor c color))

(when-let [color (get-color border-color)]
(.setBorderColor c color))

(when (not (nil? border))
(.setBorder c (if border Rectangle/BOX Rectangle/NO_BORDER)))

(when rowspan (.setRowspan c (int rowspan)))
(when colspan (.setColspan c (int colspan)))
(when set-border (.setBorder c (int (get-border set-border))))
(when border-width (.setBorderWidth c (float border-width)))
(when border-width-bottom (.setBorderWidthBottom c (float border-width-bottom)))
(when border-width-left (.setBorderWidthLeft c (float border-width-left)))
(when border-width-right (.setBorderWidthRight c (float border-width-right)))
(when border-width-top (.setBorderWidthTop c (float border-width-top)))
(when padding (pdf-cell-padding c padding))
(when padding-bottom (.setPaddingBottom c (float padding-bottom)))
(when padding-left (.setPaddingLeft c (float padding-left)))
(when padding-right (.setPaddingRight c (float padding-right)))
(when padding-top (.setPaddingTop c (float padding-top)))
(when rotation (.setRotation c (int rotation)))
(when height (.setFixedHeight c (float height)))
(when min-height (.setMinimumHeight c (float min-height)))
(.setHorizontalAlignment c ^int (get-alignment align))
(.setVerticalAlignment c ^int (get-alignment valign))
(.addElement c (create-chunk meta content))
c))

0 comments on commit dadaa52

Please sign in to comment.